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 do I check the state of a QListWidget's item's radiobuttons?
Forum Updated to NodeBB v4.3 + New Features

How do I check the state of a QListWidget's item's radiobuttons?

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 5 Posters 2.7k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    moyin
    wrote on last edited by
    #1

    Hello,
    I've been struggling with a problem about items of QListWidgets. i have a qlistwidget with radiobuttons as list elements. now i want to read the text of selected element(radiobutton). Please let me is there any way to get this to work.

    Taz742T JonBJ 2 Replies Last reply
    0
    • M moyin

      Hello,
      I've been struggling with a problem about items of QListWidgets. i have a qlistwidget with radiobuttons as list elements. now i want to read the text of selected element(radiobutton). Please let me is there any way to get this to work.

      Taz742T Offline
      Taz742T Offline
      Taz742
      wrote on last edited by Taz742
      #2

      @moyin said in How do I check the state of a QListWidget's item's radiobuttons?:

      i have a qlistwidget with radiobuttons

      How ? Like setItemWidget ? If you are using RadioButton only 1 will be selected.

      Maybe you are using checkbox ?

      If yes:

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include "QDebug"
      #include "QFileDialog"
      #include "QFile"
      #include "QRadioButton"
      #include "QCheckBox"
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          for (int i = 0; i < 10; i++) {
              auto item = new QListWidgetItem(ui->listWidget);
              ui->listWidget->setItemWidget(item, new QCheckBox(QString("Item %1").arg(i)));
          }
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      void MainWindow::on_pushButton_clicked()
      {
          for (int i = 0; i < ui->listWidget->count(); i++) {
              auto checkBox = static_cast<QCheckBox*>(ui->listWidget->itemWidget(ui->listWidget->item(i)));
              if (checkBox->isChecked()) {
                  qDebug() << checkBox->text();
              }
          }
      }
      
      

      Do what you want.

      M 1 Reply Last reply
      0
      • Taz742T Taz742

        @moyin said in How do I check the state of a QListWidget's item's radiobuttons?:

        i have a qlistwidget with radiobuttons

        How ? Like setItemWidget ? If you are using RadioButton only 1 will be selected.

        Maybe you are using checkbox ?

        If yes:

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include "QDebug"
        #include "QFileDialog"
        #include "QFile"
        #include "QRadioButton"
        #include "QCheckBox"
        
        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
        
            for (int i = 0; i < 10; i++) {
                auto item = new QListWidgetItem(ui->listWidget);
                ui->listWidget->setItemWidget(item, new QCheckBox(QString("Item %1").arg(i)));
            }
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        
        void MainWindow::on_pushButton_clicked()
        {
            for (int i = 0; i < ui->listWidget->count(); i++) {
                auto checkBox = static_cast<QCheckBox*>(ui->listWidget->itemWidget(ui->listWidget->item(i)));
                if (checkBox->isChecked()) {
                    qDebug() << checkBox->text();
                }
            }
        }
        
        
        M Offline
        M Offline
        moyin
        wrote on last edited by
        #3

        @Taz742
        thanks for rply,
        No i'm using radio buttons only, but i want the text of the selected one, so i can pass to the other functions.

        Taz742T 1 Reply Last reply
        0
        • M moyin

          Hello,
          I've been struggling with a problem about items of QListWidgets. i have a qlistwidget with radiobuttons as list elements. now i want to read the text of selected element(radiobutton). Please let me is there any way to get this to work.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @moyin
          If you have a "list widget with radio button items", that implies single selection from a list. Does you usage allow instead for a QCombobox, which is simplest?

          M 1 Reply Last reply
          1
          • M moyin

            @Taz742
            thanks for rply,
            No i'm using radio buttons only, but i want the text of the selected one, so i can pass to the other functions.

            Taz742T Offline
            Taz742T Offline
            Taz742
            wrote on last edited by Taz742
            #5

            @moyin
            I edited my answer.
            Then you can edit

            auto checkBox = static_cast<QCheckBox*>(ui->listWidget->itemWidget(ui->listWidget->item(i)));
            

            to

            auto radioButon = static_cast<QRadioButton*>(ui->listWidget->itemWidget(ui->listWidget->item(i)));
            

            Do what you want.

            1 Reply Last reply
            0
            • JonBJ JonB

              @moyin
              If you have a "list widget with radio button items", that implies single selection from a list. Does you usage allow instead for a QCombobox, which is simplest?

              M Offline
              M Offline
              moyin
              wrote on last edited by
              #6

              @JonB
              thnks for rply,
              yes i know it will be simple if i go with checkbox, but my requirment is to select one at a time and multiple selections should not allow.

              JonBJ 1 Reply Last reply
              0
              • M moyin

                @JonB
                thnks for rply,
                yes i know it will be simple if i go with checkbox, but my requirment is to select one at a time and multiple selections should not allow.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @moyin
                Then if you had not used the "radio buttons" you could have gone for http://doc.qt.io/qt-5/qabstractitemview.html#selectionMode-prop with single-selection. Using radio buttons in each item I assume you'll have to manage the selection/deselection yourself.

                1 Reply Last reply
                1
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8
                  1. why are you using setItemWidget?
                  2. Can you use Qt 5.11? (the solution is a lot easier if you reply yes)

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  M 1 Reply Last reply
                  0
                  • VRoninV VRonin
                    1. why are you using setItemWidget?
                    2. Can you use Qt 5.11? (the solution is a lot easier if you reply yes)
                    M Offline
                    M Offline
                    moyin
                    wrote on last edited by
                    #9

                    @VRonin
                    yes, sure i can use Qt5.11

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mad-hatter
                      wrote on last edited by VRonin
                      #10

                      Hello,

                      What you want could be done with signals & slots, something like this .
                      This is using a grid layout but you could use any container.

                      Regards

                      QString num;
                          QGridLayout *layout = new QGridLayout;
                          for( int i = 0; i < 10; i++ )
                          {
                              QRadioButton *RB = new QRadioButton;
                              RB->setFixedHeight(14);
                              num = QString::number(i);
                              RB->setObjectName( num );
                              RB->setText( num + " - " );
                      
                      	if ( i == 0 )
                      		{
                      			RB->setChecked( true );
                      			RB->setText( RB->text() + " (got)" );
                              }
                      		
                              QObject::connect( RB, SIGNAL( toggled( bool ) ), this,
                                                SLOT( onWhichButtonSlot( bool ) ) );
                              layout->addWidget( RB );
                          }
                      
                      void yourClass::onWhichButtonSlot( bool state )
                      {
                            QObject *object = QObject::sender();
                            QRadioButton *btn = qobject_cast<QRadioButton *>(object);
                      
                          if (state)
                          {
                              btn->setText( btn->text() + " (got)" );
                          }
                          else
                          {
                              btn->setText( btn->text().remove( " (got)" ) );
                          }
                      }
                      
                      1 Reply Last reply
                      0
                      • VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by VRonin
                        #11

                        Example, only works in Qt 5.11 or later:

                        #include <QApplication>
                        #include <QListWidget>
                        
                        int main(int argc, char *argv[])
                        {
                            QApplication application(argc, argv);
                            QListWidget mainWid;
                            mainWid.model()->insertRows(0,10);
                            for(int i=0;i<mainWid.model()->rowCount();++i){
                               QListWidgetItem* item = mainWid.item(i);
                               item->setFlags(item->flags() | Qt::ItemIsUserCheckable );
                               item->setData(Qt::CheckStateRole,Qt::Unchecked);
                               item->setData(Qt::EditRole, "Item " + QString::number(i));
                            }
                            QObject::connect(mainWid.model(),&QAbstractItemModel::dataChanged,[&mainWid](const QModelIndex& topLeft,const QModelIndex& bottomRight,const QVector<int>& roles)->void{
                                Q_ASSERT(topLeft==bottomRight);
                                if(!roles.contains(Qt::CheckStateRole))
                                    return;
                                if(topLeft.data(Qt::CheckStateRole)!=Qt::Checked)
                                    return;
                                for(int i=0;i<mainWid.model()->rowCount();++i){
                                    if(i==topLeft.row())
                                        continue;
                                    mainWid.item(i)->setData(Qt::CheckStateRole,Qt::Unchecked);
                                }
                            });
                            mainWid.show();
                            return application.exec();
                        }
                        
                        
                        

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        1 Reply Last reply
                        2

                        • Login

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