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] UI items
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] UI items

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.1k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hey, I have a lot of buttons in my application, so I am wondering if it is possible to execute functions with "for", rather than write code for each button...

    For example:
    @int number = 0;
    QString string = "Name" + QString::number(number); // Easy to change string@

    Can I also do that with ui items?
    For example:
    @ui->button5->text(); //I want to achieve this

    int index = 0;
    ui->button + index->text()@

    Since I have alot of buttons I could execute functions with for...

    @
    // Lets assume I have these buttons...
    // ui->button1
    // ui->button2
    // ui->button3

    for(int i = 0; i < number of buttons, i++)
    {
    ui->button + i->setText("abc");
    }@

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

      Hi,

      You can use findChildren and a foreach loop

      Hope it helps

      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
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Could you give me an example please....

        EDIT: Never mind I found it already... Thank you

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          Hey, thats the result...

          @QList<QPushButton *> allPButtons = this->findChildren<QPushButton *>();@

          bq. (QPushButton(0xcd6a00, name = "pushButton") , QPushButton(0xcd64a8, name = "pushButton_2")

          After I get children, can I use just some of them?

          for example with if statement? If (allPButtons.name == "pushButton_2") { ... }

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            Ok solved that too :)

            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