Results 1 to 14 of 14

Thread: QLabel as an item delegate

  1. #1
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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:



    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QLabel as an item delegate

    ok.

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

    Thanks.
    Last edited by qlands; 11th August 2011 at 09:23.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QLabel as an item delegate

    In editorEvent()
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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:

    Qt Code:
    1. QSize labelDelegate::sizeHint(const QStyleOptionViewItem &option,
    2. const QModelIndex &index) const
    3. {
    4. QLabel tlabel;
    5. tlabel.setText(index.data().toString());
    6. return tlabel.size();
    7. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by qlands; 11th August 2011 at 09:44.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QLabel as an item delegate

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QLabel as an item delegate

    Yes, I did enabled, thus I get mouse move but not enter/leave

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QLabel as an item delegate

    Show your code.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QLabel as an item delegate

    Here is is:

    labeldelegate.h
    Qt Code:
    1. class labelDelegate : public QStyledItemDelegate
    2. {
    3. Q_OBJECT
    4. public:
    5. ...
    6. bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
    7.  
    8. ...
    9.  
    10. };
    To copy to clipboard, switch view to plain text mode 

    labeldelegate.cpp
    Qt Code:
    1. bool labelDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &, const QModelIndex &index)
    2. {
    3. if (event->type() == QEvent::MouseButtonDblClick)
    4. {
    5. qDebug() << "TMouseButtonDblClick works fine";
    6.  
    7. return true;
    8. }
    9. if (event->type() == QEvent::Enter) //Does not work
    10. {
    11. qDebug() << "Enter";
    12. QCursor cursor(Qt::PointingHandCursor);
    13. QApplication::setOverrideCursor(cursor);
    14. return true;
    15. }
    16. if (event->type() == QEvent::Leave) //Does not work.
    17. {
    18. qDebug() << "Leave";
    19. QCursor cursor(Qt::ArrowCursor);
    20. QApplication::setOverrideCursor(cursor);
    21. return true;
    22. }
    23. return false;
    24. }
    To copy to clipboard, switch view to plain text mode 

    Many thanks

  12. #12
    Join Date
    Aug 2011
    Posts
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLabel as an item delegate

    You may use QTableWidget and use setCellWidget to set a widget to cells of QTableWidget.

  13. #13
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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.

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 0
    Last Post: 10th March 2011, 11:44
  2. Item Delegate Visibility
    By raneeshambat in forum Newbie
    Replies: 1
    Last Post: 18th September 2009, 20:41
  3. Replies: 5
    Last Post: 10th August 2009, 10:50
  4. Delegate for a certain item?
    By somebody in forum Qt Programming
    Replies: 1
    Last Post: 18th August 2008, 22:55
  5. Item Delegate Painting
    By stevey in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2008, 07:37

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.