Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Combobox with ListModel and PyQt5
Forum Updated to NodeBB v4.3 + New Features

Combobox with ListModel and PyQt5

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
8 Posts 3 Posters 3.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bits-n-Flips
    wrote on last edited by Bits-n-Flips
    #1

    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!

    JonBJ 1 Reply Last reply
    0
    • B Bits-n-Flips

      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!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @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 ?

      B 1 Reply Last reply
      0
      • JonBJ JonB

        @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 ?

        B Offline
        B Offline
        Bits-n-Flips
        wrote on last edited by Bits-n-Flips
        #3

        @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?

        JonBJ 1 Reply Last reply
        0
        • B Offline
          B Offline
          Bits-n-Flips
          wrote on last edited by
          #4

          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?

          J.HilkJ 1 Reply Last reply
          0
          • B Bits-n-Flips

            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?

            J.HilkJ Online
            J.HilkJ Online
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @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 to true in the parent item.


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            B 1 Reply Last reply
            0
            • J.HilkJ J.Hilk

              @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 to true in the parent item.

              B Offline
              B Offline
              Bits-n-Flips
              wrote on last edited by Bits-n-Flips
              #6

              @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.

              J.HilkJ 1 Reply Last reply
              0
              • B Bits-n-Flips

                @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?

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @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 a ListView 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 with enabled: false has indeed worked for someone else, but I'm way out of depth here ... :)

                1 Reply Last reply
                0
                • B Bits-n-Flips

                  @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.

                  J.HilkJ Online
                  J.HilkJ Online
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  @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 property enabled 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


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved