Добрый день...
В общем нужно создать свой делегат для модели таблице.....
В общем то вот то что я сделал
#pragma once
#include <QtGui>
class ComboBoxDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
ComboBoxDelegate(QObject *parent = 0);
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index)const;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
};
реализация
#include "ComboBoxDelegate.h"
#include <QtGui>
const int numberCom = 10;
ComboBoxDelegate::ComboBoxDelegate(QObject *parent) : QStyledItemDelegate(parent)
{}
QWidget * ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStringList strList;
QString text;
for (int i = 1; i <= numberCom; ++i)
{
text = QString("COM%1").arg(i);
strList << text;
}
QComboBox *editor = new QComboBox(parent);
editor->addItems(strList);
editor->setEditable(false);
return editor;
}
void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index)const
{
QComboBox *box = static_cast<QComboBox*>(editor);
QString tempStr = box->currentText();
model->setData(index, tempStr, Qt::DisplayRole);
}
void ComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index)const
{
QStyleOptionViewItemV4 optV4(option);
this->initStyleOption(&optV4, index);
QStyleOptionComboBox cmbOpt;
QString text = "COM1";
cmbOpt.currentText = text;
cmbOpt.rect = optV4.rect;
cmbOpt.state = QStyle::State_Enabled;
QApplication::style()->drawControl(QStyle::CE_ComboBoxLabel, &cmbOpt, painter);
}
Создаю маленькую табличку
#ifndef TEST1_H
#define TEST1_H
#include "ComboBoxDelegate.h"
#include <QtGui>
class test1 : public QDialog
{
Q_OBJECT
public:
test1(QWidget *parent = 0);
~test1();
private:
QTableView *myTable;
QStandardItemModel *tabModel;
ComboBoxDelegate *comCheckBoxDel;
QHBoxLayout* mainLayout;
};
#endif // TEST1_H
реализация
#include "test1.h"
test1::test1(QWidget *parent) : QDialog(parent)
{
myTable = new QTableView;
tabModel = new QStandardItemModel(2,2);
QModelIndex index = tabModel->index(0,0);
QString str1 = "Change port";
tabModel->setData(index, str1, Qt::DisplayRole);
myTable->setModel(tabModel);
comCheckBoxDel = new ComboBoxDelegate;
myTable->setItemDelegateForColumn(1, comCheckBoxDel);
myTable->horizontalHeader()->setStretchLastSection(true);
mainLayout = new QHBoxLayout;
mainLayout->addWidget(myTable);
setLayout(mainLayout);
}
test1::~test1()
{
}
Получается что как только я запускаю приложение - в ячейках для которых установлен этот делегат - данные просто отображаются, а не в виде комбобокса, но как только я кликаю по комбобоксу - включается режим редактирования - и все предстает в виде комбобокса....как исправить???
можно сделать искусственно - чтоб всегда был режим редактирования:
QModelIndex index1 = tabModel->index(0,1);
QModelIndex index2 = tabModel->index(1,1);
myTable->openPersistentEditor(index1);
myTable->openPersistentEditor(index2);
Но я хочу чтобы все это было реализовано в рамках самого делегата....