Delegate
-
I have a simple MVC architecture with a QStandardItemModel and a QtableView.
I would like to define special behaviors for all items in column 1 and I do not know how to do it.
#include "test.h" #include"modeltest.h" //Test::Test(QWidget *parent) : QWidget(parent) {} /* *the Classe that contains * the tabView */ Test::Test() : QWidget() { QStandardItemModel *model = new QStandardItemModel(0, 2); QStringList list; list << "france" << "Angleterre" << "Allemagne" << "Italie"; for(int row = 0; row<4; ++row) { model->setItem(row, 0, new QStandardItem(static_cast<QString>(list.at(row)))); } QTableView *table = new QTableView; table->setModel(model); ModelTest *delegate; table->setItemDelegateForColumn(0, delegate); QVBoxLayout *vlayout = new QVBoxLayout; vlayout->addWidget(table); setLayout(vlayout); } /*Header of the class * that handle the delegate */ #ifndef MODELTEST_H #define MODELTEST_H #include <QtWidgets> #include<QObject> class ModelTest : public QItemDelegate { Q_OBJECT public: ModelTest(QObject *parent = 0); public: 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; void updateEditorGeometry(QWidget *editor, QStyleOptionViewItem &option, const QModelIndex &index) const; }; #endif // MODELTEST_H /*.cpp of the classe *that handle the *delegate */ #include "modeltest.h" ModelTest::ModelTest(QObject *parent) : QItemDelegate(parent) { } QWidget *ModelTest::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { QSplitter *splitter = new QSplitter; return splitter; } void ModelTest::setEditorData(QWidget *editor, const QModelIndex &index) const { QStringList list; QStringList list2; list<< "cameroun" << "tchad" << "congo"; list2 << "ivoire" << "argent" << "pain"; QStandardItemModel *model = new QStandardItemModel; QStandardItem *parent1 = new QStandardItem("Pays"); for(int row =0; row<3; ++row) { parent1->appendRow(new QStandardItem(static_cast<QString>(list.at(row)))); } QStandardItem *parent2 = new QStandardItem("Monnaie"); for(int row =0; row<3; ++row) { parent2->appendRow(new QStandardItem(static_cast<QString>(list2.at(row)))); } model->setItem(0, 0, parent1); model->setItem(1, 0, parent1); QTreeView *tree = new QTreeView(static_cast<QSplitter*>(editor)); tree->setModel(model); } void ModelTest::updateEditorGeometry(QWidget *editor, QStyleOptionViewItem &option, const QModelIndex &index) const { editor->setGeometry(option.rect); }
In fact the code compiles and executes but nothing happens as if I had not defined any delegate.
How can I do? Thank you for answering me
-
Hi,
What I really wanted to do is that. I fill both columns with a tab view with strings (in my case here I
filled only one column but it has no impact) then I modify the delegates only for the 1st column using setItemColumnDelegate. The modified delegate must (on the current index) make disappear the string that is in the index (make it invisible with setVisible (false)) then make appear a splitter (widget inheriting a frame therefore same style as the frame ) That contains two tree views of other strings -
Hi,
Just to be sure I understand you correctly. You want your delegate to show two QTreeViews put in a
QSliderQSplitter whenever you select a cell from your QTableView ?[edit: fixed question SGaist]
-
Yes but no QSlider but QSplitter. thanks to answering me :)
-
I have solved my problem. thanks for your attention :)
-
Great !
What did you implement ?
Also, since you have it working now, please mark the thread as solved using the "Topic Tools" button so that other forum uses may know a solution has been found :)