How do I get my code to return all of the selected checkboxes at once and not one at a time? Google is of zero help to me at this point.
-
I have tried my best to remedy with this. Selecteditems does not work it Qt just complains about a const error. I have posted my code below, can anyone help me. I just want to click a button and have it show what was selected. Not one at a time but all at once. I figure doing a
string+=value "could" work but if I disable a checkbox then that value would still be there.code_text #include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); int c; QDirIterator dirIt("/usr/ports",QDirIterator::Subdirectories); while (dirIt.hasNext()) { dirIt.next(); if(dirIt.fileName()==".." || dirIt.fileName()==".") { } else { if (QFileInfo(dirIt.filePath()).isFile()) { } else { // c+=1; QListWidgetItem *values = new QListWidgetItem; values->setText(dirIt.fileName()); values->setCheckState(Qt::Unchecked); ui->listWidget->addItem(values); //ui->listWidget->setItemWidget(values,new QCheckBox("zstd")); } } } ui->listWidget->sortItems(Qt::AscendingOrder); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { QMessageBox msgBox; int x; QListWidgetItem *item = ui->listWidget->currentItem(); msgBox.setText(item->text()); msgBox.exec(); }
-
@voncloft said in How do I get my code to return all of the selected checkboxes at once and not one at a time? Google is of zero help to me at this point.:
Selecteditems does not work it Qt just complains about a const error.
Well that sounds like the right thing to have used. So show your attempted code + error message for that....
But selected does not mean the same as checked. I don't know if there is a better method for getting all
QListWidgetItem
s which are checked in aQListWidget
, but you can always iterate through them (QListWidget::item(int row)
) all, callingisSelected()
orcheckState()
and building you own list of those which are. -
code_text void MainWindow::on_pushButton_clicked() { QMessageBox msgbox; QListWidget *item = ui->listWidget; msgbox.setText(item->selectedItems()); }
errors:
/usr/include/qt5/QtCore/qstring.h:265: candidate constructor not viable: no known conversion from 'QList<QListWidgetItem *>' to 'QChar' for 1st argument
/usr/include/qt5/QtCore/qstring.h:273: candidate constructor not viable: no known conversion from 'QList<QListWidgetItem *>' to 'QString &&' for 1st argument
/usr/include/qt5/QtCore/qstring.h:830: candidate constructor not viable: no known conversion from 'QList<QListWidgetItem *>' to 'const char *' for 1st argument
/usr/include/qt5/QtCore/qstring.h:833: candidate constructor not viable: no known conversion from 'QList<QListWidgetItem *>' to 'const QByteArray &' for 1st argument
/usr/include/qt5/QtCore/qstring.h:950: candidate constructor not viable: no known conversion from 'QList<QListWidgetItem *>' to 'const QString::Null &' for 1st argument
/usr/include/qt5/QtCore/qstring.h:962: candidate constructor not viable: no known conversion from 'QList<QListWidgetItem *>' to 'QStringDataPtr' for 1st argument
/usr/include/qt5/QtCore/qstring.h:1062: candidate constructor not viable: no known conversion from 'QList<QListWidgetItem *>' to 'QLatin1String' for 1st argument
/usr/include/qt5/QtCore/qstring.h:1088: candidate constructor not viable: no known conversion from 'QList<QListWidgetItem *>' to 'const QString &' for 1st argument
-
Hi,
As the error said, there's no direct conversion between a list of QListWidgetItem and QString. It's your job to build the string out of them and then apply it to your message box.
-
@voncloft
Hi
You just have to loop the listQString resultStr; for (const QListWidgetItem * item : ui->listWidget_2->selectedItems() ) { resultStr += item->text()+"\n"; } QMessageBox::information(this,"title", resultStr);
Note if you only want check ones, add an if statement
QString resultStr; for (const QListWidgetItem * item : ui->listWidget_2->selectedItems() ) { if ( item->checkState() == Qt::CheckState::Checked) resultStr += item->text()+"\n"; }
-
@mrjj said in How do I get my code to return all of the selected checkboxes at once and not one at a time? Google is of zero help to me at this point.:
QString resultStr;
for (const QListWidgetItem * item : ui->listWidget_2->selectedItems() ) {
if ( item->checkState() == Qt::CheckState::Checked)
resultStr += item->text()+"\n";
}Doesn't work on push button just returns what is selected and checked.
-
got it to work with the outline below
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); QDirIterator dirIt("/usr/ports",QDirIterator::Subdirectories); while (dirIt.hasNext()) { dirIt.next(); if(dirIt.fileName()==".." || dirIt.fileName()==".") { } else { if (QFileInfo(dirIt.filePath()).isFile()) { } else { // c+=1; QListWidgetItem *values = new QListWidgetItem; values->setText(dirIt.fileName()); values->setCheckState(Qt::Unchecked); ui->listWidget->addItem(values); //ui->listWidget->setItemWidget(values,new QCheckBox("zstd")); } } } ui->listWidget->sortItems(Qt::AscendingOrder); } MainWindow::~MainWindow() { delete ui; } QString test; void MainWindow::on_listWidget_itemClicked(QListWidgetItem *item) { QMessageBox msgbox; if(item->checkState()==Qt::Checked) { test+=item->text()+" "; } else { test.remove(item->text()); } // msgbox.setText(test); // msgbox.exec(); } void MainWindow::on_pushButton_clicked() { QMessageBox msgbox; msgbox.setText(test); msgbox.exec(); }
-
@voncloft said in How do I get my code to return all of the selected checkboxes at once and not one at a time? Google is of zero help to me at this point.:
got it to work
great, so please don't forget to mark your post as solved!