Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QStandardItem + QComboBox

Tags:

qt

qcombobox

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?

like image 689
Tom Leese Avatar asked Jun 28 '10 19:06

Tom Leese


People also ask

How do I clear my QT combobox?

A combobox can be populated using the insert functions, insertItem() and insertItems() for example. Items can be changed with setItemText(). An item can be removed with removeItem() and all items can be removed with clear().


1 Answers

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));
like image 54
Kip Streithorst Avatar answered Sep 28 '22 02:09

Kip Streithorst