24

I am trying to make the items in a ComboBox checkable. I tried this:

http://programmingexamples.net/wiki/Qt/ModelView/ComboBoxOfCheckBoxes

where I subclassed QStandardItemModel and re-implemented the flags() function to make the items checkable. Then I added this model to the ComboBox. Unfortunately, a checkbox does not appear with the items. Can anyone see where I have gone wrong?

bariod
  • 69
  • 2
  • 11
David Doria
  • 9,873
  • 17
  • 85
  • 147

4 Answers4

23

Have you set a check state as well as making them checkable?

In my example below, this line is critical:

item->setData(Qt::Unchecked, Qt::CheckStateRole);

If it is omitted the check boxes won't render as there isn't a valid check-state to render.

The example shows check boxes in a combobox, list and table, as I couldn't get it to work at first either, so I tried different views.

test.cpp

#include <QtGui>

int main(int argc, char** argv)
{
    QApplication app(argc, argv);

    QStandardItemModel model(3, 1); // 3 rows, 1 col
    for (int r = 0; r < 3; ++r)
    {
        QStandardItem* 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);
    }

    QComboBox* combo = new QComboBox();
    combo->setModel(&model);

    QListView* list = new QListView();
    list->setModel(&model);

    QTableView* table = new QTableView();
    table->setModel(&model);

    QWidget container;
    QVBoxLayout* containerLayout = new QVBoxLayout();
    container.setLayout(containerLayout);
    containerLayout->addWidget(combo);
    containerLayout->addWidget(list);
    containerLayout->addWidget(table);

    container.show();

    return app.exec();
}

test.pro

QT=core gui
SOURCES=test.cpp
Silas Parker
  • 8,017
  • 1
  • 28
  • 43
  • 2
    Awesome - that certainly does the trick! Now if I want to connect one of the checkbox's signals to a slot (say, clicked(), to see when it was clicked and if it is checked), how would I do that? QStandardItem isn't derived from QObject, so it can't have signals. I looked at the signals for QComboBox - the highlighted() signal is emitted when I hover over a new checkbox, but nothing seems to be emitted when I click the checkbox? I tried adding Qt::ItemIsSelectable to the flags of the item, and then combobox::currentIndexChanged is emitted, but the combobox closes, which is not what I want.Ideas? – David Doria Dec 08 '11 at 02:49
  • 1
    You will need to do it in the model, subclass `QAbstractListModel` and implement `rowCount`, `data`, `flags` and `setData`. Make sure the model handles `Qt::CheckStateRole` role. In `setData` you can fire your signals as models are QObject derived. – Silas Parker Dec 08 '11 at 09:12
  • 2
    It is even easier than that :). Without any subclassing necessary: connect(this->Model, SIGNAL(dataChanged ( const QModelIndex&, const QModelIndex&)), this, SLOT(slot_changed())); with: void MainWindow::slot_changed() { std::cout << "highlighted." << std::endl; if(this->Item->checkState() == Qt::Unchecked) { std::cout << "Unchecked!" << std::endl; } else if(this->Item->checkState() == Qt::Checked) { std::cout << "Checked!" << std::endl; } } – David Doria Dec 08 '11 at 12:34
  • The working example is available here: http://programmingexamples.net/wiki/Qt/ModelView/ComboBoxOfCheckBoxes – David Doria Dec 08 '11 at 12:43
17

I have a little addition.

If one compiles the skyhisi's code then the combobox on Mac OS X doesn't look as combobox with native checkboxes. You can see it on the screenshot.

enter image description here

Tested with qt-4.8.5 and 5.1.1.

It seems like Qt draws these controls by itself. Our team has found the following workaround by pure accident. You can subclass QStyledItemDelegate and reimplement paint() this way:

void SubclassOfQStyledItemDelegate::paint(QPainter * painter_, const QStyleOptionViewItem & option_, const QModelIndex & index_) const
{
    QStyleOptionViewItem & refToNonConstOption = const_cast<QStyleOptionViewItem &>(option_);
    refToNonConstOption.showDecorationSelected = false;
    //refToNonConstOption.state &= ~QStyle::State_HasFocus & ~QStyle::State_MouseOver;

    QStyledItemDelegate::paint(painter_, refToNonConstOption, index_);
}

You can then set this delegate to the combo box by adding the following lines to skyhisi's code:

SubclassOfQStyledItemDelegate *delegate = new SubclassOfQStyledItemDelegate(this);
combo->setItemDelegate(delegate);

The comboBox installed with this delegate looks the following way: enter image description here

On Windows there may be a different issue: text of the checkBoxes has sticked background or dotted border around an item:

enter image description here

To change this appearance one can add the following line to the overridden paint just before the line QStyledItemDelegate::paint(painter_, refToNonConstOption, index_) (in the code sample this line was commented):

refToNonConstOption.state &= ~QStyle::State_HasFocus & ~QStyle::State_MouseOver;

Result:

enter image description here

Neptilo
  • 465
  • 1
  • 5
  • 17
gshep
  • 628
  • 6
  • 14
  • 2
    Hi, since there is multiple selection in comboBox (windows example) it doesn't make sense to show "Item 0" text. Is there a way to disable that text or to change it to something like "please, select items" – Aleksandar Jan 16 '14 at 13:14
  • 1
    @Aleksandar or instead of "Item 0" which may *not* have been selected, we should see "Item1,Item2": a concatenation of all the selected item in the combo box. Does someone knows whether it's doable? – yves Baumes Mar 02 '16 at 13:54
  • 1
    @yvesBaumes I've created a sub class a QComboBox that updates the visible text depending on selection (and some other features) https://gist.github.com/mistic100/c3b7f3eabc65309687153fe3e0a9a720 – Mistic Jan 23 '17 at 19:15
1

You can try this with QListView:

QStringList values = QStringList << "check 1" << "check 2" << "check 3" << "check 4";

QStandardItemModel model = new QStandardItemModel;
for (int i = 0; i < values.count(); i++)
{
    QStandardItem *item = new QStandardItem();
    item->setText(values[i]);
    item->setCheckable(true);
    item->setCheckState(Qt::Unchecked);
    model->setItem(i, item);
}

ui->list->setModel(model);
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Sergio Cabral
  • 6,490
  • 2
  • 35
  • 37
1

I tried to make this example on Linux Mint, but I can't make the checkboxes visible. I had to implement the SubclassOfQStyledItemDelegate class and set the delegate to the checkbox as Neptilo and gshep advised.

perimeno
  • 177
  • 1
  • 8