PDA

View Full Version : custom widget in delegate



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);
}

prasad_N
11th July 2015, 15:19
we need to implement
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) also, Implement & try if you did not.

Added after 1 43 minutes:

I tried changing your example. I could see custom editor at least with below method implementation (but at wrong place because I am passing hard coded rect).
May be you could try pass proper geometry and try.

void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
QRect rect = option.rect;
rect.adjust(0, 0,100, 50);
editor->setGeometry(rect);
}

anbu01
11th July 2015, 16:26
@prasad:
Thanks ill give a try.

wysota
11th July 2015, 17:01
What are the edit triggers for your view?

anbu01
12th July 2015, 08:10
@wysto:
QAbstractItemView::AllEditTriggers

wysota
12th July 2015, 09:09
Does your createEditor() method get called at all?

anbu01
12th July 2015, 10:18
@wysota:
it gets called and when i re-implement updateEditorGeometry(as suggested by prasad) the widget appears(may be not within the cell) but my doubt is why it doesnt appear before but when i use QSpinbox or QLineEdit then without updateEditorGeometry() the widget comes perfect within the cell.still confused on how to use custom widget with tree view .

wysota
12th July 2015, 10:24
Maybe it is a matter of setting size policies right?

anbu01
12th July 2015, 10:51
@wysota:
yes it should be,so how do we overcome this now where i need to change so that widget occupies the whole cell exactly.

wysota
12th July 2015, 11:39
Well... adjust the size policies of each widget and see if it changes anything :)

prasad_N
12th July 2015, 19:21
Yes, I think its problem with size policies only, I showed CustomWidget and try to minimize width & height to zero But I could not do it.
I think the minimum size of the CustomWidget is being much greater than cell size, if we can make minimum size of CustomWidget is to (0,0) & max size to default the it will be visible in cell.

anbu01
13th July 2015, 04:32
I don't think so the problem is with size policy because the widget is visible only when i give rect.adjust()(as suggested by prasad) so the point(x,y) of the widget is the problem.I also tried with different size policy but still widget is not visible.So any suggestion how can i proceed further.

anbu01
13th July 2015, 08:56
Thanks for your help guys,the problem was with the layout spacing.Once we set Layout Spacing and margin to 0 .The widget came perfectly