复选框的组合框? [英] ComboBox of CheckBoxes?

查看:46
本文介绍了复选框的组合框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使 ComboBox 中的项目可检查.我试过这个:

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

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

我将 QStandardItemModel 子类化并重新实现了 flags() 函数以使项目可检查.然后我将此模型添加到 ComboBox.不幸的是,项目没有出现复选框.谁能看到我哪里出错了?

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?

推荐答案

您是否设置了检查状态并使其可检查?

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

这篇关于复选框的组合框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆