3

I am using QStandardItemModel with QStandardItem's.

I don't want to write my own model and any delegates.

I just want to have tree of checkboxes with QComboBox'es in second column...

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);
}

Is it possible to use setIndexWidget this way?

UPDATE:

I have no QComboBox in second column... Why?

k06a
  • 17,755
  • 10
  • 70
  • 110

2 Answers2

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.

Mats Rietdijk
  • 2,576
  • 3
  • 20
  • 25
0

Nope, won't work:

This function should only be used to display static content within the visible area corresponding to an item of data. If you want to display custom dynamic content or implement a custom editor widget, subclass QItemDelegate instead.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Chris Browet
  • 4,156
  • 20
  • 17
  • I'd like to display static content – k06a Mar 12 '12 at 14:49
  • Mmm... That's not what I understand by `I just want to have tree of checkboxes with QComboBox'es in second column...`. Static means you'd have to manually create 1 combo for each cell you want it in, without relation with the data – Chris Browet Mar 12 '12 at 14:55
  • CheckBox's content is fixed on compile-time. – k06a Mar 12 '12 at 14:58