C++ 如何正确清除Qml ListView选择
如果我有以下资料:C++ 如何正确清除Qml ListView选择,c++,qt,qml,C++,Qt,Qml,如果我有以下资料: import QtQuick 2.4 import QtQuick.Controls 1.3 import QtQuick.Window 2.2 import QtQuick.Dialogs 1.2 ApplicationWindow { title: qsTr("Hello World") width: 800 height: 700 visible: true property var myArray: [1, 2, 3
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
ApplicationWindow {
title: qsTr("Hello World")
width: 800
height: 700
visible: true
property var myArray: [1, 2, 3, 4, 5, 6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
menuBar: MenuBar {
Menu {
title: qsTr("&File")
MenuItem {
text: qsTr("&Open")
onTriggered: messageDialog.show(qsTr("Open action triggered"));
}
MenuItem {
text: qsTr("E&xit")
onTriggered: Qt.quit();
}
}
}
Rectangle {
id: myButton
anchors.top: parent.top
anchors.topMargin: 5
color: "yellow"
width: 100
height: 25
radius: 3
anchors.horizontalCenter: parent.horizontalCenter
Text {
text: "Clear Selection"
anchors.fill: parent
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
MouseArea {
anchors.fill: parent
onClicked: {
myListView.currentIndex = -1
}
}
}
ListView {
id: myListView
width: 300
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: myButton.bottom
anchors.topMargin: 5
anchors.bottom: parent.bottom
currentIndex: -1
//highlightFollowsCurrentItem: false
highlight: Rectangle {
color: "pink"
radius: 3
width: parent.width - 10
height: 25
//y: myListView.currentItem.y
anchors.horizontalCenter: parent.horizontalCenter
}
clip: true
model: myArray
delegate: Rectangle {
width: parent.width - 10
height: 25
color: "transparent"
border.color: "cyan"
anchors.horizontalCenter: parent.horizontalCenter
Text {
text: myArray[index]
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
anchors.fill: parent
}
MouseArea {
anchors.fill: parent
onClicked: myListView.currentIndex = index
}
}
}
MessageDialog {
id: messageDialog
title: qsTr("May I have your attention, please?")
function show(caption) {
messageDialog.text = caption;
messageDialog.open();
}
}
}
单击清除选择按钮时,我收到以下信息:qrc:/main.qml:67:TypeError:无法读取null的属性
qrc:/main.qml:64:TypeError:无法读取null的属性 如何清除选择而不出现错误?它似乎没有使应用程序崩溃,但我有一个列表视图,它会根据另一个列表视图选择进行更改,并且错误会发生几次,使Qt Creator中的调试输出变得混乱。我在Qt 5.4和5.5中注意到了这一点,它说: 将为每个列表创建高亮显示组件的实例。除非highlightFollowsCurrentItem属性为false,否则结果零部件实例的几何图形由列表管理,以便与当前项保持一致 因此,您不需要自己尝试管理突出显示项的位置。如果要定位高亮显示,请改为创建中间父项:
highlight: Item {
Rectangle {
color: "pink"
radius: 3
width: parent.width - 10
height: 25
anchors.horizontalCenter: parent.horizontalCenter
}
}
至于发生这种情况的原因,很可能是因为高亮显示项被重新出租,使其处于一种状态,即其父属性为null
。您可以使用以下代码对此进行测试:
anchors.horizontalCenter: { print(parent); parent.horizontalCenter }
通常的问题是,如果你有一个foo
,它应该有一个bar
,那么你将它引用为foo.bar
,但是,如果foo
没有正确初始化,那么它就不能有bar
,因为它不存在(现在)。在您的情况下,父级
似乎没有正确初始化,因此它没有相应的宽度
和水平中心
(可能在委托中)。解决方案是正确初始化要使用其成员的对象,在我们的例子中,parent
我也在Qt论坛()上问过这个问题,但是stack的回答更快。检查父值是否有效:
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
ApplicationWindow {
title: qsTr("Hello World")
width: 800
height: 700
visible: true
property int myMargin: 5
menuBar: MenuBar {
Menu {
title: qsTr("&File")
MenuItem {
text: qsTr("&Open")
onTriggered: messageDialog.show(qsTr("Open action triggered"));
}
MenuItem {
text: qsTr("E&xit")
onTriggered: Qt.quit();
}
}
}
Rectangle {
id: myButton
anchors.top: parent.top
anchors.topMargin: myMargin
color: "yellow"
width: 100
height: 25
radius: 3
anchors.horizontalCenter: parent.horizontalCenter
Text {
text: "Clear Selection"
anchors.fill: parent
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
MouseArea {
anchors.fill: parent
onClicked: {
myListView.currentIndex = -1
}
}
}
Rectangle {
width: 300
anchors.top: myButton.bottom
anchors.topMargin: myMargin
anchors.bottom: parent.bottom
anchors.horizontalCenter: parent.horizontalCenter
ListView {
id: myListView
anchors.fill: parent
currentIndex: -1
spacing: 3
highlightMoveDuration: 25
highlight: Rectangle {
width: parent ? parent.width - 10 : 0
height: parent ? 25 : 0
color: "pink"
radius: 3
anchors.horizontalCenter: parent ? parent.horizontalCenter : undefined
}
clip: true
model: ListModel {
id: myArray
Component.onCompleted: {
for (var i = 1; i < 46; i++)
append({number: i})
}
}
delegate: Rectangle {
width: parent ? parent.width - 10 : 0
height: parent ? 25 : 0
color: "transparent"
border.color: "cyan"
anchors.horizontalCenter: parent ? parent.horizontalCenter : undefined
Text {
text: number
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
anchors.fill: parent
}
MouseArea {
anchors.fill: parent
onClicked: {
myListView.currentIndex = index
}
}
}
}
}
MessageDialog {
id: messageDialog
title: qsTr("May I have your attention, please?")
function show(caption) {
messageDialog.text = caption;
messageDialog.open();
}
}
}
导入QtQuick 2.4
导入QtQuick.Controls 1.3
导入QtQuick.Window 2.2
导入QtQuick.Dialogs 1.2
应用程序窗口{
标题:qsTr(“你好世界”)
宽度:800
身高:700
可见:正确
属性int myMargin:5
菜单栏:菜单栏{
菜单{
标题:qsTr(“&文件”)
梅努伊特姆{
文本:qsTr(“打开”)
onTriggered:messageDialog.show(qsTr(“打开触发的动作”);
}
梅努伊特姆{
正文:qsTr(“E&xit”)
onTriggered:Qt.quit();
}
}
}
长方形{
id:myButton
anchors.top:parent.top
anchors.topMargin:myMargin
颜色:“黄色”
宽度:100
身高:25
半径:3
anchors.horizontalCenter:父级.horizontalCenter
正文{
正文:“明确选择”
锚定。填充:父级
水平对齐:Text.AlignHCenter
垂直对齐:Text.AlignVCenter
}
鼠耳{
锚定。填充:父级
再次点击:{
myListView.currentIndex=-1
}
}
}
长方形{
宽度:300
anchors.top:myButton.bottom
anchors.topMargin:myMargin
.bottom:parent.bottom
anchors.horizontalCenter:父级.horizontalCenter
列表视图{
id:myListView
锚定。填充:父级
当前索引:-1
间距:3
强光移动持续时间:25
突出显示:矩形{
宽度:父级?父级.width-10:0
身高:父母?25:0
颜色:“粉色”
半径:3
anchors.horizontalCenter:父对象?父对象.horizontalCenter:未定义
}
剪辑:对
模型:ListModel{
id:myArray
Component.onCompleted:{
对于(变量i=1;i<46;i++)
追加({number:i})
}
}
代表:矩形{
宽度:父级?父级.width-10:0
身高:父母?25:0
颜色:“透明”
border.color:“青色”
anchors.horizontalCenter:父对象?父对象.horizontalCenter:未定义
正文{
文本:编号
水平对齐:Text.AlignHCenter
垂直对齐:Text.AlignVCenter
锚定。填充:父级
}
鼠耳{
锚定。填充:父级
再次点击:{
myListView.currentIndex=索引
}
}
}
}
}
消息对话框{
id:messageDialog
标题:qsTr(“请注意我好吗?”)
功能展示(字幕){
messageDialog.text=标题;
messageDialog.open();
}
}
}
您能告诉我们第64行和第67行分别是什么吗?在突出显示的锚中。horizontalCenter:parent.horizontalCenter和width:parent.width-10我已经更新了代码,这确实解决了问题,感谢您的回复。但是,如果我的代理的宽度与ListView的宽度不同,则高亮显示将关闭,如我的示例中所示。我试着摆弄来获得想要的效果,但仍然不清楚。如何使高光在代理上居中?您在代理中所做的锚定似乎偏离了高光。您可以执行我在上面的delegate
中提到的重构;将根代理项设置为项
,将其设置为父项的宽度
,将其设置为高度
,将矩形
设置为该项的子项。再次感谢。我决定我不能使用horizontalCenter,只能使用锚。fill:parent-an