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. How to get the status of radio buttons from a radio button group which is inside a QTableWidget?
Forum Update on Monday, May 27th 2025

How to get the status of radio buttons from a radio button group which is inside a QTableWidget?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.3k 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.
  • Q Offline
    Q Offline
    qt_is_not_easy
    wrote on last edited by
    #1

    Hello all, I am very new to Qt and I just built this small little gui with radio buttons in QButtonGroup in each row of a QTableWidget. I wanted a dynamic GUI so I have a button to add rows dynamically to table (which wasn't a big deal). I also have a QComboBox at the 0th column in QTableWidget and I am able to know it's text using

        for(int t_row = 0;t_row < ui->tableWidget->rowCount(); t_row++)
        {
                QComboBox *myCB = qobject_cast<QComboBox*>(ui->tableWidget->cellWidget(t_row,0));
                qDebug() << myCB->currentText();
                ui->textEdit->insertPlainText(myCB->currentText() + "\n");
        }
    

    However, I am not able to get the status of radio buttons and after spending sometime I do see that it is doable by using checkedId or checkedButton(), but I am not yet confident how to do so. Could anyone please let me know how would I do that here?

    Also, I wanted to know if it is possible for me to use models and how much easier it is to learn as that is something that many of the threads mentioned but I haven't yet been able to understand that.
    Thanks in advance!

    I am giving here the minimal code that I can to illustrate my problem and also I am attaching an image of the GUI.
    .cpp code

    #include "dynamic_gui.h"
    #include "ui_dynamic_gui.h"
    
    dynamic_gui::dynamic_gui(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::dynamic_gui)
    {
        ui->setupUi(this);
        connect(ui->pushButton_cancel, SIGNAL(clicked()), this, SLOT(close()));
    }
    
    dynamic_gui::~dynamic_gui()
    {
        delete ui;
    }
    
    
    void dynamic_gui::on_pushButton_clicked()
    {
        ui->tableWidget->insertRow(ui->tableWidget->rowCount());
        rButton_b1 = new QRadioButton(ui->tableWidget);
        rButton_b2 = new QRadioButton(ui->tableWidget);
        rButton_b3 = new QRadioButton(ui->tableWidget);
    
        ui->tableWidget->setCellWidget(ui->tableWidget->rowCount()-1,1,rButton_b1);
        ui->tableWidget->setCellWidget(ui->tableWidget->rowCount()-1,2,rButton_b2);
        ui->tableWidget->setCellWidget(ui->tableWidget->rowCount()-1,3,rButton_b3);
    
        combo = new QComboBox();
        combo->addItems({"t1", "t2", "t3", "t4","Delete"});
        connect(combo, SIGNAL(currentIndexChanged(QString)),this,SLOT(on_currentIndexChanged(QString)));
        ui->tableWidget->setCellWidget(ui->tableWidget->rowCount()-1,0,combo);
        g1 = new QButtonGroup;
        g1->addButton(rButton_b1, 1);
        g1->addButton(rButton_b2, 2);
        g1->addButton(rButton_b3, 3);
    }
    void dynamic_gui::on_currentIndexChanged(QString a)
    {
        if(a=="Delete")
        {
            ui->tableWidget->removeRow(ui->tableWidget->currentRow());
        }
    }
    
    
    void dynamic_gui::on_pushButton_ok_clicked()
    {
        // read only the qcombox values in 0th column
        for(int t_row = 0;t_row < ui->tableWidget->rowCount(); t_row++)
        {
                QComboBox *myCB = qobject_cast<QComboBox*>(ui->tableWidget->cellWidget(t_row,0));
                qDebug() << myCB->currentText();
                ui->textEdit->insertPlainText(myCB->currentText() + "\n");
        }
    
        // read which radio buttons are checked in 2nd row
    //    for(int t_col = 0;t_col < ui->tableWidget->columnCount(); t_col++)
    //    {
    //            QButtonGroup *gk=qobject_cast<QButtonGroup*>(ui->tableWidget->cellWidget(2,t_col));
    //            qDebug() << gk->checkedButton();
    
    //            QRadioButton *rk=qobject_cast<QRadioButton*>(ui->tableWidget->cellWidget(2,t_col));
    //            qDebug() << rk->isChecked();
    //    }
    }
    

    111.PNG

    jsulmJ JonBJ 2 Replies Last reply
    0
    • Q qt_is_not_easy

      Hello all, I am very new to Qt and I just built this small little gui with radio buttons in QButtonGroup in each row of a QTableWidget. I wanted a dynamic GUI so I have a button to add rows dynamically to table (which wasn't a big deal). I also have a QComboBox at the 0th column in QTableWidget and I am able to know it's text using

          for(int t_row = 0;t_row < ui->tableWidget->rowCount(); t_row++)
          {
                  QComboBox *myCB = qobject_cast<QComboBox*>(ui->tableWidget->cellWidget(t_row,0));
                  qDebug() << myCB->currentText();
                  ui->textEdit->insertPlainText(myCB->currentText() + "\n");
          }
      

      However, I am not able to get the status of radio buttons and after spending sometime I do see that it is doable by using checkedId or checkedButton(), but I am not yet confident how to do so. Could anyone please let me know how would I do that here?

      Also, I wanted to know if it is possible for me to use models and how much easier it is to learn as that is something that many of the threads mentioned but I haven't yet been able to understand that.
      Thanks in advance!

      I am giving here the minimal code that I can to illustrate my problem and also I am attaching an image of the GUI.
      .cpp code

      #include "dynamic_gui.h"
      #include "ui_dynamic_gui.h"
      
      dynamic_gui::dynamic_gui(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::dynamic_gui)
      {
          ui->setupUi(this);
          connect(ui->pushButton_cancel, SIGNAL(clicked()), this, SLOT(close()));
      }
      
      dynamic_gui::~dynamic_gui()
      {
          delete ui;
      }
      
      
      void dynamic_gui::on_pushButton_clicked()
      {
          ui->tableWidget->insertRow(ui->tableWidget->rowCount());
          rButton_b1 = new QRadioButton(ui->tableWidget);
          rButton_b2 = new QRadioButton(ui->tableWidget);
          rButton_b3 = new QRadioButton(ui->tableWidget);
      
          ui->tableWidget->setCellWidget(ui->tableWidget->rowCount()-1,1,rButton_b1);
          ui->tableWidget->setCellWidget(ui->tableWidget->rowCount()-1,2,rButton_b2);
          ui->tableWidget->setCellWidget(ui->tableWidget->rowCount()-1,3,rButton_b3);
      
          combo = new QComboBox();
          combo->addItems({"t1", "t2", "t3", "t4","Delete"});
          connect(combo, SIGNAL(currentIndexChanged(QString)),this,SLOT(on_currentIndexChanged(QString)));
          ui->tableWidget->setCellWidget(ui->tableWidget->rowCount()-1,0,combo);
          g1 = new QButtonGroup;
          g1->addButton(rButton_b1, 1);
          g1->addButton(rButton_b2, 2);
          g1->addButton(rButton_b3, 3);
      }
      void dynamic_gui::on_currentIndexChanged(QString a)
      {
          if(a=="Delete")
          {
              ui->tableWidget->removeRow(ui->tableWidget->currentRow());
          }
      }
      
      
      void dynamic_gui::on_pushButton_ok_clicked()
      {
          // read only the qcombox values in 0th column
          for(int t_row = 0;t_row < ui->tableWidget->rowCount(); t_row++)
          {
                  QComboBox *myCB = qobject_cast<QComboBox*>(ui->tableWidget->cellWidget(t_row,0));
                  qDebug() << myCB->currentText();
                  ui->textEdit->insertPlainText(myCB->currentText() + "\n");
          }
      
          // read which radio buttons are checked in 2nd row
      //    for(int t_col = 0;t_col < ui->tableWidget->columnCount(); t_col++)
      //    {
      //            QButtonGroup *gk=qobject_cast<QButtonGroup*>(ui->tableWidget->cellWidget(2,t_col));
      //            qDebug() << gk->checkedButton();
      
      //            QRadioButton *rk=qobject_cast<QRadioButton*>(ui->tableWidget->cellWidget(2,t_col));
      //            qDebug() << rk->isChecked();
      //    }
      }
      

      111.PNG

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @qt_is_not_easy said in How to get the status of radio buttons from a radio button group which is inside a QTableWidget?:

      I am not able to get the status of radio buttons

      What did you try so far? What is exactly the problem?
      You can iterate over rows/columns using https://doc.qt.io/qt-6/qtablewidget.html#item and then cast QTableWidgetItem to QRadiaButton to check its status.

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

      Q 1 Reply Last reply
      1
      • Q qt_is_not_easy

        Hello all, I am very new to Qt and I just built this small little gui with radio buttons in QButtonGroup in each row of a QTableWidget. I wanted a dynamic GUI so I have a button to add rows dynamically to table (which wasn't a big deal). I also have a QComboBox at the 0th column in QTableWidget and I am able to know it's text using

            for(int t_row = 0;t_row < ui->tableWidget->rowCount(); t_row++)
            {
                    QComboBox *myCB = qobject_cast<QComboBox*>(ui->tableWidget->cellWidget(t_row,0));
                    qDebug() << myCB->currentText();
                    ui->textEdit->insertPlainText(myCB->currentText() + "\n");
            }
        

        However, I am not able to get the status of radio buttons and after spending sometime I do see that it is doable by using checkedId or checkedButton(), but I am not yet confident how to do so. Could anyone please let me know how would I do that here?

        Also, I wanted to know if it is possible for me to use models and how much easier it is to learn as that is something that many of the threads mentioned but I haven't yet been able to understand that.
        Thanks in advance!

        I am giving here the minimal code that I can to illustrate my problem and also I am attaching an image of the GUI.
        .cpp code

        #include "dynamic_gui.h"
        #include "ui_dynamic_gui.h"
        
        dynamic_gui::dynamic_gui(QWidget *parent)
            : QMainWindow(parent)
            , ui(new Ui::dynamic_gui)
        {
            ui->setupUi(this);
            connect(ui->pushButton_cancel, SIGNAL(clicked()), this, SLOT(close()));
        }
        
        dynamic_gui::~dynamic_gui()
        {
            delete ui;
        }
        
        
        void dynamic_gui::on_pushButton_clicked()
        {
            ui->tableWidget->insertRow(ui->tableWidget->rowCount());
            rButton_b1 = new QRadioButton(ui->tableWidget);
            rButton_b2 = new QRadioButton(ui->tableWidget);
            rButton_b3 = new QRadioButton(ui->tableWidget);
        
            ui->tableWidget->setCellWidget(ui->tableWidget->rowCount()-1,1,rButton_b1);
            ui->tableWidget->setCellWidget(ui->tableWidget->rowCount()-1,2,rButton_b2);
            ui->tableWidget->setCellWidget(ui->tableWidget->rowCount()-1,3,rButton_b3);
        
            combo = new QComboBox();
            combo->addItems({"t1", "t2", "t3", "t4","Delete"});
            connect(combo, SIGNAL(currentIndexChanged(QString)),this,SLOT(on_currentIndexChanged(QString)));
            ui->tableWidget->setCellWidget(ui->tableWidget->rowCount()-1,0,combo);
            g1 = new QButtonGroup;
            g1->addButton(rButton_b1, 1);
            g1->addButton(rButton_b2, 2);
            g1->addButton(rButton_b3, 3);
        }
        void dynamic_gui::on_currentIndexChanged(QString a)
        {
            if(a=="Delete")
            {
                ui->tableWidget->removeRow(ui->tableWidget->currentRow());
            }
        }
        
        
        void dynamic_gui::on_pushButton_ok_clicked()
        {
            // read only the qcombox values in 0th column
            for(int t_row = 0;t_row < ui->tableWidget->rowCount(); t_row++)
            {
                    QComboBox *myCB = qobject_cast<QComboBox*>(ui->tableWidget->cellWidget(t_row,0));
                    qDebug() << myCB->currentText();
                    ui->textEdit->insertPlainText(myCB->currentText() + "\n");
            }
        
            // read which radio buttons are checked in 2nd row
        //    for(int t_col = 0;t_col < ui->tableWidget->columnCount(); t_col++)
        //    {
        //            QButtonGroup *gk=qobject_cast<QButtonGroup*>(ui->tableWidget->cellWidget(2,t_col));
        //            qDebug() << gk->checkedButton();
        
        //            QRadioButton *rk=qobject_cast<QRadioButton*>(ui->tableWidget->cellWidget(2,t_col));
        //            qDebug() << rk->isChecked();
        //    }
        }
        

        111.PNG

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #3

        @qt_is_not_easy
        As @jsulm says. Your QRadioButton *rk=qobject_cast<QRadioButton*>(ui->tableWidget->cellWidget(2,t_col)); already looks like the right principle, well done! Only thing is you are looping over every column assuming it will be a radio button, but not all columns are.

        Either only do that in the column numbers you know contain a QRadioButton (1, 2, 3 but not 0), or check the return result of the dynamic cast, which is a good idea anyway:

            for(int t_col = 0; t_col < ui->tableWidget->columnCount(); t_col++)
            {
                    QComboBox *ck=qobject_cast<QComboBox*>(ui->tableWidget->cellWidget(2,t_col));
                    if (ck)
                        qDebug() << t_col << "It's a combobox";
        
                    QRadioButton *rk=qobject_cast<QRadioButton*>(ui->tableWidget->cellWidget(2,t_col));
                    if (rk)
                        qDebug() << t_col << "It's a radiobutton" << rk->isChecked();
            }
        
        Q 1 Reply Last reply
        3
        • JonBJ JonB

          @qt_is_not_easy
          As @jsulm says. Your QRadioButton *rk=qobject_cast<QRadioButton*>(ui->tableWidget->cellWidget(2,t_col)); already looks like the right principle, well done! Only thing is you are looping over every column assuming it will be a radio button, but not all columns are.

          Either only do that in the column numbers you know contain a QRadioButton (1, 2, 3 but not 0), or check the return result of the dynamic cast, which is a good idea anyway:

              for(int t_col = 0; t_col < ui->tableWidget->columnCount(); t_col++)
              {
                      QComboBox *ck=qobject_cast<QComboBox*>(ui->tableWidget->cellWidget(2,t_col));
                      if (ck)
                          qDebug() << t_col << "It's a combobox";
          
                      QRadioButton *rk=qobject_cast<QRadioButton*>(ui->tableWidget->cellWidget(2,t_col));
                      if (rk)
                          qDebug() << t_col << "It's a radiobutton" << rk->isChecked();
              }
          
          Q Offline
          Q Offline
          qt_is_not_easy
          wrote on last edited by
          #4

          @JonB Thanks a lot for explaining and also for taking the time in rewriting that part of the code! It works now and yes I should have used ```

          if (ck), if (rk)
          

          Good learning.

          1 Reply Last reply
          0
          • jsulmJ jsulm

            @qt_is_not_easy said in How to get the status of radio buttons from a radio button group which is inside a QTableWidget?:

            I am not able to get the status of radio buttons

            What did you try so far? What is exactly the problem?
            You can iterate over rows/columns using https://doc.qt.io/qt-6/qtablewidget.html#item and then cast QTableWidgetItem to QRadiaButton to check its status.

            Q Offline
            Q Offline
            qt_is_not_easy
            wrote on last edited by
            #5

            @jsulm I was not checking if it is a qcomobox or a radiobutton prior to checking its check state and @JonB pointed that out so the problem is solved now. But thanks for your time!

            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