PDA

View Full Version : QLabel as an item delegate



qlands
11th August 2011, 09:02
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.

wysota
11th August 2011, 09:05
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.

qlands
11th August 2011, 09:18
ok.

How can I handle the event for cursor changes? In the mouseEnter(), mouseLeave() of the delegator()?

Thanks.

wysota
11th August 2011, 09:25
In editorEvent()

qlands
11th August 2011, 09:39
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:



QSize labelDelegate::sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QLabel tlabel;
tlabel.setText(index.data().toString());
return tlabel.size();
}

wysota
11th August 2011, 13:11
QFontMetrics

qlands
11th August 2011, 13:24
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/43838-delegate-editorEvent-does-not-receive-enter-leave-event

Thanks in advance.
Carlos.

wysota
11th August 2011, 15:32
Mouse tracking needs to be enabled for the view's viewport to receive such events, if I remember correctly.

qlands
11th August 2011, 15:41
Yes, I did enabled, thus I get mouse move but not enter/leave

wysota
11th August 2011, 16:39
Show your code.

qlands
12th August 2011, 08:28
Here is is:

labeldelegate.h


class labelDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
...
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);

...

};


labeldelegate.cpp


bool labelDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &, const QModelIndex &index)
{
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);
QApplication::setOverrideCursor(cursor);
return true;
}
if (event->type() == QEvent::Leave) //Does not work.
{
qDebug() << "Leave";
QCursor cursor(Qt::ArrowCursor);
QApplication::setOverrideCursor(cursor);
return true;
}
return false;
}


Many thanks

cplus
12th August 2011, 09:41
You may use QTableWidget and use setCellWidget to set a widget to cells of QTableWidget.

qlands
12th August 2011, 09:45
Hi,

As it is discussed before setCellWidget is not advised since it has nothing to do with the model.

Carlos.

wysota
12th August 2011, 11:51
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.