Solved Why can't the contentData property of the menu be used in initialization?
-
@J-Hilk sorry,I mean that clicking the mouse button triggers the listView to slide left or right effect.
Does the ListView provide relevant signals or methods? -
@mirro nothing specific only stuff all QML items have like for example:
import QtQuick 2.12 import QtQuick.Window 2.0 import QtQuick.Controls 2.12 import Qt.labs.settings 1.0 Window { id: window width: 300 height: 300 visible: true Button{ id:switchModel height: 50 width: 300 text: "switch" onClicked: { leftSide = !leftSide } } property bool leftSide: true ListView{ id:lView width: parent.width / 2 height: 250 y:50 x: leftSide ? 0 : window.width - width Behavior on x { NumberAnimation{duration:500} } model: 20 delegate: Rectangle{ width: lView.width height: 50 color: "green" Text{ text: modelData } } } }
-
@J-Hilk Thank you very much.One last question.
Is menuItem.highlighted available in QT5.9.7? The test program has no effect.Menu { id: menu widh:100 height:100 MenuItem { id:menuItem width:100 height:40 text: "New..." background:Rectangle{ color:menuItem.highlighted?'black':'red' } } }
-
@mirro take a look at the documentation:
https://doc.qt.io/qt-5/qtquickcontrols2-customize.html#customizing-menufor more information about that.
Since MenuItem inherits from Control, yes it has a background property since the very beginning :D, version independent
same for highlighted, its a thing since MenuItem was introduced in 5.7 -
Well, I don't know, but menuItem.highlighted is also false when I set hoverEnable:true
Menu { id: menu widh:100 height:100 MenuItem { id:menuItem hoverEnable:true width:100 height:40 text: "New..." background:Rectangle{ color:menuItem.highlighted?'black':'red' } } }
-
@mirro for once there are 2 typos in your code
widh:100 -> width:100
and
hoverEnable:true -> hoverEnabled:true -
@J-Hilk
Sorry, my work machine has no network, so I can't copy the code correctly.
The program I tested had no syntax errors, but menuItem.highlighted was always false, I don't know why.Menu { id: menu width:100 height:100 MenuItem { id:menuItem hoverEnabled:true width:100 height:40 text: "New..." background:Rectangle{ color:menuItem.highlighted?'black':'red' } } }
-
@mirro works fine for me:
import QtQuick 2.12 import QtQuick.Controls 2.12 import QtQuick.Window 2.2 ApplicationWindow { visible: true width: 640 height: 200 title: qsTr("Hello World") id:root Component.onCompleted: menu.open() Menu{ id:menu width: 640 height: 100 MenuItem{ id:menuItem2 width: 100 height: 40 text:"New..." hoverEnabled:true background:Rectangle{ implicitWidth: 100 implicitHeight: 40 color: menuItem2.highlighted ? "black" : "red" } } MenuItem{ id:menuItem1 width: 100 height: 40 text:"Delete..." hoverEnabled:true background:Rectangle{ implicitWidth: 100 implicitHeight: 40 color: menuItem1.highlighted ? "black" : "red" } } } }
but I'm on 5.12.6 🤷♂️ I do not have any other version installed
-
highlighted attribute may be need to maintain yourself in QT5.9.7
-
@mirro have you tried my fully working example code?
-
My working environment does not have Internet. I have tested it with similar code.
I maintaining this highlighted attribute in the background: Rectangle: MouseArea {} in Qt5.9.7 now.