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. How to replace QWidget name dynamically with variable
Forum Updated to NodeBB v4.3 + New Features

How to replace QWidget name dynamically with variable

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 5 Posters 2.6k Views 2 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.
  • M mrjj
    28 Jul 2021, 09:33

    Hi
    Cant you just put
    self.dlg.name1
    self.dlg.name2
    etc
    into a list ?

    You can also build this list automatically from DLG
    https://doc.qt.io/qt-5/qobject.html#findChildren

    Q Offline
    Q Offline
    qtnoob420
    wrote on 28 Jul 2021, 10:09 last edited by
    #3

    @mrjj my thought is to get relevant fields that i need to work with from QGIS and then adress my QLineEdits, QCheckboxes, etc with that.

    For every of my input field i have a label too. wouldnt they be found too with "findchildren"?

    J 1 Reply Last reply 28 Jul 2021, 10:13
    0
    • Q qtnoob420
      28 Jul 2021, 10:09

      @mrjj my thought is to get relevant fields that i need to work with from QGIS and then adress my QLineEdits, QCheckboxes, etc with that.

      For every of my input field i have a label too. wouldnt they be found too with "findchildren"?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 28 Jul 2021, 10:13 last edited by
      #4

      @qtnoob420 If you want to access your widgets using a string then you could simply put your widgets in a dictionary. This would be way faster than using findChildren.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      Q 2 Replies Last reply 28 Jul 2021, 10:55
      1
      • J jsulm
        28 Jul 2021, 10:13

        @qtnoob420 If you want to access your widgets using a string then you could simply put your widgets in a dictionary. This would be way faster than using findChildren.

        Q Offline
        Q Offline
        qtnoob420
        wrote on 28 Jul 2021, 10:55 last edited by
        #5

        @jsulm it's not that i explicitly want to do it that way, but that I don't know any other way. I don't have the experience and the knowledge i guess

        1 Reply Last reply
        0
        • J jsulm
          28 Jul 2021, 10:13

          @qtnoob420 If you want to access your widgets using a string then you could simply put your widgets in a dictionary. This would be way faster than using findChildren.

          Q Offline
          Q Offline
          qtnoob420
          wrote on 28 Jul 2021, 11:14 last edited by
          #6

          @jsulm can you provide me any help on how to do that?

          M J 2 Replies Last reply 28 Jul 2021, 11:44
          0
          • Q qtnoob420
            28 Jul 2021, 11:14

            @jsulm can you provide me any help on how to do that?

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 28 Jul 2021, 11:44 last edited by
            #7

            @qtnoob420
            Hi

            Something like

            a_dict = dict()
            a_dict["name"] = self.dlg.name1
            a_dict["name3"] = self.dlg.name2

            and you can look up the self.dlg.name1 widget with "name"

            "name" is the actual name of the widget. (objectname)

            Q 1 Reply Last reply 29 Jul 2021, 05:24
            1
            • Q qtnoob420
              28 Jul 2021, 11:14

              @jsulm can you provide me any help on how to do that?

              J Offline
              J Offline
              JonB
              wrote on 28 Jul 2021, 11:48 last edited by JonB
              #8

              @qtnoob420
              Python has a dict type, to store an arbitrary set of key-value pairs.

              If you want to do as @jsulm said and "access your widgets using a string" you could do something like:

              widgets = dict()
              widgets["name1"] = self.dlg.name1
              widgets["name2"] = self.dlg.name2
              ...
              

              You can then access, say, self.dlg.name2 via widgets["name2"]. You can also iterate through all of them, keys and values, via

              for key in widgets:
                   print(key, '->', widgets[key])
              

              If you don't want key names you can of course do similar by just storing them in a list/array instead of a dictionary.

              Q 1 Reply Last reply 29 Jul 2021, 05:26
              2
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 28 Jul 2021, 18:40 last edited by
                #9

                Hi,

                There might be some stuff doable with some Python introspection or even just using QObject's objectName method.

                Can you show the code of the class of your self.dlg variable ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • M mrjj
                  28 Jul 2021, 11:44

                  @qtnoob420
                  Hi

                  Something like

                  a_dict = dict()
                  a_dict["name"] = self.dlg.name1
                  a_dict["name3"] = self.dlg.name2

                  and you can look up the self.dlg.name1 widget with "name"

                  "name" is the actual name of the widget. (objectname)

                  Q Offline
                  Q Offline
                  qtnoob420
                  wrote on 29 Jul 2021, 05:24 last edited by
                  #10

                  @mrjj okay i got this so far.
                  now i want to set values of my fields like this...

                  a_dict["name"].setText("some text")
                  

                  but i get the error:

                  a_dict["name"].setText("some text")
                  AttributeError: 'str' object has no attribute 'setText'
                  
                  J 1 Reply Last reply 29 Jul 2021, 05:26
                  0
                  • J JonB
                    28 Jul 2021, 11:48

                    @qtnoob420
                    Python has a dict type, to store an arbitrary set of key-value pairs.

                    If you want to do as @jsulm said and "access your widgets using a string" you could do something like:

                    widgets = dict()
                    widgets["name1"] = self.dlg.name1
                    widgets["name2"] = self.dlg.name2
                    ...
                    

                    You can then access, say, self.dlg.name2 via widgets["name2"]. You can also iterate through all of them, keys and values, via

                    for key in widgets:
                         print(key, '->', widgets[key])
                    

                    If you don't want key names you can of course do similar by just storing them in a list/array instead of a dictionary.

                    Q Offline
                    Q Offline
                    qtnoob420
                    wrote on 29 Jul 2021, 05:26 last edited by
                    #11

                    @JonB i am still not able to address them like i imagine to.
                    if i try to set values of my field like:

                    a_dict["name"].setText("some text")
                    

                    i get the error:

                    AttributeError: 'str' object has no attribute 'setText'
                    
                    1 Reply Last reply
                    0
                    • Q qtnoob420
                      29 Jul 2021, 05:24

                      @mrjj okay i got this so far.
                      now i want to set values of my fields like this...

                      a_dict["name"].setText("some text")
                      

                      but i get the error:

                      a_dict["name"].setText("some text")
                      AttributeError: 'str' object has no attribute 'setText'
                      
                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 29 Jul 2021, 05:26 last edited by
                      #12

                      @qtnoob420 said in How to replace QWidget name dynamically with variable:

                      a_dict["name"]

                      What did you store there? Can you show how (and what) you set it (a_dict["name"]=...).
                      What does type(a_dict["name"]) print out?
                      It seems you put a string there...

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      Q 1 Reply Last reply 29 Jul 2021, 05:37
                      0
                      • J jsulm
                        29 Jul 2021, 05:26

                        @qtnoob420 said in How to replace QWidget name dynamically with variable:

                        a_dict["name"]

                        What did you store there? Can you show how (and what) you set it (a_dict["name"]=...).
                        What does type(a_dict["name"]) print out?
                        It seems you put a string there...

                        Q Offline
                        Q Offline
                        qtnoob420
                        wrote on 29 Jul 2021, 05:37 last edited by
                        #13

                        @jsulm oh youre right. but idk how i could assign them in an other way:

                        i assigned them like:

                        a_dict[attribute] = "self.dlg."+attribute
                        

                        attribute is the name of the attributes from QGIS. and i named my QWidgets in a similar way to match them.

                        J 1 Reply Last reply 29 Jul 2021, 05:41
                        0
                        • Q qtnoob420
                          29 Jul 2021, 05:37

                          @jsulm oh youre right. but idk how i could assign them in an other way:

                          i assigned them like:

                          a_dict[attribute] = "self.dlg."+attribute
                          

                          attribute is the name of the attributes from QGIS. and i named my QWidgets in a similar way to match them.

                          J Offline
                          J Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 29 Jul 2021, 05:41 last edited by jsulm
                          #14

                          @qtnoob420 Well, you are assigning a string. So, how should that work?
                          Why don't you assign self.dlg.attribute? Where attribute is the name of the attribute.
                          Also, what type is self.dlg?

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          Q 1 Reply Last reply 29 Jul 2021, 05:50
                          0
                          • J jsulm
                            29 Jul 2021, 05:41

                            @qtnoob420 Well, you are assigning a string. So, how should that work?
                            Why don't you assign self.dlg.attribute? Where attribute is the name of the attribute.
                            Also, what type is self.dlg?

                            Q Offline
                            Q Offline
                            qtnoob420
                            wrote on 29 Jul 2021, 05:50 last edited by
                            #15

                            @jsulm it makes completely sense, i just dont know how :(

                            i tried stuff like...

                            a_dict[attribute] = self.dlg.attribute
                            AttributeError: 'BaumkatasterDialog' object has no attribute 'attribute'
                            
                            
                            feature_dict[attribute] = self.dlg.+attribute
                            SyntaxError: invalid syntax
                            
                            

                            ... which doesnt work either. i just dont know no better

                            i used the PluginBuilder3 so it was created by the plugin. i can access all of my UI Widgets from the QtDesigner with "self.dlg.widget_name"

                            J 1 Reply Last reply 29 Jul 2021, 05:58
                            0
                            • Q qtnoob420
                              29 Jul 2021, 05:50

                              @jsulm it makes completely sense, i just dont know how :(

                              i tried stuff like...

                              a_dict[attribute] = self.dlg.attribute
                              AttributeError: 'BaumkatasterDialog' object has no attribute 'attribute'
                              
                              
                              feature_dict[attribute] = self.dlg.+attribute
                              SyntaxError: invalid syntax
                              
                              

                              ... which doesnt work either. i just dont know no better

                              i used the PluginBuilder3 so it was created by the plugin. i can access all of my UI Widgets from the QtDesigner with "self.dlg.widget_name"

                              J Offline
                              J Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on 29 Jul 2021, 05:58 last edited by
                              #16

                              @qtnoob420 As I said: use real name of the attribute, not the word "attribute". Example: if you have attribute called "a" then do:

                              a_dict[attribute] = self.dlg.a
                              

                              But it seems that you only have strings containing attribute names, right?
                              Then you could do it like this:

                              f = getattr(self.dlg, attribute)
                              f() # Here we call attribute
                              

                              You do not even need a dictionary.
                              One example for string in Python:

                              s="abc"
                              f=getattr(s, "upper")
                              f() # prints ABC
                              

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              1

                              12/16

                              29 Jul 2021, 05:26

                              • Login

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