Qt World Summit: Submit your Presentation

[SOLVED] Dynamically refer to labels

  • Hello
    How can I replace the following line with a dynamic reference:


    so that the label name is passed as a QString? I'm looking for something comparable to the following pseudo:

    @void ns::setText(QString labelName, QString text){

    Is this possible? (One way of course is to add the labels to a QMap<QString, QLabel*> with the same key as the label name, but this is a bit clumsy if there are a lot of labels...)

  • All labels have ObjectName parameter, so you don't need QMap for that.
    Also you can check "QSignalMapper":http://qt-project.org/doc/qt-5.0/qsignalmapper.html

  • 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:


    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){

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

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

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


    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.


  • 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);

  • Your example was precisely what I need ! Thanks a lot!

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

  • @AcerExtensa can we access methods of the QLabel dynamically?

  • Lifetime Qt Champion

    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 ?

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

  • Lifetime Qt Champion

    @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

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

    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.

  • Lifetime Qt Champion


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

  • Lifetime Qt Champion

    And you might wanna take a look at
    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.

Log in to reply