Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt QML ItemDelegate突出显示的属性不起作用_Qt_Hover_Qml_Highlight_Qitemdelegate - Fatal编程技术网

Qt QML ItemDelegate突出显示的属性不起作用

Qt QML ItemDelegate突出显示的属性不起作用,qt,hover,qml,highlight,qitemdelegate,Qt,Hover,Qml,Highlight,Qitemdelegate,我想在itemregate中自定义高亮显示颜色。如果我对材质主题使用默认的itemdegate,那么当我将鼠标悬停在该项目上时,所有的ok和颜色都会改变,但当我重新定义背景时,它会崩溃,颜色不再改变 MyItemDelegate.qml: 为什么突出显示的属性不起作用?如何自定义此颜色?问题很简单,高亮显示的属性不是从头创建的,您必须激活它,最常见的是它与ListView.isCurrentItem绑定,因此您必须更新currentItem: MyItemDelegate.qml import

我想在
itemregate
中自定义高亮显示颜色。如果我对材质主题使用默认的
itemdegate
,那么当我将鼠标悬停在该项目上时,所有的ok和颜色都会改变,但当我重新定义背景时,它会崩溃,颜色不再改变

MyItemDelegate.qml:


为什么突出显示的
属性不起作用?如何自定义此颜色?

问题很简单,高亮显示的属性不是从头创建的,您必须激活它,最常见的是它与
ListView.isCurrentItem
绑定,因此您必须更新
currentItem

MyItemDelegate.qml

import QtQuick 2.11
import QtQuick.Controls.Material 2.4
import QtQuick.Controls 2.4
import QtQuick.Templates 2.4 as T

T.ItemDelegate {
    id: myItemDelegate
    height: 40
    anchors.left: parent.left
    anchors.right: parent.right
    highlighted: ListView.isCurrentItem // <---
    contentItem: Text {
        text: "Hello"
        anchors.fill: parent
        verticalAlignment: Text.AlignVCenter
        horizontalAlignment: Text.AlignHCenter
    }
    background: Rectangle {
        anchors.fill: myItemDelegate
        color: myItemDelegate.highlighted ? "blue" : "transparent"
    }
}
import QtQuick 2.9
import QtQuick.Controls 2.2

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Scroll")
    ListView {
        id: listView
        anchors.fill: parent
        model: 20
        delegate: MyItemDelegate {
            MouseArea{
                anchors.fill: parent
                hoverEnabled: true
                onHoveredChanged: listView.currentIndex = index
            }
        }
    }
}
import QtQuick 2.9
import QtQuick.Controls 2.2

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Scroll")
    ListView {
        id: listView
        anchors.fill: parent
        model: 20
        delegate: MyItemDelegate {
            MouseArea{
                anchors.fill: parent
                hoverEnabled: true
                onHoveredChanged: listView.currentIndex = index
            }
        }
    }
}