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
for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 4; ++column) {
model->setItem(row, column, item);
}
}
MyDelegate *del = new MyDelegate;
veiw->setItemDelegate(del);
veiw->setModel(model);
setCentralWidget(veiw);
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);
To copy to clipboard, switch view to plain text mode
MyDelegate.cpp
{
if(index.column() == 0)
return new CustomWidget(parent);
if(index.column() == 1)
}
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);
}
To copy to clipboard, switch view to plain text mode
CustomWidget.cpp
{
layout->addWidget(edit);
layout->addWidget(but);
setLayout(layout);
}
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);
}
To copy to clipboard, switch view to plain text mode
Bookmarks