Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You don't store a <code>QComboBox</code> in a <code>QStandardItemModel</code>. Let's say you have the following choices:</p> <p>A B C D</p> <p>and you have a list with two items in a <code>QListView</code>, the first value being A the second being D:</p> <pre><code> QListView* pView = new QListView(); QStandardItemModel* pModel = new QStandardItemModel(); pView-&gt;setModel(pModel); pModel-&gt;appendRow(new QStandardItem("A")); pModel-&gt;appendRow(new QStandardItem("D")); </code></pre> <p>What we created above is a list widget which will display the values of "A" and "D". Now, to the <code>QComboBox</code>. I assume you want that to edit the values of "A" and "D" in the list. For this, you need to create a <code>QItemDelegate</code>.</p> <p>See <a href="http://doc.qt.io/qt-4.8/qitemdelegate.html" rel="nofollow noreferrer">http://doc.qt.io/qt-4.8/qitemdelegate.html</a></p> <p>An attempt:</p> <pre><code> class ComboBoxDelegate : public QItemDelegate { Q_OBJECT public: ComboBoxDelegate(QObject *parent = 0); QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index) const; void setEditorData(QWidget *editor, const QModelIndex &amp;index) const; void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &amp;index) const; void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index) const; }; ComboBoxDelegate::ComboBoxDelegate(QObject *parent) : QItemDelegate(parent) { } QWidget *ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &amp;/* option */, const QModelIndex &amp;/* index */) const { QComboBox *editor = new QComboBox(parent); editor-&gt;addItem("A"); editor-&gt;addItem("B"); editor-&gt;addItem("C"); editor-&gt;addItem("D"); return editor; } void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &amp;index) const { QString value = index.model()-&gt;data(index, Qt::EditRole).toString(); QComboBox *cBox = static_cast&lt;QComboBox*&gt;(editor); cBox-&gt;setCurrentIndex(cBox-&gt;findText(value)); } void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &amp;index) const { QComboBox *cBox = static_cast&lt;QComboBox*&gt;(editor); QString value = cBox-&gt;currentText(); model-&gt;setData(index, value, Qt::EditRole); } void ComboBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;/* index */) const { editor-&gt;setGeometry(option.rect); } </code></pre> <p>And then you need to set the delegate on the <code>QListView</code> to make it work, see:</p> <pre><code>pView-&gt;setItemDelegate(new ComboBoxDelegate(pView)); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload