通过 QtQuick 样式设置 QML 按钮背景



当我尝试按照文档设置我的按钮背景时,我遇到了很多困难。

由于某种原因,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

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

  1. 我认为,未找到模块的问题出在以前版本的Qt上。您应该使用维护工具删除 Qt5.2。之后,我在Window中尝试了您的 QML 代码,它起作用了(当然是在删除GridLayout之后)。

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

尝试更改

import QtQuick.Controls.Styles 1.2

import QtQuick.Controls.Styles 1.0

最新更新