Nominate our 2022 Qt Champions!

Increment on Ui Object names

  • Hi everyone, for today I would like to seek help to find an efficient way to name my Ui RadioButton objectname in an ascending order.

    I planned to store these lots of RadioButtons objectname into an Array. Thus, I searched online but it doesn't help much eventhough there are some topics on FindChild(). I tried using QString increment but it would also end with an error depicting that QRadioButton doesn't match with QString.

    E.g. QRadioButton* Storage[] = { ui.radioButton_1, ui.radioButton_2, ui.radioButton_3, ui.radioButton_4..... }
    // this is manual code and may be inefficient when facing with tons of other radioButton.

    What I tried is

    QRadioButton* entries;
    for (int i = 0; i< 50; i++){
         QString test1 = "ui.radioButton_ ";
         entries[i] = { test1.append(i) };

    Thus, how do I exactly increment these names ui.radioButton_XXX where XXX is the increment-ing number and store it under an array?

    Thank you and looking forward for replies :)

  • Lifetime Qt Champion

    @Faruq I don't really understand what you want to do. The code you posted is wrong in several ways.
    Do you want to create variable names? This is not possible at all at runtime. Variable names are defined in source code.
    If you just want to store pointers to your radio buttons then simply do:

    QVector<QRadioButton*> radioButtons;
    for (int i = 0; i < 50; ++i)
        radioButtons.append(new QRadioButton(this));

  • @jsulm Thank you for your reply. so what it means is that I must manually list down all the Ui Objects into the array such as this?


     QRadioButton* Storage[] =	{ ui.radioButton_1, ui.radioButton_2, ui.radioButton_3, ui.radioButton_4,  ui.radioButton_5, ui.radioButton_6, ui.radioButton_7, ui.radioButton_8, ui.radioButton_9, ui.radioButton_10, ui.radioButton_11, ui.radioButton_12 }
    //it can be more. So I must literally typed in everything? 

    There is no concise method to cut short on the manual labeling ?

  • Moderators

    @Faruq said in Increment on Ui Object names:

    //it can be more. So I must literally typed in everything?

    If you have so many, you should dynamically create the radio buttons.

    Don't use Qt Designer to add so many buttons by hand. That takes a long time too.

    QVector<QRadioButton*> radioButtons;
    for (int i = 0; i < 50; ++i) {
        QString label = "Option_" + QString::number(i);
        auto button = new QRadioButton(label);
        layout->addWidget(button); // This is your widget's QLayout
        radioButtons << button;

    Also, please use QVector or std::vector. Don't use a raw C array!

  • @JKSH Alright thank you for your reply. Will work on it! :)

  • @JKSH Hi, I tried working on it but as I run it, it doesnt display any QPushButton at all. I have added a verticalLayout in the Ui. Am I missing a step since it result in an empty output?

    I also tried to check the content of radioButtons and realised that qDebug-ing it will result in its address instead of the object name (E.g. Object_3) . How can I check or replace it with the supposed object name?

    testing.cpp . Header file is a clean default .h

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "QRadioButton"
    #include "QVector"
    #include "QListWidget"
    #include "QGridLayout"
    #include "QDebug"
    MainWindow::MainWindow(QWidget *parent) :
        ui(new Ui::MainWindow)
        QVector<QRadioButton*> radioButtons;
        for (int i = 0; i < 50; ++i) {
            QString label = "Option_" + QString::number(i);
            auto button = new QRadioButton(label);
            ui->verticalLayout->addWidget(button);    // I added verticalLayout in the ui. 
            radioButtons << button;                                  // I believe this is the container/arrays tracking. 
        qDebug() << &radioButtons[2];   //shows 0x1ee15568. Why doesn't it reflect object name? E.g. Option_3
        qDebug() << radioButtons[2];    //shows QRadioButton(0x1ee090c0) 
        delete ui;

  • Lifetime Qt Champion


     qDebug() << radioButtons[2];    //shows QRadioButton(0x1ee090c0) 

    outputs the pointer stored in the radioButtons lists.

    To show its name would be

     qDebug() << radioButtons[2]->objectName();

    I first now understand what you ask in first post.
    it is possible to find object via

      QList<QRadioButton*> RBList = this->findChildren<QRadioButton*>();
      foreach (auto rb, RBList) {
        qDebug() << rb->objectName();

    But as JKSH says, its most likely better to generate them at runtime than to actually insert that many into UI files
    with Designer.

    Also it should show. i added layout and ran code.
    alt text

  • @JKSH @jsulm @mrjj

    Thank you everyone, I studied each of the solution and learnt something from it. Really appreciate it.

Log in to reply