Difference between revisions of "Combo Boxes in Item Views"

From Qt Wiki
Jump to: navigation, search
(Adjusted to current Qt style)
 
(10 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 +
{{LangSwitch}}
 
[[Category:Snippets]]
 
[[Category:Snippets]]
 +
Sample code to use combo boxes as editor widgets in an item view or item widget.
  
= Combo Boxes in Item Views =
+
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.
  
Sample code to use combo boxes as editor widgets in an item view or item widget.
+
File comboboxitemdelegate.h
  
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":http://doc.qt.io/qt-5.0/qtwidgets/qstyleditemdelegate.html creates.
+
<code>
 +
#ifndef COMBOBOXITEMDELEGATE_H
 +
#define COMBOBOXITEMDELEGATE_H
  
File itemdelegate.h
+
#include <QStyledItemDelegate>
  
<code><br />// —— File itemdelegate.h ——<br />#ifndef ITEMDELEGATE_H<br />#define ITEMDELEGATE_H
+
class ComboBoxItemDelegate : public QStyledItemDelegate
 +
{
 +
    Q_OBJECT
 +
public:
 +
    ComboBoxItemDelegate(QObject *parent = nullptr);
 +
    ~ComboBoxItemDelegate();
  
#include &lt;QStyledItemDelegate&amp;gt;
+
    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;
 +
};
  
class ComboBoxItemDelegate : public QStyledItemDelegate<br />{<br /> Q_OBJECT
+
#endif // COMBOBOXITEMDELEGATE_H
 +
</code>
  
public:<br /> ComboBoxItemDelegate(QObject *parent = 0);<br /> ~ComboBoxItemDelegate();
+
File comboboxitemdelegate.cpp
  
virtual QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index ) const;<br /> virtual void setEditorData ( QWidget *editor, const QModelIndex &amp;index ) const;<br /> virtual void setModelData ( QWidget *editor, QAbstractItemModel *model, const QModelIndex &amp;index ) const;
+
<code>
 +
#include "comboboxitemdelegate.h"
 +
#include <QComboBox>
  
};
+
ComboBoxItemDelegate::ComboBoxItemDelegate(QObject *parent)
 +
    : QStyledItemDelegate(parent)
 +
{
 +
}
  
#endif // ITEMDELEGATE_H<br /></code>
 
  
File itemdelegate.cpp
+
ComboBoxItemDelegate::~ComboBoxItemDelegate()
 +
{
 +
}
  
<code><br />// —— File itemdelegate.cpp ——<br />#include &quot;itemdelegate.h&amp;quot;<br />#include &lt;QComboBox&amp;gt;
 
  
ComboBoxItemDelegate::ComboBoxItemDelegate(QObject '''parent)<br /> : QStyledItemDelegate(parent)<br />{<br />}
+
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;
 +
}
  
<br />ComboBoxItemDelegate::~ComboBoxItemDelegate()<br />{<br />}
 
  
<br />QWidget''' ComboBoxItemDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index ) const<br />{<br /> // ComboBox ony in column 2<br /> if(index.column() != 1)<br /> return QStyledItemDelegate::createEditor(parent, option, index);
+
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);
 +
}
  
// Create the combobox and populate it<br /> QComboBox *cb = new QComboBox(parent);<br /> int row = index.row();<br /> cb-&gt;addItem(QString(&quot;one in row %1&amp;quot;).arg(row));<br /> cb-&gt;addItem(QString(&quot;two in row %1&amp;quot;).arg(row));<br /> cb-&gt;addItem(QString(&quot;three in row %1&amp;quot;).arg(row));<br /> return cb;<br />}
 
  
void ComboBoxItemDelegate::setEditorData ( QWidget *editor, const QModelIndex &amp;index ) const<br />{<br /> if(QComboBox '''cb = qobject_cast&amp;lt;QComboBox'''&gt;(editor)) {<br /> // get the index of the text in the combobox that matches the current value of the itenm<br /> QString currentText = index.data(Qt::EditRole).toString();<br /> int cbIndex = cb-&gt;findText(currentText);<br /> // if it is valid, adjust the combobox<br /> if(cbIndex &gt;= 0)<br /> cb-&gt;setCurrentIndex(cbIndex);<br /> } else {<br /> QStyledItemDelegate::setEditorData(editor, index);<br /> }<br />}
+
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);
 +
}
 +
</code>
  
void ComboBoxItemDelegate::setModelData ( QWidget *editor, QAbstractItemModel *model, const QModelIndex &amp;index ) const<br />{<br /> if(QComboBox '''cb = qobject_cast&amp;lt;QComboBox'''&gt;(editor))<br /> // save the current text of the combo box as the current value of the item<br /> model-&gt;setData(index, cb-&gt;currentText(), Qt::EditRole);<br /> else<br /> QStyledItemDelegate::setModelData(editor, model, index);<br />}<br /></code>
 
  
 
File main.cpp
 
File main.cpp
  
<code><br />// —— File main.cpp ——<br />#include &lt;QApplication&amp;gt;<br />#include &lt;QTableWidget&amp;gt;
+
<code>
 +
#include <QApplication>
 +
#include <QTableWidget>
  
#include &quot;itemdelegate.h&amp;quot;
+
#include "comboboxitemdelegate.h"
  
int main(int argc, char *argv[])<br />{<br /> QApplication a(argc, argv);<br /> QTableWidget tw;
+
int main(int argc, char *argv[])
 +
{
 +
    QApplication a(argc, argv);
 +
    QTableWidget tw;
  
ComboBoxItemDelegate *cbid = new ComboBoxItemDelegate(&amp;tw);<br /> tw.setItemDelegate(cbid);<br /> tw.setColumnCount(4);<br /> tw.setRowCount(10);<br /> tw.resize(600,400);<br /> tw.show();
+
    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&amp;amp;#40;&amp;#41;;<br />}
+
    return a.exec();
 +
}
 +
</code>

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();
}