anbu01
11th July 2015, 13:09
Hi,
I need to show custom widget(lineEdit +PushButton) on double click of QTreeview so i have created my own Delegate but for some reason the widget is not shown.I have createEditor() method override in delegate.Here is a sample Code i have done.I have not implemented the paint method to render the data but my problem is the widget doesn't shown when create editor called.Guide me on this
MainWindow.cpp
QStandardItemModel *model = new QStandardItemModel;
for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 4; ++column) {
QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
model->setItem(row, column, item);
}
}
QTreeView *veiw = new QTreeView(this);
MyDelegate *del = new MyDelegate;
veiw->setItemDelegate(del);
veiw->setModel(model);
setCentralWidget(veiw);
MyDelegate.cpp
QWidget *MyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(index.column() == 0)
return new CustomWidget(parent);
if(index.column() == 1)
return new QComboBox(parent);
}
CustomWidget.cpp
CustomWidget::CustomWidget(QWidget *parent) : QWidget(parent)
{
QLineEdit *edit = new QLineEdit;
QPushButton *but = new QPushButton;
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(edit);
layout->addWidget(but);
setLayout(layout);
}
I need to show custom widget(lineEdit +PushButton) on double click of QTreeview so i have created my own Delegate but for some reason the widget is not shown.I have createEditor() method override in delegate.Here is a sample Code i have done.I have not implemented the paint method to render the data but my problem is the widget doesn't shown when create editor called.Guide me on this
MainWindow.cpp
QStandardItemModel *model = new QStandardItemModel;
for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 4; ++column) {
QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
model->setItem(row, column, item);
}
}
QTreeView *veiw = new QTreeView(this);
MyDelegate *del = new MyDelegate;
veiw->setItemDelegate(del);
veiw->setModel(model);
setCentralWidget(veiw);
MyDelegate.cpp
QWidget *MyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(index.column() == 0)
return new CustomWidget(parent);
if(index.column() == 1)
return new QComboBox(parent);
}
CustomWidget.cpp
CustomWidget::CustomWidget(QWidget *parent) : QWidget(parent)
{
QLineEdit *edit = new QLineEdit;
QPushButton *but = new QPushButton;
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(edit);
layout->addWidget(but);
setLayout(layout);
}