Results 1 to 11 of 11

Thread: Capture mouse event on QHeaderView

  1. #1
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Capture mouse event on QHeaderView

    I wish to capture mouse events on the QHeaderView part of a QTreeview. header()->installEventFilter(this); captures some events but even if the only statement in the function is "return true" (ie, filter the event), the QHeaderView still gets the mouse message.

    Any ideas? I don't want to block the events, just analyse them as well.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Capture mouse event on QHeaderView

    show us code.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Capture mouse event on QHeaderView

    Qt Code:
    1. class FLView : public QTreeView
    2. {
    3. ...
    4. }
    5.  
    6. FLView::FLView(QWidget *parent) : QTreeView(parent) ,
    7. {
    8. header()->installEventFilter(this);
    9. }
    10.  
    11. bool FLView::eventFilter(QObject *obj, QEvent *event)
    12. {
    13. return true;
    14. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Capture mouse event on QHeaderView

    try this
    Qt Code:
    1. bool FLView::eventFilter(QObject *obj, QEvent *event)
    2. {
    3. return QTreeView::eventFilter(obj, event);
    4. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Capture mouse event on QHeaderView

    Yes, but events I'm interested in don't get to my event filter. I can prove this by just using "return true" to attempt to block the event.

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Capture mouse event on QHeaderView

    I didn't get: do you need to block a header's events or don't block them?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Capture mouse event on QHeaderView

    I wish to analyse all events passed to an object.

    To see if my method of doing so worked, I attempted to ignore all the events that get passed to me so they never reach there destination. However, the control still acted as normal, handling all mouse events.

    Therefore my event filter failed. Its not much of an event filter if it allows all of them through regardless.

    Also, If I change the event filter code so that it pops up a messagebox when it filters an event of type "MouseButtonPress", the message box never shows up.

  8. #8
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Capture mouse event on QHeaderView

    Fixed.

    QHeaderView doesn't process the events directly, it gets passed from another object. Bottom line is you need to hook into the headers viewport, not the header itself.

  9. The following 2 users say thank you to squidge for this useful post:

    busimus (26th December 2017), JoZCaVaLLo (20th April 2011)

  10. #9
    Join Date
    Jun 2009
    Posts
    37
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Capture mouse event on QHeaderView

    Hello squidge!!!

    1 year and a half later somebody can still fall in this difficulty... it annoyed me during the last 2 days... it was not really obvious to find it out. Unfortunately google doesn't really look inside qtcentre.org

    This is the solution that worked for me...

    Qt Code:
    1. class MouseReleaser : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. MouseReleaser(QObject * parent) : QObject(parent){;}
    6. protected:
    7. bool eventFilter(QObject *obj, QEvent *ev)
    8. {
    9. if (ev->type()==QEvent::MouseButtonRelease)
    10. return true;
    11. else
    12. return QObject::eventFilter(obj, ev);
    13. }
    14. };
    15.  
    16. ...
    17.  
    18. MyTableView::MyTableView(QWidget *parent)
    19. :QTableView(parent)
    20. {
    21. MouseReleaser *mr = new MouseReleaser(this);
    22. horizontalHeader()->viewport()->installEventFilter(mr);
    23. verticalHeader()->viewport()->installEventFilter(mr);
    24. }
    To copy to clipboard, switch view to plain text mode 


    Thank you for the great job!

  11. #10
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Capture mouse event on QHeaderView

    Quote Originally Posted by JoZCaVaLLo View Post
    Hello squidge!!!

    1 year and a half later somebody can still fall in this difficulty... it annoyed me during the last 2 days... it was not really obvious to find it out. Unfortunately google doesn't really look inside qtcentre.org
    It does if you ask it , eg: "site:qtcentre.org QHeaderView capture" brings you to this thread as first hit

  12. #11
    Join Date
    May 2012
    Location
    India
    Posts
    51
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Capture mouse event on QHeaderView

    There is no direct API for this at the moment, so for the time being, you need to reimplement the mousePressEvent() [doc.qt.nokia.com] and mouseReleaseEvent() [doc.qt.nokia.com] for the headerview and check which section is being pressed on, then if it is one you want to allow to be movable, or clickable, you turn it on before calling the base implementation. In the mouseReleaseEvent() you would turn off the movable/clickable properties.

    When it comes to setSortIndicatorShown() [doc.qt.nokia.com], then it is expected that the indicator will be shown until the user clicks on another section. So in mousePressEvent() we hide the indicator if the section clicked is not the one that should show the indicator. Alternatively the sort indicator can simply stay on the section clicked previously.

    The example below demonstrates how this can be done.

    Qt Code:
    1. #include <QtGui>
    2. class HeaderView : public QHeaderView
    3. {
    4. Q_OBJECT
    5. public:
    6. HeaderView(QWidget *parent = 0) : QHeaderView(Qt::Horizontal, parent)
    7. {
    8. connect(this, SIGNAL(sectionClicked(int)), this, SLOT(clickedSection(int)));
    9. }
    10. void mousePressEvent(QMouseEvent *event)
    11. {
    12. if(visualIndexAt(event->pos().x()) == 1) {
    13. setClickable(true);
    14. setSortIndicator(1, sortIndicatorOrder());
    15. setSortIndicatorShown(true);
    16. } else {
    17. setSortIndicatorShown(false);
    18. resizeSection(1, sectionSize(1) - 1);
    19. resizeSection(1, sectionSize(1) + 1);
    20. }
    21. QHeaderView::mousePressEvent(event);
    22. }
    23. void mouseReleaseEvent(QMouseEvent *event)
    24. {
    25. QHeaderView::mouseReleaseEvent(event);
    26. setClickable(false);
    27. }
    28. public slots:
    29. void clickedSection(int s)
    30. {
    31. qDebug() << "Section " << s << " clicked";
    32. }
    33. };
    34.  
    35. class TreeWidget : public QTreeWidget
    36. {
    37. public:
    38. TreeWidget()
    39. {
    40. setColumnCount(3);
    41. list << "a" << "b" << "c";
    42. QStringList list2;
    43. list2 << "d" << "e" << "f";
    44. QTreeWidgetItem *item1 = new QTreeWidgetItem(this, list);
    45. QTreeWidgetItem *item2 = new QTreeWidgetItem(this, list2);
    46. addTopLevelItem(item1);
    47. addTopLevelItem(item2);
    48. setHeader(new HeaderView(this));
    49. }
    50. };
    51.  
    52. #include "main.moc"
    53.  
    54. int main(int argc, char** argv)
    55. {
    56. QApplication app(argc, argv);
    57. TreeWidget tree;
    58. tree.show();
    59. return app.exec();
    60. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How to keep the mouse event?
    By oscar721 in forum Qt Programming
    Replies: 1
    Last Post: 27th October 2009, 09:48
  2. Mouse over event on qwtmarker?
    By shud in forum Qwt
    Replies: 2
    Last Post: 20th September 2009, 15:12
  3. QSystemTrayIcon capture mouse hover event
    By alan in forum Qt Programming
    Replies: 2
    Last Post: 1st August 2009, 19:42
  4. how to send a emulated mouse event to QListWidget
    By yxmaomao in forum Qt Programming
    Replies: 4
    Last Post: 22nd July 2008, 02:49
  5. The event fired by the mouse click on the frame
    By Placido Currò in forum Qt Programming
    Replies: 8
    Last Post: 3rd March 2007, 09:05

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.