1 Attachment(s)
custom widget in delegate
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
Code:
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);
MyDelegate.cpp
Code:
{
if(index.column() == 0)
return new CustomWidget(parent);
if(index.column() == 1)
}
CustomWidget.cpp
Code:
{
layout->addWidget(edit);
layout->addWidget(but);
setLayout(layout);
}
Re: custom widget in delegate
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);
}
Re: custom widget in delegate
@prasad:
Thanks ill give a try.
Re: custom widget in delegate
What are the edit triggers for your view?
Re: custom widget in delegate
@wysto:
QAbstractItemView::AllEditTriggers
Re: custom widget in delegate
Does your createEditor() method get called at all?
Re: custom widget in delegate
@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 .
Re: custom widget in delegate
Maybe it is a matter of setting size policies right?
Re: custom widget in delegate
@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.
Re: custom widget in delegate
Well... adjust the size policies of each widget and see if it changes anything :)
Re: custom widget in delegate
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.
1 Attachment(s)
Re: custom widget in delegate
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.
Re: custom widget in delegate
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