Results 1 to 7 of 7

Thread: QGraphicsItem tooltip behavior

  1. #1
    Join Date
    Jan 2007
    Posts
    68
    Thanks
    9
    Thanked 8 Times in 8 Posts

    Default QGraphicsItem tooltip behavior

    hi ppl,

    I've got the following small prob:

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class MyGraphicsItem : public QGraphicsItem
    4. {
    5. public:
    6. MyGraphicsItem(QGraphicsItem *parent = 0) : QGraphicsItem(parent), _width(40), _height(10){}
    7. ~MyGraphicsItem(void){qDebug("delBand");}
    8.  
    9. QRectF boundingRect() const{
    10. return QRectF(0, 0, _width, _height);
    11. }
    12. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0){
    13. painter->setBrush(Qt::darkRed);
    14. painter->setPen(Qt::black);
    15. painter->drawRoundRect(0, 0, _width, _height);
    16. }
    17.  
    18. private:
    19. int _width;
    20. int _height;
    21. };
    To copy to clipboard, switch view to plain text mode 

    using:
    Qt Code:
    1. setToolTip(QString("testToolTip"));
    To copy to clipboard, switch view to plain text mode 
    in the constructor, works perfectly to enable tooltips for the item

    but using:
    Qt Code:
    1. QString toolTip() const {
    2. return QString("testToolTip");
    3. }
    To copy to clipboard, switch view to plain text mode 
    does not work

    why???

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem tooltip behavior

    because QGraphicsItem::tooltip is not virtual.
    The view/scene will always call the base class version, which returns an empty string if you don't set any.

    Regards

  3. #3
    Join Date
    May 2007
    Location
    Lublin, Poland
    Posts
    345
    Thanks
    40
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsItem tooltip behavior

    Hi,

    This post should be removed. Sorry.

    Maverick
    Qt allows you to use everything you want
    wysota
    --------------------------------------------------------------------------------
    #if defined(Q_OS_UNIX) && defined(QT_DEBUG)
    abort(); // trap; generates core dump
    #else
    exit(1); // goodbye cruel world
    #endif

  4. #4
    Join Date
    May 2007
    Location
    Lublin, Poland
    Posts
    345
    Thanks
    40
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsItem tooltip behavior

    Hi,

    Ok...solved.
    I have saved this post at the same time when the answer was given.
    This post is not needed. I hope moderator or admin will remove it.


    Maverick
    Qt allows you to use everything you want
    wysota
    --------------------------------------------------------------------------------
    #if defined(Q_OS_UNIX) && defined(QT_DEBUG)
    abort(); // trap; generates core dump
    #else
    exit(1); // goodbye cruel world
    #endif

  5. #5
    Join Date
    Jan 2007
    Posts
    68
    Thanks
    9
    Thanked 8 Times in 8 Posts

    Default Re: QGraphicsItem tooltip behavior

    Quote Originally Posted by marcel View Post
    because QGraphicsItem::tooltip is not virtual.
    The view/scene will always call the base class version, which returns an empty string if you don't set any.

    Regards
    so there's no possibility to get the tooltip working without using setToolTip,

    and to only to use a function like QString toolTip() const ?

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem tooltip behavior

    so there's no possibility to get the tooltip working without using setToolTip,

    and to only to use a function like QString toolTip() const ?
    No.

    But if you do a little of extra work, you might be able to show a tooltip when you want it to, but manually.
    Hint: override QGraphicsScene::helpEvent.

    Regards

  7. #7
    Join Date
    Jan 2007
    Posts
    68
    Thanks
    9
    Thanked 8 Times in 8 Posts

    Default Re: QGraphicsItem tooltip behavior

    solved my prob the following way:

    in constructor:
    Qt Code:
    1. setAcceptsHoverEvents(true);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. protected:
    2. void hoverEnterEvent ( QGraphicsSceneHoverEvent * event ){
    3. QString str = QString("testToolTip");
    4. if (toolTip() != str){
    5. setToolTip(str);
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    this does exactly, what I wanted,

    but is a little long winded

Similar Threads

  1. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 10:31

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.