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. Increment on Ui Object names
Forum Update on Monday, May 27th 2025

Increment on Ui Object names

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 1.8k 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.
  • F Offline
    F Offline
    Faruq
    wrote on 8 Jun 2018, 04:28 last edited by
    #1

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

    J 1 Reply Last reply 8 Jun 2018, 05:21
    0
    • F Faruq
      8 Jun 2018, 04:28

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

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 8 Jun 2018, 05:21 last edited by
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      F 1 Reply Last reply 8 Jun 2018, 07:28
      5
      • J jsulm
        8 Jun 2018, 05:21

        @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));
        
        F Offline
        F Offline
        Faruq
        wrote on 8 Jun 2018, 07:28 last edited by
        #3

        @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?

        Sample

         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 ?

        J 1 Reply Last reply 8 Jun 2018, 07:34
        0
        • F Faruq
          8 Jun 2018, 07:28

          @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?

          Sample

           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 ?

          J Offline
          J Offline
          JKSH
          Moderators
          wrote on 8 Jun 2018, 07:34 last edited by
          #4

          @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!

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          F 2 Replies Last reply 8 Jun 2018, 08:16
          6
          • J JKSH
            8 Jun 2018, 07:34

            @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!

            F Offline
            F Offline
            Faruq
            wrote on 8 Jun 2018, 08:16 last edited by
            #5

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

            1 Reply Last reply
            0
            • J JKSH
              8 Jun 2018, 07:34

              @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!

              F Offline
              F Offline
              Faruq
              wrote on 8 Jun 2018, 18:13 last edited by
              #6

              @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) :
                  QMainWindow(parent),
                  ui(new Ui::MainWindow)
              {
                  ui->setupUi(this);
              
                  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) 
                  
                  }
              
              
              MainWindow::~MainWindow()
              {
                  delete ui;
              }
              
              
              1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on 8 Jun 2018, 18:53 last edited by mrjj 6 Aug 2018, 18:56
                #7

                Hi

                 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

                1 Reply Last reply
                5
                • F Offline
                  F Offline
                  Faruq
                  wrote on 11 Jun 2018, 04:31 last edited by
                  #8

                  @JKSH @jsulm @mrjj

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

                  1 Reply Last reply
                  0

                  1/8

                  8 Jun 2018, 04:28

                  • Login

                  • Login or register to search.
                  1 out of 8
                  • First post
                    1/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved