Combo Boxes in Item Views/de: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Add "cleanup" tag)
(Sync with english side)
 
(4 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 as Editor-Widget in einem ItemView oder ItemWidget benutzt
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":http://doc.qt.io/qt-5.0/qtwidgets/qstyleditemdelegate.html 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.


Datei itemdelegate.h
Datei itemdelegate.h


<code>
<code>
// —— File itemdelegate.h ——
#ifndef COMBOBOXITEMDELEGATE_H
#ifndef ITEMDELEGATE_H
#define COMBOBOXITEMDELEGATE_H
#define ITEMDELEGATE_H


#include <QStyledItemDelegate>
#include <QStyledItemDelegate>
Line 23: Line 22:
class ComboBoxItemDelegate : public QStyledItemDelegate
class ComboBoxItemDelegate : public QStyledItemDelegate
{
{
Q_OBJECT
    Q_OBJECT
 
public:
public:
ComboBoxItemDelegate(QObject *parent = 0);
    using QStyledItemDelegate::QStyledItemDelegate;
~ComboBoxItemDelegate();
 
virtual QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index ) const;
virtual void setEditorData ( QWidget *editor, const QModelIndex &amp;index ) const;
virtual void setModelData ( QWidget *editor, QAbstractItemModel *model, const QModelIndex &amp;index ) const;


    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 // ITEMDELEGATE_H
#endif // COMBOBOXITEMDELEGATE_H
</code>
</code>


Datei itemdelegate.cpp
Datei comboboxitemdelegate.cpp


<code>
<code>
// —— File itemdelegate.cpp ——
#include "comboboxitemdelegate.h"
#include "itemdelegate.h"
#include <QComboBox>
#include <QComboBox>


ComboBoxItemDelegate::ComboBoxItemDelegate(QObject '''parent)
QWidget *ComboBoxItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
: QStyledItemDelegate(parent)
{
{
    // 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::~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);
}
}




QWidget''' ComboBoxItemDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index ) const
void ComboBoxItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
{
// ComboBox ony in column 2
    QComboBox *cb = qobject_cast<QComboBox *>(editor);
if(index.column() != 1)
    Q_ASSERT(cb);
return QStyledItemDelegate::createEditor(parent, option, index);
    model->setData(index, cb->currentText(), Qt::EditRole);
 
// 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;
}
}
</code>


void ComboBoxItemDelegate::setEditorData ( QWidget *editor, const QModelIndex &amp;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 itenm
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 &amp;index ) const
Dateimain.cpp
{
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);
}
</code>
 
Datei main.cpp


<code>
<code>
// —— File main.cpp ——
#include <QApplication>
#include <QApplication>
#include <QTableWidget>
#include <QTableWidget>


#include "itemdelegate.h"
#include "comboboxitemdelegate.h"


int main(int argc, char *argv[])
int main(int argc, char *argv[])
{
{
QApplication a(argc, argv);
    QApplication a(argc, argv);
QTableWidget tw;
    QTableWidget tw;


ComboBoxItemDelegate *cbid = new ComboBoxItemDelegate(&amp;tw);
    ComboBoxItemDelegate* cbid = new ComboBoxItemDelegate(&tw);
tw.setItemDelegate(cbid);
    // ComboBox only in column 2
tw.setColumnCount(4);
    tw.setItemDelegateForColumn(1, cbid);
tw.setRowCount(10);
    tw.setColumnCount(4);
tw.resize(600,400);
    tw.setRowCount(10);
tw.show();
    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();
}