Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. dynamicly access ui-elements
Forum Updated to NodeBB v4.3 + New Features

dynamicly access ui-elements

Scheduled Pinned Locked Moved Unsolved Language Bindings
5 Posts 2 Posters 1.2k Views
  • 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.
  • AdmiralCrunchA Offline
    AdmiralCrunchA Offline
    AdmiralCrunch
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • hskoglundH Online
      hskoglundH Online
      hskoglund
      wrote on last edited by
      #2

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

          for (QLabel* l : findChildren<QLabel*>())
              qDebug() << l->text();
      
      1 Reply Last reply
      0
      • AdmiralCrunchA Offline
        AdmiralCrunchA Offline
        AdmiralCrunch
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • hskoglundH Online
          hskoglundH Online
          hskoglund
          wrote on last edited by
          #4

          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
          
          1 Reply Last reply
          1
          • AdmiralCrunchA Offline
            AdmiralCrunchA Offline
            AdmiralCrunch
            wrote on last edited by
            #5

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