Unsolved 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) : 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(); } } }
-
@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 aQCombobox
, which is simplest? -
@moyin
I edited my answer.
Then you can editauto checkBox = static_cast<QCheckBox*>(ui->listWidget->itemWidget(ui->listWidget->item(i)));
to
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 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. -
- why are you using
setItemWidget
? - Can you use Qt 5.11? (the solution is a lot easier if you reply yes)
- why are you using
-
@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.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)" ) ); } }
-
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(); }