PDA

View Full Version : QComboBox in QTableView



giker
22nd December 2010, 15:48
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);
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);
}

but comboBox is not showing in the cells, and data too.
How can I draw the combobox in the cell?

giker
23rd December 2010, 11:39
Answer is


ui->tableView->openPersistentEditor(m_model->index(0, 2));