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 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.
Forum Update on Monday, May 27th 2025

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.

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 568 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.
  • V Offline
    V Offline
    voncloft
    wrote on last edited by
    #1

    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();
      
    
    }
    
    JonBJ 1 Reply Last reply
    0
    • V voncloft

      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();
        
      
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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 QListWidgetItems which are checked in a QListWidget, but you can always iterate through them (QListWidget::item(int row)) all, calling isSelected() or checkState() and building you own list of those which are.

      1 Reply Last reply
      1
      • V Offline
        V Offline
        voncloft
        wrote on last edited by
        #3
        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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • V Offline
            V Offline
            voncloft
            wrote on last edited by
            #5

            Can you point me to somewhere on how to do that google is useless at this point.

            mrjjM 1 Reply Last reply
            0
            • V voncloft

              Can you point me to somewhere on how to do that google is useless at this point.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @voncloft
              Hi
              You just have to loop the list

                  QString 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";
                  }
              
              V 1 Reply Last reply
              1
              • mrjjM mrjj

                @voncloft
                Hi
                You just have to loop the list

                    QString 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";
                    }
                
                V Offline
                V Offline
                voncloft
                wrote on last edited by voncloft
                #7

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

                1 Reply Last reply
                0
                • V Offline
                  V Offline
                  voncloft
                  wrote on last edited by
                  #8

                  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();
                      }
                  
                  
                  Pablo J. RoginaP 1 Reply Last reply
                  0
                  • V voncloft

                    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();
                        }
                    
                    
                    Pablo J. RoginaP Offline
                    Pablo J. RoginaP Offline
                    Pablo J. Rogina
                    wrote on last edited by
                    #9

                    @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!

                    Upvote the answer(s) that helped you solve the issue
                    Use "Topic Tools" button to mark your post as Solved
                    Add screenshots via postimage.org
                    Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                    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