Page 1 of 3 123 LastLast
Results 1 to 20 of 42

Thread: Hover and Highlight QTable and QTree Items

  1. #1
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Hover and Highlight QTable and QTree Items

    I was curious, is Qt Stylesheets the only way to get the hover: property?

    I wanted to somehow highlight like a whole row in a TableWidget when you hover your mouse over a row. I also wanted to highlight the QTreeWidgetItem when someone hovers their mouse over it, so the background color changes on that item. I figured it out using QPalette for Highlight by clicking, but not my mouseover...

    Any ideas? Thanks.

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Hover and Highlight QTable and QTree Items

    U can use itemAt() function of QTableWidget to get the table widget item under the mouse... then set its background brush....
    same u can do with qtreewidgetitems i guess...

    and yah... capture the mouse movements on mouseMoveEvent() ....
    you have to set setMouseTracking(true) to receive the mouse move events....

    hope this will help you..

  3. #3
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Hover and Highlight QTable and QTree Items

    It kind of helps, I like your itemAt idea, I could do like
    itemAt(QCursor:os()); but then I would need to put this in a loop and multithread right? Where else could I put this? How could I code this so it works?

    I think perhaps the best way is to use mouseMoveEvent() and subclass TableWidget and TreeWidget.

  4. #4
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Hover and Highlight QTable and QTree Items

    So subclassing is only way I take it? I couldn't figure out the itemAt thing.

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Hover and Highlight QTable and QTree Items

    Enable mouse tracking on the viewport and catch mouse move events either by subclassing and reimplementing the event handler or alternatively with an event filter. The latter approach does not require subclassing.
    J-P Nurmi

  6. #6
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Hover and Highlight QTable and QTree Items

    Where can I learn about "event filters". The subclassing seems time consuming :O.

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Hover and Highlight QTable and QTree Items

    There's an example in QObject::installEventFilter() docs.
    J-P Nurmi

  8. The following user says thank you to jpn for this useful post:

    VireX (11th May 2007)

  9. #8
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Hover and Highlight QTable and QTree Items

    Ok I got the code, I'm at the part where I do
    event->type() == QEvent::MouseMove){
    so in here, do I just somehow get an instance of my original class of QTreeWidget and just do:
    QTreeWidgetItem* bla = Widget->itemAt(QCursor:os());
    bla->setBackground(0, QBrush(QColor(255,0,0)));

    Is that how I am suppose to do it? Or wait do I cast the QObject as a QTreeWidget perhaps?

  10. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Hover and Highlight QTable and QTree Items

    The position passed within the event might be a bit more reliable:
    Qt Code:
    1. // QTreeWidgetItem* prev;
    2.  
    3. if (event->type() == QEvent::MouseMove)
    4. {
    5. QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
    6. QTreeWidgetItem* current = ui.treeWidget->itemAt(mouseEvent->pos());
    7. if (current && current != prev)
    8. {
    9. if (prev)
    10. prev->setBackground(0, palette().base()); // reset previous
    11. current->setBackground(0, Qt::red); // highlight current
    12. prev = current;
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  11. The following user says thank you to jpn for this useful post:

    VireX (12th May 2007)

  12. #10
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Hover and Highlight QTable and QTree Items

    I think that's exactly what I was looking for thank you. I think I was close.

  13. #11
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Hover and Highlight QTable and QTree Items

    I made a Filter class,
    I used Q_OBJECT.
    I used the obj and event as you said.
    I even fixed your code, but it seems it works but very slowly. Like I have to whirl my mouse all over USerList and outside of UserList in order to get it to work properly. I mean it works, it even deletes old highlights, but it doesn't highlight the new spot when i move my mouse.

  14. #12
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Hover and Highlight QTable and QTree Items

    Have you enabled mouse tracking on the viewport and also installed the event filter on the viewport?
    Qt Code:
    1. treeWidget->viewport()->setMouseTracking(true);
    2. treeWidget->viewport()->installEventFilter(this);
    To copy to clipboard, switch view to plain text mode 

    Edit: is the tree massive, containing huge amount of items in a complex hierarchy?
    Last edited by jpn; 12th May 2007 at 08:01.
    J-P Nurmi

  15. #13
    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: Hover and Highlight QTable and QTree Items

    Just stumbled upon this thread, so I decided to include my own 5 cents...

    There is another possibility - you can use a custom delegate and reimplement editorEvent() and act upon one (or more) of QEvent::HoverEnter, QEvent::HoverLeave or QEvent::HoverMove. If I remember correctly you have to enable mouse tracking for these events to be delivered, although I'm not sure of that.

  16. #14
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Hover and Highlight QTable and QTree Items

    wysota, I don't think I wanna delve into making something too complex.
    jpn, I was doing:
    TreeWidg->setMouseTracking(true);
    TreeWidg->installEventFilter(mhf); // mhf = new MouseHoverFilter;

    When I changed it to:
    TreeWidg->viewport()->setMouseTracking(true);
    TreeWidg->viewport()->installEventFilter(mhf); // mhf = new MouseHoverFilter;
    Now If I hover over treewidget it crashes the program.

  17. #15
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Hover and Highlight QTable and QTree Items

    Quote Originally Posted by VireX View Post
    Now If I hover over treewidget it crashes the program.
    Improper cast? Insufficient checks? ...see the attached example.
    Attached Files Attached Files
    J-P Nurmi

  18. #16
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Hover and Highlight QTable and QTree Items

    No I did exactly what you posted, except that I didn't subclass my treeWidget.
    I made a class called MouseHoverFilter which has the event filter. Then I installed the event filter and setmousetracking on the TreeWidget. It only crashes if i add viewport().

  19. #17
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Hover and Highlight QTable and QTree Items

    Heres the exact code I used, just try it:

    In my main file:
    MouseHoverFilter* mhf;
    MyList->setMouseTracking(true);
    mhf = new MouseHoverFilter;
    MyList->installEventFilter(mhf);

    Filters.h :

    class MouseHoverFilter : public QObject {
    Q_OBJECT
    public:
    MouseHoverFilter(QObject* parent);
    QTreeWidgetItem* prev;
    protected:
    bool eventFilter(QObject *obj, QEvent *event);
    };

    MouseHoverFilter::MouseHoverFilter(QObject* parent = 0) : QObject(parent) {
    prev = 0;
    }

    bool MouseHoverFilter::eventFilter(QObject *obj, QEvent *event){
    if(event->type() == QEvent::MouseMove){
    QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
    QTreeWidget* qtw = static_cast<QTreeWidget*>(obj);
    QTreeWidgetItem* current = qtw->itemAt(mouseEvent->pos());
    if(current && current != prev && current->childCount() == 0){
    if(prev)prev->setBackground(0, qtw->palette().base());
    if(prev)prev->setBackground(1, qtw->palette().base());
    if(prev)prev->setBackground(2, qtw->palette().base());
    current->setBackground(0, QColor(193, 235, 255));
    current->setBackground(1, QColor(193, 235, 255));
    current->setBackground(2, QColor(193, 235, 255));
    prev = current;
    }
    return true;
    } else {
    QObject::eventFilter(obj, event);
    }
    return false;
    }
    #include "Filters.moc"

  20. #18
    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: Hover and Highlight QTable and QTree Items

    Quote Originally Posted by VireX View Post
    wysota, I don't think I wanna delve into making something too complex.
    Doesn't seem very complex...
    Attached Files Attached Files

  21. #19
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Hover and Highlight QTable and QTree Items

    This is the improper cast:
    Qt Code:
    1. QTreeWidget* qtw = static_cast<QTreeWidget*>(obj);
    To copy to clipboard, switch view to plain text mode 
    The receiver object is the viewport widget, not the tree widget.
    J-P Nurmi

  22. #20
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Hover and Highlight QTable and QTree Items

    I've tried setting a QTreeWidget inside the Mouse class and delivering the original QTreeWidget to the Mouse Class constructor, but that made no difference, so that isn't the problem, it works this way too.

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.