[SOLVED] UI items

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

  • Lifetime Qt Champion


    You can use findChildren and a foreach loop

    Hope it helps

  • Could you give me an example please....

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

  • 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") { ... }

  • Ok solved that too :)

Log in to reply

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