QLabel as an item delegate
Hi,
I need to create an item delegate that shows the item data using a QLabel instead than a QEdit (the standard delegator for text data). The label need to show text (item value for example) in underline and blue and also show a different mouse pointer. See this image for example:
http://www.qlands.com/other_files/view.jpg
I can create a label that is blue, underline and has a different mouse pointer... But what do I need to do to use it in the delegate? I tried to follow the starDelegate example but I got lost in the paint().
Thanks for the help.
Carlos.
Re: QLabel as an item delegate
There is no such thing as "QEdit". The standard delegate draws everything by itself. If you want to use QLabel then you can use QAbstractItemView::setIndexWidget() but it is not advised since it has nothing to do with your model. It's better to implement a proper delegate, draw the text using QPainter::drawText() and handle events for cursor changes.
Re: QLabel as an item delegate
ok.
How can I handle the event for cursor changes? In the mouseEnter(), mouseLeave() of the delegator()?
Thanks.
Re: QLabel as an item delegate
Re: QLabel as an item delegate
Brilliant!
I guess I also need to reimplement sizeHint() ... How can I produce a value to return?
Maybe by giving the size of a normal QLabel holding the value:
Code:
{
tlabel.setText(index.data().toString());
return tlabel.size();
}
Re: QLabel as an item delegate
Re: QLabel as an item delegate
Yep.
I got that working after all.
Now the problem that I have is with the events enter and leave. Because editorEvent does not seem to receive such events from the view..
Here is the post:
http://www.qtcentre.org/threads/4383...er-leave-event
Thanks in advance.
Carlos.
Re: QLabel as an item delegate
Mouse tracking needs to be enabled for the view's viewport to receive such events, if I remember correctly.
Re: QLabel as an item delegate
Yes, I did enabled, thus I get mouse move but not enter/leave
Re: QLabel as an item delegate
Re: QLabel as an item delegate
Here is is:
labeldelegate.h
Code:
class labelDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
...
...
};
labeldelegate.cpp
Code:
{
if (event
->type
() == QEvent::MouseButtonDblClick) {
qDebug() << "TMouseButtonDblClick works fine";
return true;
}
if (event
->type
() == QEvent::Enter) //Does not work {
qDebug() << "Enter";
QCursor cursor
(Qt
::PointingHandCursor);
return true;
}
if (event
->type
() == QEvent::Leave) //Does not work. {
qDebug() << "Leave";
return true;
}
return false;
}
Many thanks
Re: QLabel as an item delegate
You may use QTableWidget and use setCellWidget to set a widget to cells of QTableWidget.
Re: QLabel as an item delegate
Hi,
As it is discussed before setCellWidget is not advised since it has nothing to do with the model.
Carlos.
Re: QLabel as an item delegate
Since the editorEvent() for the events you want is not called, providing code for editorEvent() is pointless :) Try providing a minimal compilable example reproducing the problem. The most interesting part is how you prepare the view widget.