dynamicly access ui-elements



  • Hi,

    I have a number of label-elements in my UI which I want to access dynamicly from a foreach().
    Is there a way to do it?

    I have tried like

    int i = 0;
    foreach(const QString &str, patternSlices) {
       i++;       
       ui->step_(i)->blablablablabla();           
        }
    

    I'm searching all the time, but can't figure it out how to do that :/



  • Hi, for example, you can use findChildren like this:

        for (QLabel* l : findChildren<QLabel*>())
            qDebug() << l->text();
    


  • Hi,
    thank you for the quick answer, but I don't get it :/
    how should findChildren() get me the correct label-element?



  • Well if you have a common objectName for the labels you're interested in, you could look for it, like:

    for (QLabel* l : findChildren<QLabel*>())
        if (l->objectName().startsWith("patternSli"))
        // party on the label
    


  • Thank you :)

    it took me a while, until I finally got it working :)

    int i = 0;
        foreach(const QString &str, patternSlices) {
            i++;
            QString labelName = QString("step_%1").arg(i);
            QLabel* label = findChild<QLabel*>(labelName);
            if(label) {           
                    label->setPixmap(otherStep.scaled(100,100, Qt::KeepAspectRatio));
                    }
            }
               
         
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.