Combo Boxes in Item Views/de: Difference between revisions
Jump to navigation
Jump to search
AutoSpider (talk | contribs) m (AutoSpider moved page Combo Boxes in Item Views German to Combo Boxes in Item Views/de: Localisation) |
m (added missing close code block tag) |
||
Line 8: | Line 8: | ||
= ComboBox in ItemView oder ItemWidget = | = ComboBox in ItemView oder ItemWidget = | ||
Beispiel-Code, wie man ComboBoxen | Beispiel-Code, wie man ComboBoxen als Editor-Widget in einem ItemView oder ItemWidget benutzt | ||
Das Delegate erzeugt eine ComboBox, falls der Index in der zweiten Spalte eines ListView ist. Bei allen anderen Spalten gibt es lediglich den Default-Editor zurück, der von [http://doc.qt.io/qt-5.0/qtwidgets/qstyleditemdelegate.html QStyledItemDelegate] erzeugt wird. | Das Delegate erzeugt eine ComboBox, falls der Index in der zweiten Spalte eines ListView ist. Bei allen anderen Spalten gibt es lediglich den Default-Editor zurück, der von [http://doc.qt.io/qt-5.0/qtwidgets/qstyleditemdelegate.html QStyledItemDelegate] erzeugt wird. | ||
Line 45: | Line 45: | ||
#include <QComboBox> | #include <QComboBox> | ||
ComboBoxItemDelegate::ComboBoxItemDelegate(QObject | ComboBoxItemDelegate::ComboBoxItemDelegate(QObject *parent) | ||
: QStyledItemDelegate(parent) | : QStyledItemDelegate(parent) | ||
{ | { | ||
Line 56: | Line 56: | ||
QWidget | QWidget *ComboBoxItemDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const | ||
{ | { | ||
// ComboBox | // ComboBox only in column 2 | ||
if(index.column() != 1) | if(index.column() != 1) | ||
return QStyledItemDelegate::createEditor(parent, option, index); | return QStyledItemDelegate::createEditor(parent, option, index); | ||
Line 73: | Line 73: | ||
void ComboBoxItemDelegate::setEditorData ( QWidget *editor, const QModelIndex &index ) const | void ComboBoxItemDelegate::setEditorData ( QWidget *editor, const QModelIndex &index ) const | ||
{ | { | ||
if(QComboBox | if(QComboBox *cb = qobject_cast<QComboBox*>(editor)) { | ||
// get the index of the text in the combobox that matches the current value of the item | |||
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 { | } else { | ||
QStyledItemDelegate::setEditorData(editor, index); | |||
} | } | ||
} | } | ||
Line 87: | Line 88: | ||
void ComboBoxItemDelegate::setModelData ( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const | void ComboBoxItemDelegate::setModelData ( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const | ||
{ | { | ||
if(QComboBox | 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 | } else { | ||
QStyledItemDelegate::setModelData(editor, model, index); | |||
} | |||
} | } | ||
</code> | </code> | ||
Line 118: | Line 120: | ||
return a.exec(); | return a.exec(); | ||
} | } | ||
</code> |
Revision as of 12:38, 9 June 2018
This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine. Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean. |
Deutsch | English
ComboBox in ItemView oder ItemWidget
Beispiel-Code, wie man ComboBoxen als Editor-Widget in einem ItemView oder ItemWidget benutzt
Das Delegate erzeugt eine ComboBox, falls der Index in der zweiten Spalte eines ListView ist. Bei allen anderen Spalten gibt es lediglich den Default-Editor zurück, der von QStyledItemDelegate erzeugt wird.
Datei itemdelegate.h
// —— 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
Datei itemdelegate.cpp
// —— 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 only 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 item
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);
}
}
Datei 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();
}