Unsolved Combobox with ListModel and PyQt5
-
Hello Everyone!
So I have a bit of a question here regarding how to access and change the property of a ListModel ListElement from Python using PyQt5.
Say for example I have this snippet in QML...
ComboBox { id: combobox_001 objectName: "combobox_001" model: ListModel { id: listmodel_001 objectName: "listmodel_001" ListElement { text: "Item 1" } ListElement { text: "Item 2" } ListElement { text: "Item 3" } } }
and this in Python:
view = QQuickView() view.setSource(QUrl('sample_qml.qml')) py_window_objs = view.rootObject() py_listmodel_001= py_window_objs.findChild(QObject, "listmodel_001")
How could I individually modify combobox_001's ListElements from the python / PyQt5 side?
What I'd ultimately want to do is make some of the ListElements not enabled (e.g. enabled: false)I've been looking around and haven't quite got a solid answer.
I tried this:py_listmodel_001.setProperty(1,"enabled", "false")
But of course that returns as an error on Python's side because it sees an unexpected int "1" (typically setProperty just takes two elements, both strings, not sure if setProperty is overloaded?)
Any help would be greatly appreciated! Thank you all!
-
@Bits-n-Flips
Are you saying there is no PyQt5 definition/call for http://doc.qt.io/qt-5/qml-qtqml-models-listmodel.html#setProperty-method ? -
@JonB
From the looks of it, yes there is no function call like that in PyQt that takes three arguments. Unless I'm doing something wrong?
Has anyone else tried doing this? -
Also I guess the other question is (I suddenly noticed this)
There doesn't seem to be a way to disable a ListElement entry?I still want the ListElement to be visible to the user, but not selectable.
For example, if I do this:ListElement { enabled: false text: "Item 1" }
It does nothing in QML. Is there a way to disable certain elements but not have them deleted?
-
@Bits-n-Flips set their height to 0, that works, but if your list element has items that are independet form the parent size, they will show.
either make them than sizedependet or set
clip
totrue
in the parent item. -
@J.Hilk
Well, I don't want to hide them. I can just delete entries (or append them too), that works fine. What I'm trying to do is disable the entries; gray them out and make them unselectable by the user, I don't want to hide them because I want to show the user that in another version these entries will be usable.Also if I set the ListElement height to 0 or clip to true; nothing happens. It still renders and I can still see the entry.
-
@Bits-n-Flips said in Combobox with ListModel and PyQt5:
@JonB
From the looks of it, yes there is no function call like that in PyQt that takes three arguments. Unless I'm doing something wrong?
Has anyone else tried doing this?I use PyQt5, but no QML.
I came across https://github.com/Ultimaker/Uranium/blob/master/UM/Qt/ListModel.py. I have no idea what it is, in relation to PyQt itself, but I note that for
class ListModel
it includes:97 @pyqtSlot(int, str, QVariant) 98 def setProperty(self, index, property, value): 99 self._items[index][property] = value 100 self.dataChanged.emit(self.index(index, 0), self.index(index, 0))
so that signature does exist there.
This forum here is great. But for very specific PyQt questions/woes, go sign yourself up to the mailing list via https://riverbankcomputing.com/mailman/listinfo/pyqt, and pose your question(s). You will get an immediate response, and the author of PyQt lives there! They will surely sort you out for any PyQt problems.
I don't know about your
enabled
property. Does a ListElement even have that (I don't see it in the docs, but I don't know)? https://stackoverflow.com/questions/7674373/how-can-i-make-a-qt-quick-qml-listview-item-unselectable code shows a way of doing what I think you want for aListView
item, I don't know if that helps. The only "reference" I can find is in https://together.jolla.com/question/136609/how-to-disable-listview-or-listitem-clicks-in-qml/, where a rather cryptic/terse comment implies to me what your are trying withenabled: false
has indeed worked for someone else, but I'm way out of depth here ... :) -
@Bits-n-Flips said in Combobox with ListModel and PyQt5:
@J.Hilk
Well, I don't want to hide them. I can just delete entries (or append them too), that works fine. What I'm trying to do is disable the entries; gray them out and make them unselectable by the user, I don't want to hide them because I want to show the user that in another version these entries will be usable.Also if I set the ListElement height to 0 or clip to true; nothing happens. It still renders and I can still see the entry.
Ok I see, I wrongly interpreted your intensions than.
All QML items have the propertyenabled
its set by default to true and you can disable the Item with that. If you don't make your own Style, than the Item becomes grey andI believe all interactions are disabled