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
Qt 6.11 is out! See what's new in the release blog

How to replace QWidget name dynamically with variable

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 5 Posters 3.9k 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.
  • Q qtnoob420

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

    mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on 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
    1
    • Q qtnoob420

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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on 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
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on 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
        • mrjjM mrjj

          @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 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'
          
          jsulmJ 1 Reply Last reply
          0
          • JonBJ JonB

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

              @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'
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on 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
              0
              • jsulmJ jsulm

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

                jsulmJ 1 Reply Last reply
                0
                • Q qtnoob420

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

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 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
                  0
                  • jsulmJ jsulm

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

                    jsulmJ 1 Reply Last reply
                    0
                    • Q qtnoob420

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

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 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

                      • Login

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