首页 > 解决方案 > 在设置 TableView Scroll 样式后,我在 QML 中收到警告

问题描述

我正在使用以下代码在 TableView 组件中设置处理程序的样式:

TableView {
    style: ScrollViewStyle{
        handle: Rectangle {
            implicitWidth: globals.scrollBar.handlerWidth
            implicitHeight: globals.scrollBar.handlerWidth
            color:  globals.scrollBar.handlerColor
        }
    }
}

属性是:

property var scrollBar: QtObject {
    property int handlerWidth: 14
    property color handlerColor: "#6b6b6b"
}

我尝试切换,int但我仍然有同样的问题。colorvar

全局变量在 main 上定义,如下所示:

Globals {
    id: globals
}

现在我在 QT 中收到很多警告

file:///C:/Qt/5.11.1/mingw53_32/qml/QtQuick/Controls/Private/BasicTableView.qml:393:48: Unable to assign [undefined] to bool
file:///C:/Qt/5.11.1/mingw53_32/qml/QtQuick/Controls/Private/BasicTableView.qml:97:38: Unable to assign [undefined] to QQmlComponent*
file:///C:/Qt/5.11.1/mingw53_32/qml/QtQuick/Controls/Private/BasicTableView.qml:461:20: Unable to assign [undefined] to QColor

我错过了什么?

使用 qml 创建一个空的 qt (5.12) 应用程序的最小功能示例

import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Controls 1.4
    import QtQuick.Layouts 1.3
import QtQuick.Controls.Styles 1.1

ApplicationWindow {
    id: window
    title: "Stack"
    visible: true
    width: 1400
    ListModel {
        id: libraryModel
        ListElement {
            title: "A Masterpiece"
            author: "Gabriel"
        }
        ListElement {
            title: "Brilliance"
            author: "Jens"
        }
        ListElement {
            title: "Outstanding"
            author: "Frederik"
        }
    }

    Page {
        id: page
        anchors.fill: parent
        background: { color:"black" }
        TableView{
            style: ScrollViewStyle{//TODO:  ScrollViewStyle is giving a lot of warnings
                handle: Rectangle {
                    implicitWidth: 15
                    implicitHeight: 15
                    color:  "#000000"
                }
                minimumHandleLength: 30
            }
            anchors.fill:parent
            TableViewColumn {
                role: "title"
                title: "Title"
                width: 100
            }
            TableViewColumn {
                role: "author"
                title: "Author"
                width: 200
            }
            model: libraryModel
            itemDelegate: Text
            {
              text: styleData.value
              elide: Text.ElideRight
            }
        }
    }
}

也许错误/警告与不兼容的进口有关?

标签: qtqml

解决方案


如果您使用TableView那么您必须使用TableViewStyle而不是ScrollViewStyle。并且您可以继续使用相同的属性,因为TableViewStyle继承自ScrollViewStyle

style: TableViewStyle{
    handle: Rectangle {
        implicitWidth: 15
        implicitHeight: 15
        color:  "#000000"
    }
    minimumHandleLength: 30
}

删除下面这行代码,因为 background 需要一个 Component,但也没有必要,因为 TableView 会占据整个 Page:

background: { color:"black" }

推荐阅读