Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ComboBox with Checkboxes inside, C++ Qt, select/unselect all checkboxes

I wrote a simple combobox with checkboxes as its items. When I select an item and press button, it shows what item I selected. But I would like to do something like this: I have an item called "all" - when I select it, all other items should be selected, and when I unselect it, all others items should be unselected. Any ideas?

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QStandardItemModel>
#include <QComboBox>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QStandardItemModel *model;

private slots:

    void buttonclicked();
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->model = new QStandardItemModel(4, 1); // 4 rows, 1 col
    for (int r = 0; r < 4; ++r)
    {
        QStandardItem* item;
        if(r == 0)
            item = new QStandardItem(QString("All"));
        else
            item = new QStandardItem(QString("Item %0").arg(r));

        item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
        item->setData(Qt::Unchecked, Qt::CheckStateRole);

        model->setItem(r, 0, item);
    }

    ui->comboBox->setModel(model);

    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(buttonclicked()));
}

void MainWindow::buttonclicked()
{
    unsigned int i;
    for(i=0; i<ui->comboBox->count(); i++)
    {
        QModelIndex index = ui->comboBox->model()->index(i, 0);
        QVariant v = index.data(Qt::CheckStateRole);
        int j = v.toInt();
        if(j == 2)
        {
            QModelIndex ii = ui->comboBox->model()->index(i, 0);
            QString text = ii.data(Qt::DisplayRole).toString();
            qDebug() << text;
        }

    }
    qDebug()<<"";
}

MainWindow::~MainWindow()
{
    delete ui;
}

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

ui file

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QComboBox" name="comboBox">
    <property name="geometry">
     <rect>
      <x>100</x>
      <y>50</y>
      <width>201</width>
      <height>23</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>110</x>
      <y>100</y>
      <width>181</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>Check what was selected</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>400</width>
     <height>20</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

It looks like this:

enter image description here

like image 550
Brian Brown Avatar asked Nov 12 '22 21:11

Brian Brown


1 Answers

Implement a qtslot for this button such that when it is clicked/triggered you will iterate over all of the checkboxes within the desired combobox and edit their checked state.

like image 167
UpAndAdam Avatar answered Nov 15 '22 04:11

UpAndAdam