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
Jump to navigation
Jump to search
AutoSpider (talk | contribs) (Decode HTML entity names) |
m (Change code to nowiki tags for formatting) |
||
(6 intermediate revisions by 3 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. | 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 {{DocLink|QStyledItemDelegate}} creates. | ||
File | File comboboxitemdelegate.h | ||
< | <nowiki> | ||
#ifndef COMBOBOXITEMDELEGATE_H | |||
#ifndef | #define COMBOBOXITEMDELEGATE_H | ||
#define | |||
#include <QStyledItemDelegate> | #include <QStyledItemDelegate> | ||
Line 22: | Line 15: | ||
class ComboBoxItemDelegate : public QStyledItemDelegate | class ComboBoxItemDelegate : public QStyledItemDelegate | ||
{ | { | ||
Q_OBJECT | |||
public: | 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 // | #endif // COMBOBOXITEMDELEGATE_H | ||
</ | </nowiki> | ||
File | File comboboxitemdelegate.cpp | ||
< | <nowiki> | ||
#include "comboboxitemdelegate.h" | |||
#include " | |||
#include <QComboBox> | #include <QComboBox> | ||
ComboBoxItemDelegate::ComboBoxItemDelegate(QObject | ComboBoxItemDelegate::ComboBoxItemDelegate(QObject *parent) | ||
: QStyledItemDelegate(parent) | |||
{ | { | ||
} | } | ||
Line 55: | Line 45: | ||
QWidget | 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 | 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 | |||
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); | |||
} | } | ||
</ | </nowiki> | ||
File main.cpp | File main.cpp | ||
< | <nowiki> | ||
#include <QApplication> | #include <QApplication> | ||
#include <QTableWidget> | #include <QTableWidget> | ||
#include " | #include "comboboxitemdelegate.h" | ||
int main(int argc, char *argv[]) | int main(int argc, char *argv[]) | ||
{ | { | ||
QApplication a(argc, argv); | |||
QTableWidget tw; | |||
ComboBoxItemDelegate *cbid = new ComboBoxItemDelegate(&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(); | return a.exec(); | ||
} | } | ||
</nowiki> |
Latest revision as of 20:31, 24 May 2021
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(); }