2

我将 QStandardItemModel 与 QStandardItem 一起使用。

我不想编写自己的模型和任何代表。

我只想在第二列中有带有 QComboBox 的复选框树...

m_model->setColumnCount(2);
for (int i = 0; i < sectionCount; i++)
{
    QStandardItem * section = new QStandardItem(tr("Section %1").arg(i+1));
    section->setCheckable(true);
    section->setCheckState(Qt::Checked);

    for (int j = 0; j < itemsCount; j++)
    {
        QStandardItem * item = new QStandardItem(tr("Item %1").arg(j+1));
        item->setCheckable(true);
        item->setCheckState(Qt::Checked);

        QStandardItem * item2 = new QStandardItem("xxx");

        section->appendRow(QList<QStandardItem*>() << item << item2);

        QComboBox * combo = new QComboBox();
        QModelIndex index = m_model->index(j, 1, );

        // HERE i have index = {-1;-1}

        ui->treeView_options->setIndexWidget(index, combo);
    }
    m_model->appendRow(section);
}

可以这样使用 setIndexWidget 吗?

更新:

我在第二列中没有 QComboBox... 为什么?

4

2 回答 2

6

it is possible actually. I would recommend first creating a model with two columns. Create the items in a row and append it to the model. Only after you appended the row with items you can call view->setIndexWidget(), with your combobox content. It worked for me, and I have dynamic content. ItemDelegates are more complicated, I would recommend setIndexWidget() - worked for me just fine.

于 2012-11-05T19:43:28.520 回答
0

不,不会工作:

此功能仅应用于显示与数据项对应的可见区域内的静态内容。如果要显示自定义动态内容或实现自定义编辑器小部件,请改为子类 QItemDelegate。

于 2012-03-12T14:45:12.557 回答