#include <QtGui>#include "signaldelegate.h"SignalDelegate::SignalDelegate(int selSignalColumn, QObject *parent) : QItemDelegate(parent){ this->selSignalColumn = selSignalColumn;}QWidget *SignalDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const{ if (index.column() == selSignalColumn) { QComboBox *box = new QComboBox(parent); box->setEditable(true); box->setAutoCompletion(true); //box->setModel(const_cast<QAbstractItemModel*>(model)); box->setModelColumn(index.column()); box->installEventFilter(const_cast<SignalDelegate*>(this)); return box; } else { return QItemDelegate::createEditor(parent, option, index); }}void SignalDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const{ if (index.column() == selSignalColumn) { QComboBox* box = qobject_cast<QComboBox*>(editor); const QAbstractItemModel *model = index.model(); if (!box || !model) QItemDelegate::setEditorData(editor, index); box->setCurrentIndex(index.row()); } else { QItemDelegate::setEditorData(editor, index); }}void SignalDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{ if (index.column() == selSignalColumn) { QComboBox *box = qobject_cast<QComboBox*>(editor); if (!box) return QItemDelegate::setModelData(editor, model, index); model->setData(index, box->currentText(), Qt::DisplayRole); model->setData(index, box->currentText(), Qt::EditRole); } else { return QItemDelegate::setModelData(editor, model, index); }}
#ifndef SIGNALDELEGATE_H#define SIGNALDELEGATE_H#include <QItemDelegate>class SignalDelegate: public QItemDelegate{ Q_OBJECT public: SignalDelegate(int selSignalColumn, QObject *parent = 0); //void paint(QPainter *painter, const QStyleOptionViewItem &option, // const QModelIndex &index) const; QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; void setEditorData(QWidget *editor, const QModelIndex &index) const; void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;//private slots: //void commitAndCloseEditor(); private: int selSignalColumn;};#endif
QStandardItemModel model(4, 2); // Создадим табличное представление // и установим для него модель QTableView tableView; tableView.setModel(&model);
for (int row = 0; row < 4; row++) { // создаем строчки for (int column = 0; column < 2; column++) { // создаем колонки QModelIndex index = model.index(row, column, QModelIndex()); model.setData(index, QVariant((row) + (column)));// } }
QComboBox *cmbox = new QComboBox(parent); cmbox->addItem("0"); cmbox->addItem("1");
QWidget *SignalDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const{ if (index.column() == selSignalColumn) { QComboBox *box = new QComboBox(parent); box->setEditable(true); box->setAutoCompletion(true); box->addItem("Signal 2"); box->addItem("Signal 3"); box->installEventFilter(const_cast<SignalDelegate*>(this)); connect(box, SIGNAL(currentIndexChanged()), this, SLOT(commitAndCloseEditor())); /////// Вот этот конект return static_cast<QWidget *>(box); } else { return QItemDelegate::createEditor(parent, option, index); }}
void SignalDelegate::commitAndCloseEditor(){ QMessageBox msgBox; msgBox.setText(QString("%1").arg("msg")); msgBox.exec();}
box->addItem("Signal 2");
box->setCurrentIndex(arr[index.row()]);