I need to show some cells in QTableView as QComboBox, even when the tableView no in edit mode. I create my class for it that inherits QStyledItemDelegateClass and reimplement paint method:
//comboboxdelegate.h
#ifndef COMBOBOXDELEGATE_H
#define COMBOBOXDELEGATE_H
#include <QStyledItemDelegate>
#include <QComboBox>
class ComboBoxDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
ComboBoxDelegate
(QObject *parent
= 0);
private:
};
#endif // COMBOBOXDELEGATE_H
//comboboxdelegate.cpp
#include "comboboxdelegate.h"
#include <QComboBox>
#include <QApplication>
ComboBoxDelegate
::ComboBoxDelegate(QObject *parent
) : QStyledItemDelegate(parent)
{
}
{
QString value
= index.
data().
toString();
comboBoxOption.rect = option.rect;
comboBoxOption.currentText = value;
comboBoxOption.
state = QStyle::State_Enabled;
}
//comboboxdelegate.h
#ifndef COMBOBOXDELEGATE_H
#define COMBOBOXDELEGATE_H
#include <QStyledItemDelegate>
#include <QComboBox>
class ComboBoxDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
ComboBoxDelegate(QObject *parent = 0);
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setItems(const QStringList &itemsList);
private:
QStringList m_itemsList;
};
#endif // COMBOBOXDELEGATE_H
//comboboxdelegate.cpp
#include "comboboxdelegate.h"
#include <QComboBox>
#include <QApplication>
ComboBoxDelegate::ComboBoxDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
}
void ComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QString value = index.data().toString();
QStyleOptionComboBox comboBoxOption;
comboBoxOption.rect = option.rect;
comboBoxOption.currentText = value;
comboBoxOption.state = QStyle::State_Enabled;
QApplication::style()->drawComplexControl(QStyle::CC_ComboBox, &comboBoxOption, painter);
}
To copy to clipboard, switch view to plain text mode
but comboBox is not showing in the cells, and data too.
How can I draw the combobox in the cell?
Bookmarks