/* table.cc */ #include "table.h" #include #include #include #include /* ---------------------------------------------------------------------- */ class MyTableDelegate : public QItemDelegate { public: virtual QWidget * createEditor (QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const; virtual void setEditorData (QWidget * param_editor, const QModelIndex & index) const; virtual void setModelData (QWidget * param_editor, QAbstractItemModel * model, const QModelIndex & index) const; /* virtual void updateEditorGeometry (QWidget * param_editor, const QStyleOptionViewItem & option, const QModelIndex &index) const; */ }; /* ---------------------------------------------------------------------- */ QWidget * MyTableDelegate::createEditor (QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const { QComboBox * editor = new QComboBox (parent); editor->addItem ("int"); editor->addItem ("float"); editor->addItem ("double"); editor->addItem ("bool"); editor->addItem ("string"); return editor; } void MyTableDelegate::setEditorData (QWidget * param_editor, const QModelIndex & index) const { QComboBox * editor = dynamic_cast < QComboBox * > (param_editor); assert (editor != NULL); QString value = index.model()->data(index, Qt::EditRole).toString(); editor->setEditText (value); } void MyTableDelegate::setModelData (QWidget * param_editor, QAbstractItemModel * model, const QModelIndex & index) const { QComboBox * editor = dynamic_cast < QComboBox * > (param_editor); assert (editor != NULL); QString value = editor->currentText(); model->setData (index, value, Qt::EditRole); } /* ---------------------------------------------------------------------- */ #if 0 QWidget * MyTableDelegate::createEditor (QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const { if (index.column() == 1) { QComboBox * editor = new QComboBox (parent); editor->addItem ("int"); editor->addItem ("float"); editor->addItem ("double"); editor->addItem ("bool"); editor->addItem ("string"); return editor; } else { return NULL; } } void MyTableDelegate::setEditorData (QWidget * param_editor, const QModelIndex & index) const { if (index.column() == 1) { QComboBox * editor = dynamic_cast < QComboBox * > (param_editor); assert (editor != NULL); QString value = index.model()->data(index, Qt::EditRole).toString(); editor->setEditText (value); } } void MyTableDelegate::setModelData (QWidget * param_editor, QAbstractItemModel * model, const QModelIndex & index) const { if (index.column() == 1) { QComboBox * editor = dynamic_cast < QComboBox * > (param_editor); assert (editor != NULL); QString value = editor->currentText(); model->setData (index, value, Qt::EditRole); } } #endif /* ---------------------------------------------------------------------- */ void MyTable::insertLine () { int line = this->currentRow (); int col = this->currentColumn (); if (line < 0) line = 0; if (col < 0) col = 0; this->insertRow (line); this->setCurrentCell (line, col); } void MyTable::removeLine () { int line = this->currentRow (); if (line >= 0) { this->removeRow (line); } } /* ---------------------------------------------------------------------- */ MyTable::MyTable (QWidget* parent) : QTableWidget (parent) { this->setColumnCount (8); this->setRowCount (10); this->setEditTriggers (QAbstractItemView::CurrentChanged); // this->setEditTriggers (QAbstractItemView::AllEditTriggers); // this->setItemDelegate (new MyTableDelegate); this->setItemDelegateForColumn (1, new MyTableDelegate); // !? delete QStringList list; list << "identifier" << "type"; this->setHorizontalHeaderLabels (list); // after setColumnCount /* context menu */ QAction * a = new QAction (NULL); a->setText ("insert line"); QObject::connect (a, SIGNAL(triggered()), this, SLOT(insertLine())); this->addAction (a); a = new QAction (NULL); a->setText ("remove line"); QObject::connect (a, SIGNAL(triggered()), this, SLOT(removeLine())); this->addAction (a); this->setContextMenuPolicy (Qt::ActionsContextMenu); /* items */ for (int i = 0; i < this->rowCount (); i++) for (int j = 0; j < this->columnCount (); j++) { QTableWidgetItem * item = new QTableWidgetItem (); this->setItem (i, j , item); } } /* ---------------------------------------------------------------------- */ // #include "table.moc"