Important: Please read the Qt Code of Conduct -

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

  • 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.

  • @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) :
        ui(new Ui::MainWindow)
        for (int i = 0; i < 10; i++) {
            auto item = new QListWidgetItem(ui->listWidget);
            ui->listWidget->setItemWidget(item, new QCheckBox(QString("Item %1").arg(i)));
        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();

  • @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.

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

  • @moyin
    I edited my answer.
    Then you can edit

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


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

  • @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.

  • @moyin
    Then if you had not used the "radio buttons" you could have gone for with single-selection. Using radio buttons in each item I assume you'll have to manage the selection/deselection yourself.

    1. why are you using setItemWidget?
    2. Can you use Qt 5.11? (the solution is a lot easier if you reply yes)

  • @VRonin
    yes, sure i can use Qt5.11

  • 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.


    QString num;
        QGridLayout *layout = new QGridLayout;
        for( int i = 0; i < 10; i++ )
            QRadioButton *RB = new QRadioButton;
            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)" );
            btn->setText( btn->text().remove( " (got)" ) );

  • 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;
        for(int i=0;i<mainWid.model()->rowCount();++i){
           QListWidgetItem* item = mainWid.item(i);
           item->setFlags(item->flags() | Qt::ItemIsUserCheckable );
           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{
            for(int i=0;i<mainWid.model()->rowCount();++i){
        return application.exec();

Log in to reply