Combo Boxes in Item Views/de: Difference between revisions
Jump to navigation
Jump to search
AutoSpider (talk | contribs) (Decode HTML entity names) |
(Sync with english side) |
||
(2 intermediate revisions by 2 users not shown) | |||
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 15: | Line 15: | ||
<code> | <code> | ||
#ifndef COMBOBOXITEMDELEGATE_H | |||
#ifndef | #define COMBOBOXITEMDELEGATE_H | ||
#define | |||
#include <QStyledItemDelegate> | #include <QStyledItemDelegate> | ||
Line 23: | Line 22: | ||
class ComboBoxItemDelegate : public QStyledItemDelegate | class ComboBoxItemDelegate : public QStyledItemDelegate | ||
{ | { | ||
Q_OBJECT | |||
public: | public: | ||
using QStyledItemDelegate::QStyledItemDelegate; | |||
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 | ||
</code> | </code> | ||
Datei | Datei comboboxitemdelegate.cpp | ||
<code> | <code> | ||
#include "comboboxitemdelegate.h" | |||
#include " | |||
#include <QComboBox> | #include <QComboBox> | ||
ComboBoxItemDelegate:: | QWidget *ComboBoxItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const | ||
{ | { | ||
// Erzeugen und Füllen der ComboBox | |||
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; | |||
} | } | ||
ComboBoxItemDelegate:: | void ComboBoxItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const | ||
{ | { | ||
QComboBox *cb = qobject_cast<QComboBox *>(editor); | |||
Q_ASSERT(cb); | |||
// Suche nach dem aktuellen Wert welcher im Model gespeichert ist... | |||
const QString currentText = index.data(Qt::EditRole).toString(); | |||
const int cbIndex = cb->findText(currentText); | |||
// ... und setzen der Combobox auf diesen, falls vorhanden. | |||
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); | |||
} | } | ||
</code> | |||
Dateimain.cpp | |||
<code> | <code> | ||
#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(); | ||
} | } | ||
</code> |
Latest revision as of 18:10, 13 June 2019
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
#ifndef COMBOBOXITEMDELEGATE_H
#define COMBOBOXITEMDELEGATE_H
#include <QStyledItemDelegate>
class ComboBoxItemDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
using QStyledItemDelegate::QStyledItemDelegate;
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
Datei comboboxitemdelegate.cpp
#include "comboboxitemdelegate.h"
#include <QComboBox>
QWidget *ComboBoxItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
// Erzeugen und Füllen der ComboBox
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);
// Suche nach dem aktuellen Wert welcher im Model gespeichert ist...
const QString currentText = index.data(Qt::EditRole).toString();
const int cbIndex = cb->findText(currentText);
// ... und setzen der Combobox auf diesen, falls vorhanden.
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);
}
Dateimain.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();
}