Difference between revisions of "Combo Boxes in Item Views"

From Qt Wiki
Jump to: navigation, search
(Checking index.column() in the delegate is bad design as it ties the delegate to a specific model, setItemDelegateForColumn should be used instead)
m (Added nullptr and override)
Line 8: Line 8:
  
 
<code>
 
<code>
#ifndef ITEMDELEGATE_H
+
#ifndef COMBOBOXITEMDELEGATE_H
#define ITEMDELEGATE_H
+
#define COMBOBOXITEMDELEGATE_H
  
 
#include <QStyledItemDelegate>
 
#include <QStyledItemDelegate>
Line 17: Line 17:
 
     Q_OBJECT
 
     Q_OBJECT
 
public:
 
public:
     ComboBoxItemDelegate(QObject* parent=0);
+
     ComboBoxItemDelegate(QObject* parent= Q_NULLPTR);
 
     ~ComboBoxItemDelegate();
 
     ~ComboBoxItemDelegate();
  
     virtual QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const;
+
     QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const Q_DECL_OVERRIDE;
     virtual void setEditorData(QWidget* editor, const QModelIndex& index) const;
+
     void setEditorData(QWidget* editor, const QModelIndex& index) const Q_DECL_OVERRIDE;
     virtual void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const;
+
     void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const Q_DECL_OVERRIDE;
 
};
 
};
  
#endif // ITEMDELEGATE_H
+
#endif // COMBOBOXITEMDELEGATE_H
 
</code>
 
</code>
  

Revision as of 08:54, 5 December 2018

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

Sample code to use combo boxes as editor widgets in an item view or item widget.

The delegate creates a combo box if the index is in the second column of a list view. For the other columns it just returns the default editor, that QStyledItemDelegate creates.

File itemdelegate.h

#ifndef COMBOBOXITEMDELEGATE_H
#define COMBOBOXITEMDELEGATE_H

#include <QStyledItemDelegate>

class ComboBoxItemDelegate : public QStyledItemDelegate
{
    Q_OBJECT
public:
    ComboBoxItemDelegate(QObject* parent= Q_NULLPTR);
    ~ComboBoxItemDelegate();

    QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const Q_DECL_OVERRIDE;
    void setEditorData(QWidget* editor, const QModelIndex& index) const Q_DECL_OVERRIDE;
    void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const Q_DECL_OVERRIDE;
};

#endif // COMBOBOXITEMDELEGATE_H

File itemdelegate.cpp

#include "itemdelegate.h"
#include <QComboBox>

ComboBoxItemDelegate::ComboBoxItemDelegate(QObject* parent)
    : QStyledItemDelegate(parent)
{
}


ComboBoxItemDelegate::~ComboBoxItemDelegate()
{
}


QWidget* ComboBoxItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    // Create the combobox and populate it
    QComboBox* cb = new QComboBox(parent);
    int row = index.row();
    cb->addItem(QString("one in row %1").arg(row));
    cb->addItem(QString("two in row %1").arg(row));
    cb->addItem(QString("three in row %1").arg(row));
    return cb;
}


void ComboBoxItemDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
    QComboBox* cb = qobject_cast<QComboBox*>(editor);
    Q_ASSERT(cb);
    // get the index of the text in the combobox that matches the current value of the itenm
    QString currentText = index.data(Qt::EditRole).toString();
    int cbIndex = cb->findText(currentText);
    // if it is valid, adjust the combobox
    if (cbIndex >= 0)
       cb->setCurrentIndex(cbIndex);
}


void ComboBoxItemDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
{
    QComboBox* cb = qobject_cast<QComboBox*>(editor);
    Q_ASSERT(cb);
    model->setData(index, cb->currentText(), Qt::EditRole);
}


File main.cpp

#include <QApplication>
#include <QTableWidget>

#include "itemdelegate.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTableWidget tw;

    ComboBoxItemDelegate* cbid = new ComboBoxItemDelegate(&tw);
    // ComboBox ony in column 2 
    tw.setItemDelegateForColumn(1, cbid);
    tw.setColumnCount(4);
    tw.setRowCount(10);
    tw.resize(600,400);
    tw.show();

    return a.exec();
}