QStandardItem + QComboBox

Tom Leese picture Tom Leese · Jun 28, 2010 · Viewed 9.5k times · Source

I am trying to put a QComboBox into a QStandardItem to be used in a QStandardItemModel. I have been looking around and I cannot find an answer, any ideas?

Answer

Kip Streithorst picture Kip Streithorst · Jun 29, 2010

You don't store a QComboBox in a QStandardItemModel. Let's say you have the following choices:

A B C D

and you have a list with two items in a QListView, the first value being A the second being D:

 QListView* pView = new QListView();
 QStandardItemModel* pModel = new QStandardItemModel();
 pView->setModel(pModel);
 pModel->appendRow(new QStandardItem("A"));
 pModel->appendRow(new QStandardItem("D"));

What we created above is a list widget which will display the values of "A" and "D". Now, to the QComboBox. I assume you want that to edit the values of "A" and "D" in the list. For this, you need to create a QItemDelegate.

See http://doc.qt.io/qt-4.8/qitemdelegate.html

An attempt:

 class ComboBoxDelegate : public QItemDelegate
 {
    Q_OBJECT

 public:
    ComboBoxDelegate(QObject *parent = 0);

    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                       const QModelIndex &index) const;

    void setEditorData(QWidget *editor, const QModelIndex &index) const;
    void setModelData(QWidget *editor, QAbstractItemModel *model,
                   const QModelIndex &index) const;

    void updateEditorGeometry(QWidget *editor,
     const QStyleOptionViewItem &option, const QModelIndex &index) const;
};

ComboBoxDelegate::ComboBoxDelegate(QObject *parent)
 : QItemDelegate(parent)
{
}

QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
 const QStyleOptionViewItem &/* option */,
 const QModelIndex &/* index */) const
{
   QComboBox *editor = new QComboBox(parent);
   editor->addItem("A");
   editor->addItem("B");
   editor->addItem("C");
   editor->addItem("D");

   return editor;
}

void ComboBoxDelegate::setEditorData(QWidget *editor,
                                 const QModelIndex &index) const
{
   QString value = index.model()->data(index, Qt::EditRole).toString();

   QComboBox *cBox = static_cast<QComboBox*>(editor);
   cBox->setCurrentIndex(cBox->findText(value));
}

void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                const QModelIndex &index) const
{
   QComboBox *cBox = static_cast<QComboBox*>(editor);
   QString value = cBox->currentText();

   model->setData(index, value, Qt::EditRole);
}    

void ComboBoxDelegate::updateEditorGeometry(QWidget *editor,
 const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
   editor->setGeometry(option.rect);
}

And then you need to set the delegate on the QListView to make it work, see:

pView->setItemDelegate(new ComboBoxDelegate(pView));