【问题标题】:Setting QML button background by QtQuick Styles通过 QtQuick 样式设置 QML 按钮背景
【发布时间】:2014-12-09 11:43:51
【问题描述】:

我在尝试按照documention 设置按钮背景时遇到了很多困难。

由于某种原因,Qt 找不到导入 QtQuick.Controls.Styles。我尝试以多种方式导入它,但没有成功。我的最后一次尝试:

import QtQuick 2.3
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2

import QtGraphicalEffects 1.0

Button {
    id: btn
    property int row
    property int col
    property Item buttonPreview
    property bool hasPreview: true
    GridLayout.row: row
    GridLayout.col: col
    GridLayout.colSpan: 2
    GridLayout.rowSpan: 2
    width: 50
    height: 50

    style: ButtonStyle {
        background: Rectangle {
                 color:"white"
                }
    }

...

}

我遇到以下错误:

  1. import QtQuick.Controls.Styles 1.2 代码下划线表示 QML module not found
  2. Btn.qml:21:5: Cannot assign to non-existent property "style" 在我使用应用程序时出现。

奇怪的是,如果我删除 import QtQuick.Controls.Styles 1.2style: ButtonStyle { 会变成下划线。

我使用Qt 5.3.2

我的 .pro 配置文件的一部分:

QT       += core gui svg xml network quick gui-private qml quickwidgets widgets concurrent
TARGET = ProjectName 
TEMPLATE = app
CONFIG += c++12 plugin

感谢您的任何想法,非常感谢您花时间帮助解决我的问题。

【问题讨论】:

  • 您使用的是哪个 Qt 版本? <qtdir>/qml/QtQuick/Controls/Styles 中有什么内容?
  • qml\QtQuick\Controls\Styles 下有 2 个目录和一个文件:Base、Desktop 目录和 qmldir 文件。
  • 顺便说一句,我认为CONFIG += c++12 必须是CONFIG += c++11
  • 好点应该是 c++11。我的 Qt creator 版本是 3.1。
  • 你解决问题了吗。我也有同样的问题,我无法设置背景

标签: qt qml qt5 qt-quick qtquick2


【解决方案1】:
  1. 我认为未找到模块的问题与以前版本的 Qt 有关。您应该使用维护工具删除 Qt5.2。 之后,我在 Window 中尝试了您的 QML 代码,它工作正常(当然是在删除 GridLayouts 之后)。

  2. GridLayout 需要模块import QtQuick.Layouts 1.1 并应用于Grid,但您没有Grid

【讨论】:

    【解决方案2】:

    尝试改变

    import QtQuick.Controls.Styles 1.2

    import QtQuick.Controls.Styles 1.0

    【讨论】:

      猜你喜欢
      • 2012-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多