Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Property with a list as possible values for custom widget?
Forum Update on Tuesday, May 27th 2025

Property with a list as possible values for custom widget?

Scheduled Pinned Locked Moved Unsolved General and Desktop
26 Posts 4 Posters 9.5k Views 1 Watching
  • 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.
  • P Offline
    P Offline
    Panoss
    wrote on 17 Jan 2017, 07:47 last edited by
    #1

    I 'm making a custom widget.
    I 'm trying to make a property that will have a list of values.
    And, in Qt Designer, I 'll be able to choose whichever I want from the list.
    So, my question is how do I make such a property?
    (that has a list, a combo box)
    Like e.g QLabel has the frameShape, or the frameShadow property.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 17 Jan 2017, 08:03 last edited by
      #2

      Hi
      I think those are just Qt enums and it should work automatically.
      Did not try it though.
      Try code from sample and see what you get.

      http://doc.qt.io/qt-5/properties.html

      class MyClass : public QObject
      {
          Q_OBJECT
          Q_PROPERTY(Priority priority READ priority WRITE setPriority NOTIFY priorityChanged)
          Q_ENUMS(Priority)
      
      public:
          MyClass(QObject *parent = 0);
          ~MyClass();
      
          enum Priority { High, Low, VeryHigh, VeryLow };
      
          void setPriority(Priority priority)
          {
              m_priority = priority;
              emit priorityChanged(priority);
          }
          Priority priority() const
          { return m_priority; }
      
      signals:
          void priorityChanged(Priority);
      
      private:
          Priority m_priority;
      };
      
      1 Reply Last reply
      3
      • V Offline
        V Offline
        VRonin
        wrote on 17 Jan 2017, 08:16 last edited by
        #3

        I agree with @mrjj , the only remark is that Q_ENUMS is now deprecated, Q_ENUM should now be used (see https://woboq.com/blog/q_enum.html for further details)

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        P 1 Reply Last reply 17 Jan 2017, 08:20
        3
        • V VRonin
          17 Jan 2017, 08:16

          I agree with @mrjj , the only remark is that Q_ENUMS is now deprecated, Q_ENUM should now be used (see https://woboq.com/blog/q_enum.html for further details)

          P Offline
          P Offline
          Panoss
          wrote on 17 Jan 2017, 08:20 last edited by
          #4

          And If I want to change the content of the list dynamically?
          The list will contain the names of the widgets that are in the same container as my custom widget.
          Let's say I add (in Qt Designer) a new widget in the container.
          Can I add the name of this widget in the list?

          M 1 Reply Last reply 17 Jan 2017, 08:51
          0
          • P Panoss
            17 Jan 2017, 08:20

            And If I want to change the content of the list dynamically?
            The list will contain the names of the widgets that are in the same container as my custom widget.
            Let's say I add (in Qt Designer) a new widget in the container.
            Can I add the name of this widget in the list?

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 17 Jan 2017, 08:51 last edited by mrjj
            #5

            @Panoss
            Hi ahh yes. That type of list.
            Well you can just use a QList or QStringList
            Make sure to define both Read and Write

            Q_PROPERTY(QList<int> points READ getpoints WRITE putpoints )
            (list of ints)

            1 Reply Last reply
            1
            • P Offline
              P Offline
              Panoss
              wrote on 17 Jan 2017, 10:02 last edited by Panoss
              #6

              I forgot to mention I 'm working in python, so I'm trying to 'translate' your code in python.
              I 'll call my property 'ContainersWidgets'.
              So, a 'translation' of the above code by mrJJ would be something like:

              ContainersWidgets = QtCore.pyqtProperty(QList, getContainersWidgets , setContainersWidgets )
              

              Where should '(list of ints)' go?

              M 1 Reply Last reply 17 Jan 2017, 10:04
              0
              • P Panoss
                17 Jan 2017, 10:02

                I forgot to mention I 'm working in python, so I'm trying to 'translate' your code in python.
                I 'll call my property 'ContainersWidgets'.
                So, a 'translation' of the above code by mrJJ would be something like:

                ContainersWidgets = QtCore.pyqtProperty(QList, getContainersWidgets , setContainersWidgets )
                

                Where should '(list of ints)' go?

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 17 Jan 2017, 10:04 last edited by mrjj
                #7

                @Panoss
                Im 110% python noob but it seems yes.
                https://wiki.qt.io/Using_Qt_Properties_in_PySide

                from PySide.QtCore import QObject, Property
                 
                class MyObject(QObject):
                 def ''init''(self,startval=42):
                 QObject.''init''(self)
                 self.ppval = startval
                 
                def readPP(self):
                 return self.ppval
                 
                def setPP(self,val):
                 self.ppval = val
                 
                pp = Property(int, readPP, setPP)
                 
                obj = MyObject()
                obj.pp = 47
                print (obj.pp)
                
                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  Panoss
                  wrote on 17 Jan 2017, 10:23 last edited by Panoss
                  #8

                  I use QtGui.QListWidget (the Qt Designer 's QListWidget )

                  ContainersWidgets = QtCore.pyqtProperty(QtGui.QListWidget, getContainersWidgets, setContainersWidgets)
                  

                  But how do I set the value?

                  @QtCore.pyqtSlot(int)
                      def setContainersWidgets(self, value):
                          self._containers_widgets = value
                  

                  So the variable self._containers_widgets will get the value.
                  How shall I put this value as selected item from the list?

                  First of all, how shall I refer to the list?(I mean the widget, the QtGui.QListWidget)
                  list=??

                  M 1 Reply Last reply 17 Jan 2017, 10:34
                  0
                  • P Panoss
                    17 Jan 2017, 10:23

                    I use QtGui.QListWidget (the Qt Designer 's QListWidget )

                    ContainersWidgets = QtCore.pyqtProperty(QtGui.QListWidget, getContainersWidgets, setContainersWidgets)
                    

                    But how do I set the value?

                    @QtCore.pyqtSlot(int)
                        def setContainersWidgets(self, value):
                            self._containers_widgets = value
                    

                    So the variable self._containers_widgets will get the value.
                    How shall I put this value as selected item from the list?

                    First of all, how shall I refer to the list?(I mean the widget, the QtGui.QListWidget)
                    list=??

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 17 Jan 2017, 10:34 last edited by
                    #9

                    @Panoss

                    Hi
                    Sadly I dont know enough python to know.

                    http://pyqt.sourceforge.net/Docs/PyQt4/qstringlist.html#details

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      Panoss
                      wrote on 17 Jan 2017, 10:41 last edited by Panoss
                      #10

                      Don't worry my friend, 'll find out the way, sooner or later.
                      I 'm not sure it is correct that I 'm using Qt Designer 's QListWidget in the place of 'QList<int>' you wrote above.
                      Or maybe I should use QStringList?
                      The QList you 're referring to, is a widget?

                      I think I should use QStringList in place of your QList.

                      M 1 Reply Last reply 17 Jan 2017, 10:52
                      0
                      • P Panoss
                        17 Jan 2017, 10:41

                        Don't worry my friend, 'll find out the way, sooner or later.
                        I 'm not sure it is correct that I 'm using Qt Designer 's QListWidget in the place of 'QList<int>' you wrote above.
                        Or maybe I should use QStringList?
                        The QList you 're referring to, is a widget?

                        I think I should use QStringList in place of your QList.

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 17 Jan 2017, 10:52 last edited by
                        #11

                        @Panoss

                        Hi
                        QListWidget is a widget ( listbox) so that is not well for properties.

                        • I think I should use QStringList in place of your QList.
                          I agree since QList is template based it might be missing as QList.
                          But QStringList is
                          class QStringList : public QList<QString>
                          So very alike.

                        Maybe this can be used
                        http://www.programcreek.com/python/example/63681/PyQt4.QtCore.QStringList

                        1 Reply Last reply
                        1
                        • P Offline
                          P Offline
                          Panoss
                          wrote on 17 Jan 2017, 11:03 last edited by
                          #12

                          Even though referred in the examples you posted, PyQt4.QtCore.QStringList doesn't exist!!

                          M 1 Reply Last reply 17 Jan 2017, 11:17
                          0
                          • P Panoss
                            17 Jan 2017, 11:03

                            Even though referred in the examples you posted, PyQt4.QtCore.QStringList doesn't exist!!

                            M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 17 Jan 2017, 11:17 last edited by mrjj
                            #13

                            @Panoss
                            And you have something like
                            from PyQt4.QtCore import QStringList
                            or similar import ?

                            1 Reply Last reply
                            0
                            • P Offline
                              P Offline
                              Panoss
                              wrote on 17 Jan 2017, 11:22 last edited by Panoss
                              #14

                              I find nothing similar to QStringList in PyQt4.QtCore.
                              That's quite strange, either it has been moved...or I have no idea! :)

                              1 Reply Last reply
                              0
                              • P Offline
                                P Offline
                                Panoss
                                wrote on 17 Jan 2017, 12:11 last edited by Panoss
                                #15

                                From here:

                                From PyQt4 4.6+ in Python3 QString doesn't exist and you are supposed to use ordinary Python3 unicode objects (string literals). To do this so that your code will work in both Python 2.x AND Python 3.x you can do following:

                                try:
                                    from PyQt4.QtCore import QString
                                except ImportError:
                                    # we are using Python3 so QString is not defined
                                    QString = type("")
                                

                                Ok, this for QString.

                                From here:
                                The QList class is a template class that provides lists. available in C++ only. But in python, it have python list data-type ready to used. If you want to use behavior like list you can use python list similar QList

                                So how will I do it? QList = []?

                                (with '[]' we define a list in python)

                                1 Reply Last reply
                                1
                                • P Offline
                                  P Offline
                                  Panoss
                                  wrote on 17 Jan 2017, 12:31 last edited by
                                  #16

                                  I tried this:

                                  QList = []
                                  
                                  ContainersWidgets = QtCore.pyqtProperty(QList, getContainersWidgets, setContainersWidgets)
                                  

                                  But I get an error:
                                  "ContainersWidgets = QtCore.pyqtProperty(QList, getContainersWidgets, setContainersWidgets)
                                  TypeError: bytes or ASCII string expected not 'list'"

                                  It doesn't accept a List, only bytes or ASCII.

                                  M jsulmJ 2 Replies Last reply 17 Jan 2017, 12:37
                                  0
                                  • P Panoss
                                    17 Jan 2017, 12:31

                                    I tried this:

                                    QList = []
                                    
                                    ContainersWidgets = QtCore.pyqtProperty(QList, getContainersWidgets, setContainersWidgets)
                                    

                                    But I get an error:
                                    "ContainersWidgets = QtCore.pyqtProperty(QList, getContainersWidgets, setContainersWidgets)
                                    TypeError: bytes or ASCII string expected not 'list'"

                                    It doesn't accept a List, only bytes or ASCII.

                                    M Offline
                                    M Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on 17 Jan 2017, 12:37 last edited by
                                    #17

                                    I think you need a python head for this :)
                                    Are you using
                                    https://pypi.python.org/pypi/PyQt5

                                    or what version ?

                                    1 Reply Last reply
                                    0
                                    • P Offline
                                      P Offline
                                      Panoss
                                      wrote on 17 Jan 2017, 12:40 last edited by
                                      #18

                                      The previous version, PyQt4.

                                      M 1 Reply Last reply 17 Jan 2017, 12:41
                                      0
                                      • P Panoss
                                        17 Jan 2017, 12:40

                                        The previous version, PyQt4.

                                        M Offline
                                        M Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on 17 Jan 2017, 12:41 last edited by
                                        #19

                                        @Panoss said in Property with a list as possible values for custom widget?:

                                        PyQt4

                                        ok it really should have the qstringlist

                                        http://pyqt.sourceforge.net/Docs/PyQt4/qstringlist.html

                                        1 Reply Last reply
                                        0
                                        • P Offline
                                          P Offline
                                          Panoss
                                          wrote on 17 Jan 2017, 12:43 last edited by
                                          #20

                                          Yes it should, but it doesn't :) !!!

                                          M 1 Reply Last reply 17 Jan 2017, 12:46
                                          0

                                          1/26

                                          17 Jan 2017, 07:47

                                          • Login

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