Combo Boxes in Item Views: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
=Combo Boxes in Item Views= | [[Category:Snippets]] | ||
= 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 | 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. | ||
File itemdelegate.h | File itemdelegate.h | ||
<code><br />// —— File itemdelegate.h ——<br />#ifndef ITEMDELEGATE_H<br />#define ITEMDELEGATE_H | |||
#include <QStyledItemDelegate&gt; | |||
class ComboBoxItemDelegate : public QStyledItemDelegate<br />{<br /> Q_OBJECT | |||
public:<br /> ComboBoxItemDelegate(QObject *parent = 0);<br /> ~ComboBoxItemDelegate(); | |||
virtual QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const;<br /> virtual void setEditorData ( QWidget *editor, const QModelIndex &index ) const;<br /> virtual void setModelData ( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const; | |||
}; | |||
#endif // ITEMDELEGATE_H<br /></code> | |||
File itemdelegate.cpp | File itemdelegate.cpp | ||
<code><br />// —— File itemdelegate.cpp ——<br />#include "itemdelegate.h&quot;<br />#include <QComboBox&gt; | |||
ComboBoxItemDelegate::ComboBoxItemDelegate(QObject '''parent)<br /> : QStyledItemDelegate(parent)<br />{<br />} | |||
<br />ComboBoxItemDelegate::~ComboBoxItemDelegate()<br />{<br />} | |||
<br />QWidget''' ComboBoxItemDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const<br />{<br /> // ComboBox ony in column 2<br /> if(index.column() != 1)<br /> return QStyledItemDelegate::createEditor(parent, option, index); | |||
// Create the combobox and populate it<br /> QComboBox *cb = new QComboBox(parent);<br /> int row = index.row();<br /> cb->addItem(QString("one in row %1&quot;).arg(row));<br /> cb->addItem(QString("two in row %1&quot;).arg(row));<br /> cb->addItem(QString("three in row %1&quot;).arg(row));<br /> return cb;<br />} | |||
void ComboBoxItemDelegate::setEditorData ( QWidget *editor, const QModelIndex &index ) const<br />{<br /> if(QComboBox '''cb = qobject_cast&lt;QComboBox'''>(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->findText(currentText);<br /> // if it is valid, adjust the combobox<br /> if(cbIndex >= 0)<br /> cb->setCurrentIndex(cbIndex);<br /> } else {<br /> QStyledItemDelegate::setEditorData(editor, index);<br /> }<br />} | |||
void ComboBoxItemDelegate::setModelData ( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const<br />{<br /> if(QComboBox '''cb = qobject_cast&lt;QComboBox'''>(editor))<br /> // save the current text of the combo box as the current value of the item<br /> model->setData(index, cb->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 <QApplication&gt;<br />#include <QTableWidget&gt; | ||
#include "itemdelegate.h&quot; | |||
int main(int argc, char *argv[])<br />{<br /> QApplication a(argc, argv);<br /> QTableWidget tw; | |||
ComboBoxItemDelegate *cbid = new ComboBoxItemDelegate(&tw);<br /> tw.setItemDelegate(cbid);<br /> tw.setColumnCount(4);<br /> tw.setRowCount(10);<br /> tw.resize(600,400);<br /> tw.show(); | |||
return a.exec&amp;#40;&#41;;<br />} |
Revision as of 14:38, 23 February 2015
Combo Boxes in Item Views
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":http://doc.qt.io/qt-5.0/qtwidgets/qstyleditemdelegate.html creates.
File itemdelegate.h
<br />// —— File itemdelegate.h ——<br />#ifndef ITEMDELEGATE_H<br />#define ITEMDELEGATE_H
#include <QStyledItemDelegate&gt;
class ComboBoxItemDelegate : public QStyledItemDelegate<br />{<br /> Q_OBJECT
public:<br /> ComboBoxItemDelegate(QObject *parent = 0);<br /> ~ComboBoxItemDelegate();
virtual QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const;<br /> virtual void setEditorData ( QWidget *editor, const QModelIndex &index ) const;<br /> virtual void setModelData ( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const;
};
#endif // ITEMDELEGATE_H<br />
File itemdelegate.cpp
<br />// —— File itemdelegate.cpp ——<br />#include "itemdelegate.h&quot;<br />#include <QComboBox&gt;
ComboBoxItemDelegate::ComboBoxItemDelegate(QObject '''parent)<br /> : QStyledItemDelegate(parent)<br />{<br />}
<br />ComboBoxItemDelegate::~ComboBoxItemDelegate()<br />{<br />}
<br />QWidget''' ComboBoxItemDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const<br />{<br /> // ComboBox ony in column 2<br /> if(index.column() != 1)<br /> return QStyledItemDelegate::createEditor(parent, option, index);
// Create the combobox and populate it<br /> QComboBox *cb = new QComboBox(parent);<br /> int row = index.row();<br /> cb->addItem(QString("one in row %1&quot;).arg(row));<br /> cb->addItem(QString("two in row %1&quot;).arg(row));<br /> cb->addItem(QString("three in row %1&quot;).arg(row));<br /> return cb;<br />}
void ComboBoxItemDelegate::setEditorData ( QWidget *editor, const QModelIndex &index ) const<br />{<br /> if(QComboBox '''cb = qobject_cast&lt;QComboBox'''>(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->findText(currentText);<br /> // if it is valid, adjust the combobox<br /> if(cbIndex >= 0)<br /> cb->setCurrentIndex(cbIndex);<br /> } else {<br /> QStyledItemDelegate::setEditorData(editor, index);<br /> }<br />}
void ComboBoxItemDelegate::setModelData ( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const<br />{<br /> if(QComboBox '''cb = qobject_cast&lt;QComboBox'''>(editor))<br /> // save the current text of the combo box as the current value of the item<br /> model->setData(index, cb->currentText(), Qt::EditRole);<br /> else<br /> QStyledItemDelegate::setModelData(editor, model, index);<br />}<br />
File main.cpp
// —— 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&#40;);
}