Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

Combo Boxes in Item Views: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Decode HTML entity names)
(Cleanup)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
{{LangSwitch}}
 
[[Category:Snippets]]
[[Category:Snippets]]
'''English''' | [[Combo_Boxes_in_Item_Views_German|Deutsch]]
= Combo Boxes in Item Views =
Sample code to use combo boxes as editor widgets in an item view or item widget.
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 [http://doc.qt.io/qt-5.0/qtwidgets/qstyleditemdelegate.html 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 itemdelegate.h


<code>
<code>
// —— File itemdelegate.h ——
#ifndef ITEMDELEGATE_H
#ifndef ITEMDELEGATE_H
#define ITEMDELEGATE_H
#define ITEMDELEGATE_H
Line 22: Line 15:
class ComboBoxItemDelegate : public QStyledItemDelegate
class ComboBoxItemDelegate : public QStyledItemDelegate
{
{
Q_OBJECT
    Q_OBJECT
 
public:
public:
ComboBoxItemDelegate(QObject *parent = 0);
    ComboBoxItemDelegate(QObject* parent=0);
~ComboBoxItemDelegate();
    ~ComboBoxItemDelegate();
 
virtual QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
virtual void setEditorData ( QWidget *editor, const QModelIndex &index ) const;
virtual void setModelData ( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const;


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


Line 40: Line 31:


<code>
<code>
// —— File itemdelegate.cpp ——
#include "itemdelegate.h"
#include "itemdelegate.h"
#include <QComboBox>
#include <QComboBox>


ComboBoxItemDelegate::ComboBoxItemDelegate(QObject '''parent)
ComboBoxItemDelegate::ComboBoxItemDelegate(QObject* parent)
: QStyledItemDelegate(parent)
    : QStyledItemDelegate(parent)
{
{
}
}
Line 55: 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
    // ComboBox ony in column 2
if(index.column() != 1)
    if (index.column() != 1)
return QStyledItemDelegate::createEditor(parent, option, index);
        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();
    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));
cb->addItem(QString("three in row %1").arg(row));
    cb->addItem(QString("three in row %1").arg(row));
return cb;
    return cb;
}
}


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)) {
    if (QComboBox* cb = qobject_cast<QComboBox*>(editor)) {
// get the index of the text in the combobox that matches the current value of the itenm
      // get the index of the text in the combobox that matches the current value of the itenm
QString currentText = index.data(Qt::EditRole).toString();
      QString currentText = index.data(Qt::EditRole).toString();
int cbIndex = cb->findText(currentText);
      int cbIndex = cb->findText(currentText);
// if it is valid, adjust the combobox
      // if it is valid, adjust the combobox
if(cbIndex >= 0)
      if (cbIndex >= 0)
cb->setCurrentIndex(cbIndex);
          cb->setCurrentIndex(cbIndex);
} else {
    } else {
QStyledItemDelegate::setEditorData(editor, index);
        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))
    if (QComboBox* cb = qobject_cast<QComboBox*>(editor))
// save the current text of the combo box as the current value of the item
        // save the current text of the combo box as the current value of the item
model->setData(index, cb->currentText(), Qt::EditRole);
        model->setData(index, cb->currentText(), Qt::EditRole);
else
    else
QStyledItemDelegate::setModelData(editor, model, index);
        QStyledItemDelegate::setModelData(editor, model, index);
}
}
</code>
</code>


File main.cpp
File main.cpp


<code>
<code>
// —— File main.cpp ——
#include <QApplication>
#include <QApplication>
#include <QTableWidget>
#include <QTableWidget>
Line 105: Line 97:
int main(int argc, char *argv[])
int main(int argc, char *argv[])
{
{
QApplication a(argc, argv);
    QApplication a(argc, argv);
QTableWidget tw;
    QTableWidget tw;


ComboBoxItemDelegate *cbid = new ComboBoxItemDelegate(&tw);
    ComboBoxItemDelegate* cbid = new ComboBoxItemDelegate(&tw);
tw.setItemDelegate(cbid);
    tw.setItemDelegate(cbid);
tw.setColumnCount(4);
    tw.setColumnCount(4);
tw.setRowCount(10);
    tw.setRowCount(10);
tw.resize(600,400);
    tw.resize(600,400);
tw.show();
    tw.show();


return a.exec();
    return a.exec();
}
}
</code>

Revision as of 12:44, 28 June 2015

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 ITEMDELEGATE_H
#define ITEMDELEGATE_H

#include <QStyledItemDelegate>

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

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

#endif // ITEMDELEGATE_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
{
    // ComboBox ony in column 2
    if (index.column() != 1)
        return QStyledItemDelegate::createEditor(parent, option, index);

    // 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
{
    if (QComboBox* cb = qobject_cast<QComboBox*>(editor)) {
       // 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);
    } else {
        QStyledItemDelegate::setEditorData(editor, index);
    }
}


void ComboBoxItemDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
{
    if (QComboBox* cb = qobject_cast<QComboBox*>(editor))
        // save the current text of the combo box as the current value of the item
        model->setData(index, cb->currentText(), Qt::EditRole);
    else
        QStyledItemDelegate::setModelData(editor, model, index);
}


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);
    tw.setItemDelegate(cbid);
    tw.setColumnCount(4);
    tw.setRowCount(10);
    tw.resize(600,400);
    tw.show();

    return a.exec();
}