Reputation: 327
I have derived a class from QTableView. and I have Promoted the Widget QTableViewto the derived class in Qt creator. and try to implement the Custom delegate in the derived class, which does not seem to work. But when I demote the QTableView Widget to QTableView. Custom delegate works.
I tried to go through a documentation but could not find any solution. Am I missing anything?
Update:
I have checked the Paint() method is called but not createEditor method.
You can find the code below.
Geometry.cpp (constructor of the class derived from QTableView)
Geometry::Geometry(QWidget *parent) :
QTableView(parent)
{
this->setAcceptDrops(true);
this->setSelectionMode(QAbstractItemView::ExtendedSelection);
this->setContextMenuPolicy(Qt::CustomContextMenu);
this->setEditTriggers(QAbstractItemView::NoEditTriggers);
grpModel = new QStandardItemModel();
grpModel->setHorizontalHeaderItem(0,new QStandardItem ("Geometry part"));
grpModel->setHorizontalHeaderItem(0,new QStandardItem ("Surface property"));
this->setModel(grpModel);
}
MyDelegate.cpp (Definition of custom delegate)
QWidget* MyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
//Definition
this method is not called
}
void MyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
//definition
}
void MyDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
//definition
}
void MyDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyledItemDelegate::paint(painter,option,index); // This method is called
}
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->tableGeometry->setModel(grpModel); //tabelGeometry is promoted to Geometry
comboDelegate = new MyDelegate();
ui->tableGeometry->setItemDelegate(comboDelegate);
}
Upvotes: 2
Views: 627
Reputation: 327
The solution was that
I have removed this->setEditTriggers(QAbstractItemView::NoEditTriggers);
from the constructor of the class Geometry.cpp. What i have understood that delegate was attached but not called because Items must be editable for the delegate to be called.
Upvotes: 3