Difference between revisions of "Combo Boxes in Item Views"

From Qt Wiki
Jump to: navigation, search
(Cleanup)
(Adjusted to current Qt style)
 
(4 intermediate revisions by one other user not shown)
Line 5: Line 5:
 
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 {{DocLink|QStyledItemDelegate}} creates.
 
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 {{DocLink|QStyledItemDelegate}} creates.
  
File itemdelegate.h
+
File comboboxitemdelegate.h
  
 
<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 = 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 override;
     virtual void setEditorData(QWidget* editor, const QModelIndex& index) const;
+
     void setEditorData(QWidget *editor, const QModelIndex &index) const override;
     virtual void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const;
+
     void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
 
};
 
};
  
#endif // ITEMDELEGATE_H
+
#endif // COMBOBOXITEMDELEGATE_H
 
</code>
 
</code>
  
File itemdelegate.cpp
+
File comboboxitemdelegate.cpp
  
 
<code>
 
<code>
#include "itemdelegate.h"
+
#include "comboboxitemdelegate.h"
 
#include <QComboBox>
 
#include <QComboBox>
  
ComboBoxItemDelegate::ComboBoxItemDelegate(QObject* parent)
+
ComboBoxItemDelegate::ComboBoxItemDelegate(QObject *parent)
 
     : QStyledItemDelegate(parent)
 
     : QStyledItemDelegate(parent)
 
{
 
{
Line 45: Line 45:
  
  
QWidget* ComboBoxItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
+
QWidget *ComboBoxItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
 
{
 
{
    // ComboBox ony in column 2
 
    if (index.column() != 1)
 
        return QStyledItemDelegate::createEditor(parent, option, index);
 
 
 
     // Create the combobox and populate it
 
     // Create the combobox and populate it
     QComboBox* cb = new QComboBox(parent);
+
     QComboBox *cb = new QComboBox(parent);
     int row = index.row();
+
     const int row = index.row();
 
     cb->addItem(QString("one in row %1").arg(row));
 
     cb->addItem(QString("one in row %1").arg(row));
 
     cb->addItem(QString("two in row %1").arg(row));
 
     cb->addItem(QString("two in row %1").arg(row));
Line 61: Line 57:
  
  
void ComboBoxItemDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
+
void ComboBoxItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
 
{
 
{
     if (QComboBox* cb = qobject_cast<QComboBox*>(editor)) {
+
     QComboBox *cb = qobject_cast<QComboBox *>(editor);
      // get the index of the text in the combobox that matches the current value of the itenm
+
    Q_ASSERT(cb);
      QString currentText = index.data(Qt::EditRole).toString();
+
    // get the index of the text in the combobox that matches the current value of the item
      int cbIndex = cb->findText(currentText);
+
    const QString currentText = index.data(Qt::EditRole).toString();
      // if it is valid, adjust the combobox
+
    const int cbIndex = cb->findText(currentText);
      if (cbIndex >= 0)
+
    // if it is valid, adjust the combobox
          cb->setCurrentIndex(cbIndex);
+
    if (cbIndex >= 0)
    } else {
+
      cb->setCurrentIndex(cbIndex);
        QStyledItemDelegate::setEditorData(editor, index);
 
    }
 
 
}
 
}
  
  
void ComboBoxItemDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
+
void ComboBoxItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
 
{
 
{
     if (QComboBox* cb = qobject_cast<QComboBox*>(editor))
+
     QComboBox *cb = qobject_cast<QComboBox *>(editor);
        // save the current text of the combo box as the current value of the item
+
    Q_ASSERT(cb);
        model->setData(index, cb->currentText(), Qt::EditRole);
+
    model->setData(index, cb->currentText(), Qt::EditRole);
    else
 
        QStyledItemDelegate::setModelData(editor, model, index);
 
 
}
 
}
 
</code>
 
</code>
Line 93: Line 85:
 
#include <QTableWidget>
 
#include <QTableWidget>
  
#include "itemdelegate.h"
+
#include "comboboxitemdelegate.h"
  
 
int main(int argc, char *argv[])
 
int main(int argc, char *argv[])
Line 101: Line 93:
  
 
     ComboBoxItemDelegate* cbid = new ComboBoxItemDelegate(&tw);
 
     ComboBoxItemDelegate* cbid = new ComboBoxItemDelegate(&tw);
     tw.setItemDelegate(cbid);
+
    // ComboBox only in column 2
 +
     tw.setItemDelegateForColumn(1, cbid);
 
     tw.setColumnCount(4);
 
     tw.setColumnCount(4);
 
     tw.setRowCount(10);
 
     tw.setRowCount(10);

Latest revision as of 13:13, 5 February 2019

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 comboboxitemdelegate.h

#ifndef COMBOBOXITEMDELEGATE_H
#define COMBOBOXITEMDELEGATE_H

#include <QStyledItemDelegate>

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

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

#endif // COMBOBOXITEMDELEGATE_H

File comboboxitemdelegate.cpp

#include "comboboxitemdelegate.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);
    const 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 item
    const QString currentText = index.data(Qt::EditRole).toString();
    const 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 "comboboxitemdelegate.h"

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

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

    return a.exec();
}