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. [SOLVED] Dynamically refer to labels
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Dynamically refer to labels

Scheduled Pinned Locked Moved General and Desktop
15 Posts 5 Posters 7.9k Views 3 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.
  • T Offline
    T Offline
    ThaRez
    wrote on last edited by
    #3

    I'm sorry, but I don't really understand your reply... I know each label has a object name, and that I can't refer to it directly, but my question was how to do it dynamically? not to have to write statically:

    @this->ui->timeLabel@

    the Qmap enables me to first push this label to the map

    @labelMap["timeLabel"] = this->ui->timeLabel;@

    and then dynamically use it:

    @void ns::changeLabelText(QString labelName, QString text){
    labelMap[labelName]->setText(text);
    }@

    But is there any way of doing this without the QMap?
    -RS

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AcerExtensa
      wrote on last edited by
      #4

      I don't really understand your problem too. Were the labels are? In the same class? Globally somewhere?
      If they are all in the same class, you can simply qobject_cast<QLabel *>(this) and find out needed label by the object name...

      God is Real unless explicitly declared as Integer.

      1 Reply Last reply
      1
      • T Offline
        T Offline
        ThaRez
        wrote on last edited by
        #5

        All the labels are in the same class, added via the Qt Designer, hence

        @this->ui->@

        I know the name of all the labels, I just want to construct one function that sets a given text to the label which name is passed as a string, my problem is how to "link the string to the correct label"?

        There are about 300 labels in this one class, and the name of the labels are stored externally, that's why I'd like to create a function that is able to refer to the label named the same as what's passed in the name string.

        -RS

        1 Reply Last reply
        0
        • A Offline
          A Offline
          AcerExtensa
          wrote on last edited by
          #6

          Then construct this function using qobject_cast or using QObject function findChild, which uses qobject-cast! Whats the problem? Have you read about qobject_cast already?

          @
          void YourMainWdiget::SetNamedLabelText(const QString & label_name, const QString & text)
          {
          QLabel * lbl = this->findChild<QLabel *>(label_name);
          if(lbl)lbl->setText(text);
          }
          @

          God is Real unless explicitly declared as Integer.

          G 1 Reply Last reply
          2
          • T Offline
            T Offline
            ThaRez
            wrote on last edited by
            #7

            Your example was precisely what I need ! Thanks a lot!
            -RS

            G 1 Reply Last reply
            0
            • T ThaRez

              Your example was precisely what I need ! Thanks a lot!
              -RS

              G Offline
              G Offline
              gaurav118
              wrote on last edited by
              #8

              @ThaRez I know this is a very old thread but mine issue is just like yours. Just like you, i too need to refer QLabel dynamically. Using QObject->findChild method makes a perfect sense. But i need to refer even the methods of that QLabel object dynamically.
              Any help from your side.

              1 Reply Last reply
              0
              • A AcerExtensa

                Then construct this function using qobject_cast or using QObject function findChild, which uses qobject-cast! Whats the problem? Have you read about qobject_cast already?

                @
                void YourMainWdiget::SetNamedLabelText(const QString & label_name, const QString & text)
                {
                QLabel * lbl = this->findChild<QLabel *>(label_name);
                if(lbl)lbl->setText(text);
                }
                @

                G Offline
                G Offline
                gaurav118
                wrote on last edited by
                #9

                @AcerExtensa can we access methods of the QLabel dynamically?

                mrjjM 1 Reply Last reply
                0
                • G gaurav118

                  @AcerExtensa can we access methods of the QLabel dynamically?

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  @gaurav118
                  Hi
                  What do you mean by dynamically ?

                  QLabel * lbl = this->findChild<QLabel *>(label_name);
                  if(lbl)lbl->setText(text); <<< this is an actual QLabel method.

                  Can you tell a bit of what you need to do ?

                  G 1 Reply Last reply
                  3
                  • mrjjM mrjj

                    @gaurav118
                    Hi
                    What do you mean by dynamically ?

                    QLabel * lbl = this->findChild<QLabel *>(label_name);
                    if(lbl)lbl->setText(text); <<< this is an actual QLabel method.

                    Can you tell a bit of what you need to do ?

                    G Offline
                    G Offline
                    gaurav118
                    wrote on last edited by
                    #11

                    @mrjj Thanks for your reply
                    // QLabel * lbl = this->findChild<QLabel *>(label_name);
                    This way we have reffered the QLabel object. So far so good.
                    but now as per your example
                    // if(lbl)lbl->setText(text); <<< this is an actual QLabel method.

                    I dont want to fix this property here. Can i call any other method the same way we refer QLabel control.

                    Typically saying somehow to use lbl.setProperty("SetText").setValue("Hi") (rubbish code just to make you more clear :-p)

                    mrjjM 1 Reply Last reply
                    0
                    • G gaurav118

                      @mrjj Thanks for your reply
                      // QLabel * lbl = this->findChild<QLabel *>(label_name);
                      This way we have reffered the QLabel object. So far so good.
                      but now as per your example
                      // if(lbl)lbl->setText(text); <<< this is an actual QLabel method.

                      I dont want to fix this property here. Can i call any other method the same way we refer QLabel control.

                      Typically saying somehow to use lbl.setProperty("SetText").setValue("Hi") (rubbish code just to make you more clear :-p)

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #12

                      @gaurav118 said in [SOLVED] Dynamically refer to labels:

                      Can i call any other method the same way we refer QLabel control.
                      Yes, you can call methods of QLabel.

                      You can also add and remove properties dynamically.
                      (see setProperty() )

                      But c++ is a compiled languages so you cannot add new methods runtime.
                      Also "SetText" would be a method.

                      You can of cause make up some tag system so you can lookup exiting members with a "tag"
                      but i fail to see why it would be helpful in any way.

                      so i guess the answer is no. You cannot just make up new function dynamically.

                      Maybe there is another solution to your goal if you take the time to describe what you
                      are trying to build/do

                      G 1 Reply Last reply
                      1
                      • mrjjM mrjj

                        @gaurav118 said in [SOLVED] Dynamically refer to labels:

                        Can i call any other method the same way we refer QLabel control.
                        Yes, you can call methods of QLabel.

                        You can also add and remove properties dynamically.
                        (see setProperty() )

                        But c++ is a compiled languages so you cannot add new methods runtime.
                        Also "SetText" would be a method.

                        You can of cause make up some tag system so you can lookup exiting members with a "tag"
                        but i fail to see why it would be helpful in any way.

                        so i guess the answer is no. You cannot just make up new function dynamically.

                        Maybe there is another solution to your goal if you take the time to describe what you
                        are trying to build/do

                        G Offline
                        G Offline
                        gaurav118
                        wrote on last edited by
                        #13

                        @mrjj You got my point exactly. I am not trying to add new methods in runtime. Obviously cannot do this.
                        I am actually looking to update the properties of QLabel outside the app and you guessed it right. I am trying to build this using Tag system.

                        Take a look,
                        <Object Name="LoginUser" Type="QLabel">
                        <Property="setAlignment" Value="Qt::AlignVCenter|Qt::AlignCenter" />
                        <Property="setMargin" Value="0" />
                        <Property="setFixedWidth" Value="180" />
                        <Property="setStyleSheet" Value="color:white" />
                        </Object>

                        I haven't used setProperty(), so just looking for some sample code snippet how to use it.
                        Hopefully by <Object/> tag could brief you what i am actually trying to do.

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #14

                          Hi,

                          It's all explained in the method documentation and the The Property System chapter of Qt's documentation.

                          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
                          2
                          • mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #15

                            Hi
                            And you might wanna take a look at
                            http://doc.qt.io/qt-5/quiloader.html
                            which basically loads a xml and create objects from it.
                            (.UI files are just xml files)
                            So depending on your needs, this might also be an option.

                            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