Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 42

Thread: Hover and Highlight QTable and QTree Items

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

    Default Re: Hover and Highlight QTable and QTree Items

    Here's my Filters.h
    Attached Files Attached Files

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

    Default Re: Hover and Highlight QTable and QTree Items

    Quote Originally Posted by VireX View Post
    Here's my Filters.h
    Works for me. Does this still crash for you? Could you paste backtrace from debugger?
    J-P Nurmi

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

    Default Re: Hover and Highlight QTable and QTree Items

    No it doesn't crash after I removed your code of "bla->viewport()->setMouseTracking" back to the correct form: "bla->setMouseTracking()".

    Thats not the problem, it's slow, I could take a video, and what you will see is, me spinning and making my mouse go crazy on the treewidget list, and after several seconds it finally highlights one item, and when my mouse is not even on the list, it's still highlighted. Like as if, the treewidget picks randomly when it wants to highlight something. Then I spin my mouse all over the screen again and again, then finally some other random TreeItem highlights itself.

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

    Default Re: Hover and Highlight QTable and QTree Items

    Why don't you try my solution?

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

    Default Re: Hover and Highlight QTable and QTree Items

    Quote Originally Posted by VireX View Post
    No it doesn't crash after I removed your code of "bla->viewport()->setMouseTracking" back to the correct form: "bla->setMouseTracking()".
    I'm sorry to say but it's not correct at all. It's the viewport widget which receives mouse move events.

    Thats not the problem, it's slow, I could take a video, and what you will see is, me spinning and making my mouse go crazy on the treewidget list, and after several seconds it finally highlights one item, and when my mouse is not even on the list, it's still highlighted. Like as if, the treewidget picks randomly when it wants to highlight something. Then I spin my mouse all over the screen again and again, then finally some other random TreeItem highlights itself.
    That's most likely because you're not receiving those mouse move events but some other event causes an update every now and then, depending on if you enter and leave the widget or change the focus or something like that. Is the compilable example in post #15 that "slow"? Or did you even bother checking out out?
    J-P Nurmi

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

    Default Re: Hover and Highlight QTable and QTree Items

    1. Well doing bla->viewport()-> causes crash, that's 100% for sure, therefore it is not correct.

    2. Maybe so, but of course why would I bother checking it out when I have the same code you showed me earlier in a different style?

    @wysota: Your solution works great. Still trying to find out what went wrong with jpn's solution.
    Last edited by VireX; 12th May 2007 at 16:08.

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

    Default Re: Hover and Highlight QTable and QTree Items

    Quote Originally Posted by VireX View Post
    1. Well doing bla->viewport()-> causes crash, that's 100% for sure, therefore it is not correct.
    Again, the example works for fine. You must have a quirk of some kind in your code.

    1. Use a debugger
    2. examine the backtrace
    3. find a reason why does it crash


    2. Maybe so, but of course why would I bother checking it out when I have the same code you showed me earlier in a different style?
    Because it would convince you that the approach works after all.
    J-P Nurmi

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

    Default Re: Hover and Highlight QTable and QTree Items

    Both your solutions work fine, in their isolated situations.
    I just don't understand why it doesn't work with my MouseFilter class which is almost EXACTLY like jpn's program.

    Why would Qt put setMouseTracking and installEventFilter in the QAbstracts if it was suppose to be used inside a class with viewport().

    When I do TreeWidget->viewport()->setMouseTracking(true); it crashes, that's the SOLE reason. I think jpn doesn't realize i'm not talking about his program.

  9. #29
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Hover and Highlight QTable and QTree Items

    "Almost" sometimes makes a big difference. I have to agree with JP - you have to install the filter on the viewport and not the list itself. Event filters don't propagate - if the list installs a filter on its viewport and you install a fillter on the list, you won't get events for the viewport. You're having obvious errors in your code which JP already pointed out. You'll be receiving mouse move events only for that parts of the view which are not occupied by other widgets (viewport and scrollbars) and I doubt that's what you want.

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

    Default Re: Hover and Highlight QTable and QTree Items

    Maybe so.

    One question why doesnt using QPalette::Background or Window make the background become red? I tried different color roles in your code.

    Also your code highlights each item in each column. Is there a way to highlight background of the whole row? (I have selection mode in selectRow).

  11. #31
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Hover and Highlight QTable and QTree Items

    No, you have to do it through editorEvent as I suggested earlier. But it's simpler to adjust the selection using an event filter. Here is a combined approach:

    Qt Code:
    1. bool editorEvent ( QEvent * event, QAbstractItemModel * model,
    2. const QStyleOptionViewItem & option, const QModelIndex & index ){
    3. if(event->type()==QEvent::MouseMove){
    4. // view is the parent
    5. QAbstractItemView *view = qobject_cast<QAbstractItemView*>(parent());
    6. if(view){
    7. view->selectionModel()->select(index, QItemSelectionModel::ClearAndSelect);
    8. return true;
    9. }
    10. }
    11. return QItemDelegate::editorEvent(event, model, option, index);
    12. }
    13. bool eventFilter(QObject *o, QEvent *e){
    14. // view is the parent
    15. QAbstractItemView *view = qobject_cast<QAbstractItemView*>(parent());
    16. if(view && e->type()==QEvent::Leave){
    17. view->selectionModel()->clearSelection();
    18. }
    19. return QItemDelegate::eventFilter(o, e);
    20. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Hover and Highlight QTable and QTree Items

    I added that to delegate class but it made no difference.

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

    Default Re: Hover and Highlight QTable and QTree Items

    Did you remember about enabling mouse tracking?

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

    Default Re: Hover and Highlight QTable and QTree Items

    Here's my code, and mouse tracking does not change anything (I put mousetracking on list List->setMouseTracking(true); I even tried List->viewport()->setMouseTracking(true); nothing works):
    Qt Code:
    1. class Delegate : public QItemDelegate {
    2. public:
    3. Delegate(QObject *parent=0) : QItemDelegate(parent){ }
    4. protected:
    5. void drawDisplay ( QPainter * painter, const QStyleOptionViewItem & option, const QRect &rect, const QString &text ) const{
    6. QStyleOptionViewItem opt = option;
    7. if(option.state & QStyle::State_MouseOver){
    8. opt.palette.setColor(QPalette::Base, QColor(0,255,0));
    9. opt.palette.setColor(QPalette::Text, Qt::red);
    10. //opt.font.setBold(true);
    11. }
    12. QItemDelegate::drawDisplay(painter, opt, rect, text);
    13. }
    14. bool editorEvent ( QEvent * event, QAbstractItemModel * model,
    15. const QStyleOptionViewItem & option, const QModelIndex & index ){
    16. if(event->type()==QEvent::MouseMove){
    17. // view is the parent
    18. QAbstractItemView *view = qobject_cast<QAbstractItemView*>(parent());
    19. if(view){
    20. view->selectionModel()->select(index, QItemSelectionModel::ClearAndSelect);
    21. return true;
    22. }
    23. }
    24. return QItemDelegate::editorEvent(event, model, option, index);
    25. }
    26. bool eventFilter(QObject *o, QEvent *e){
    27. // view is the parent
    28. QAbstractItemView *view = qobject_cast<QAbstractItemView*>(parent());
    29. if(view && e->type()==QEvent::Leave){
    30. view->selectionModel()->clearSelection();
    31. }
    32. return QItemDelegate::eventFilter(o, e);
    33. }
    34. };
    35. #include "Filters.moc"
    To copy to clipboard, switch view to plain text mode 

  15. #35
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Hover and Highlight QTable and QTree Items

    "view is the parent" - is it true for you? Did you set the view as the parent of the delegate? Because if you didn't then it doesn't have a chance to work And remember to install the event filter on the viewport of the view, otherwise selection won't be cleared when you leave the view.

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

    Default Re: Hover and Highlight QTable and QTree Items

    Why would I install the eventfilter, we dont use that for this. We dont use installEventFilter for this function. It would be easier if you showed me the exact code you used in like your main.cpp.

    I did:
    UserList->viewport()->setMouseTracking(true);
    UserList->setItemDelegate(new Delegate(UserList->viewport()));
    UserList->viewport()->setAttribute(Qt::WA_Hover, true);

    Which is all you told me to do.

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

    Default Re: Hover and Highlight QTable and QTree Items

    I think wysota meant more or less something like this:
    Qt Code:
    1. Delegate* delegate = new Delegate(UserList); // view as parent, not viewport
    2. UserList->viewport()->installEventFilter(delegate); // install event filter on the viewport
    3. UserList->setItemDelegate(delegate);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

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

    Default Re: Hover and Highlight QTable and QTree Items

    Well all that did is take away the red bold hover on mouse over. Didn't fix the problem.

  19. #39
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Hover and Highlight QTable and QTree Items

    Quote Originally Posted by VireX View Post
    Why would I install the eventfilter, we dont use that for this. We dont use installEventFilter for this function.
    So why do you paste me the code that uses it?
    It would be easier if you showed me the exact code you used in like your main.cpp.
    My main.cpp doesn't exist for a few days anymore, so this is not an option.

    I did:
    UserList->viewport()->setMouseTracking(true);
    UserList->setItemDelegate(new Delegate(UserList->viewport()));
    UserList->viewport()->setAttribute(Qt::WA_Hover, true);

    Which is all you told me to do.
    No, I didn't. Hover attribute shouldn't be necessary and the viewport shouldn't be the parent of the delegate. Please verify that the if block in the editorEvent gets entered at all.

    Here, I implemented the thing again.
    Attached Files Attached Files
    Last edited by wysota; 16th May 2007 at 09:06. Reason: Added the attachment

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

    Default Re: Hover and Highlight QTable and QTree Items

    Nice it worked finally, I guess since I was using your old stuff and new stuff together it was conflicting and not showing up. And perhaps wrong parents etc.

    Anyway I'm still wondering, how come selectionMode being SelectRow, yet the hover only highlights EACH column. Is it possible to code a way to highlight the WHOLE ROW, instead of a single column of the row?

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
  •  
Qt is a trademark of The Qt Company.