Results 1 to 7 of 7

Thread: QEvents subclassing QListWidget - not getting mouse press or release

  1. #1
    Join Date
    Oct 2009
    Posts
    38
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Question QEvents subclassing QListWidget - not getting mouse press or release

    I'm a bit lost here. I've created a new class subclassing QListWidget, and I have a qDebug() as the first statement in my event() method. It's printing out the event type for each call. I'm getting the following list (with the text to the right of the event # added by me):

    1 - Regular timer event
    8 - Gaining keyboard focus
    9 - Losing keyboard focus
    10 - Mouse entering boundaries
    11 - Mouse leaving boundaries
    13 - Widget position changed
    14 - Widget size changed
    17 - Widget shown on screen
    24 - Window was activated
    25 - Window was deactivated
    26 - A child widget has been shown
    39 - Widget palette has changed
    67 - ??
    68 - Child was added
    69 - Child was polished
    70 - Child was inserted
    71 - Child was removed
    74 - Request for polish
    75 - Widget was polished
    97 - Font was changed
    110 - "What's This?" help was clicked
    178 - The margins of the content rect changed
    194 - Touch begin: beginning of a sequence of touch-screen or track pad events
    198 - Gesture: a gesture was triggered
    203 - Widget's WinId was changed

    I'm not seeing any mouse press or release events. I'm trying to trap events so that mouse clicks are no longer processed by QListWidget, but by me. And yet, when I click the mouse in my list widget, the items in the list are still being highlighted and unhighlighted. How is the base class getting ahold of these events if I'm overriding event()?

    Any help here? Or should this question be in another forum? It seems to me this is a Qt question, because somehow the super class is getting events I'm not seeing.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QEvents subclassing QListWidget - not getting mouse press or release

    Why don't you override the corresponding event handlers?
    mousePressEvent() and friends...
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Oct 2009
    Posts
    38
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: QEvents subclassing QListWidget - not getting mouse press or release

    I started working on that. But the question still remains. When I look in the "C++ GUI Programming with Qt 4" book by Blanchette, supposedly the officially supported book, but admittedly old at this point, they say that you should be able to override event() to catch any events before it goes to the specific event filters, such as mousePressEvent(). The online documentation seems to support this. So I guess the question still stands in my mind as to why this doesn't work. I'm off to do it the other way, but if anyone has an ideas as to why the documented approach doesn't work, or as to how I could be reading that incorrectly, I would appreciate the input.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QEvents subclassing QListWidget - not getting mouse press or release

    they say that you should be able to override event() to catch any events before it goes to the specific event filters, such as mousePressEvent().
    This is true.
    You are probably doing something wrong in your implementation.
    Instead of dwelling on that, I suggested to override the event handlers since it will work for sure, and get you going.
    If you want, post your code, and we can have a look at what you are doing wrong
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Oct 2009
    Posts
    38
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: QEvents subclassing QListWidget - not getting mouse press or release

    So I have looked further and found that the event() function in QWidget is not virtual, which I'm not sure why that's that way. As far as code, I set up a gui project, and my MainWindow class creates and populates a ChildListWidget object which is derived from QListWidget. I found out that I had to implement mousePressEvent(), mouseReleaseEvent(), and mouseMoveEvent() in order to get QListWidget to no longer process these events. They were not being passed through my reimplementation of event(). At the very top of ChildListWidget::event(), I am printing the enum for the event type that I get. The enum for mouseButtonPress is 2 and for mouseButtonRelease is 3, but I see neither of these when I look at the output. My code is as follows. If I'm doing something wrong here I'd love to know about it:

    mainwindow.h:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. class ChildListWidget;
    7.  
    8. namespace Ui {
    9. class MainWindow;
    10. }
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit MainWindow(QWidget *parent = 0);
    18. ~MainWindow();
    19.  
    20. private:
    21. Ui::MainWindow *ui;
    22. ChildListWidget *m_listWidget;
    23. };
    24.  
    25. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "childlistwidget.h"
    3. #include "ui_mainwindow.h"
    4. #include <QListWidget>
    5. #include <QSize>
    6.  
    7. MainWindow::MainWindow(QWidget *parent) :
    8. QMainWindow(parent),
    9. ui(new Ui::MainWindow)
    10. {
    11. ui->setupUi(this);
    12.  
    13. QSize winsize = this->window()->size();
    14.  
    15. m_listWidget = new ChildListWidget(this);
    16. m_listWidget->resize(winsize.width(), winsize.height() - 35);
    17.  
    18. item = new QListWidgetItem("Item 1");
    19. m_listWidget->addItem(item);
    20. item = new QListWidgetItem("Item 2");
    21. m_listWidget->addItem(item);
    22. item = new QListWidgetItem("Item 3");
    23. m_listWidget->addItem(item);
    24. item = new QListWidgetItem("Item 4");
    25. m_listWidget->addItem(item);
    26. item = new QListWidgetItem("Item 5");
    27. m_listWidget->addItem(item);
    28. item = new QListWidgetItem("Item 6");
    29. m_listWidget->addItem(item);
    30. item = new QListWidgetItem("Item 7");
    31. m_listWidget->addItem(item);
    32.  
    33. this->setCentralWidget(m_listWidget);
    34. }
    To copy to clipboard, switch view to plain text mode 

    childlistwidget.h:
    Qt Code:
    1. #ifndef CHILDLISTWIDGET_H
    2. #define CHILDLISTWIDGET_H
    3.  
    4. #include <QListWidget>
    5.  
    6. class QGestureEvent;
    7. class QSwipeGesture;
    8. class QPanGesture;
    9. class QEvent;
    10.  
    11. class ChildListWidget : public QListWidget
    12. {
    13. Q_OBJECT
    14. public:
    15. explicit ChildListWidget(QWidget *parent = 0);
    16.  
    17. virtual bool event(QEvent *e);
    18. virtual void mousePressEvent(QMouseEvent *event);
    19. virtual void mouseReleaseEvent(QMouseEvent *event);
    20. virtual void mouseMoveEvent(QMouseEvent *event);
    21.  
    22. bool gestureEvent(QGestureEvent *event);
    23.  
    24. void swipeTriggered(QSwipeGesture* gesture);
    25. void panTriggered(QPanGesture* gesture);
    26.  
    27. bool eventFilter(QObject *obj, QEvent *event);
    28.  
    29. };
    30.  
    31. #endif // CHILDLISTWIDGET_H
    To copy to clipboard, switch view to plain text mode 

    childlistwidget.cpp:
    Qt Code:
    1. #include "childlistwidget.h"
    2.  
    3. #include <QEvent>
    4. #include <QGesture>
    5. #include <QGestureEvent>
    6. #include <QPainter>
    7. #include <QEvent>
    8. #include <QList>
    9. #include <QDebug>
    10.  
    11. ChildListWidget::ChildListWidget(QWidget *parent) :
    12. QListWidget(parent)
    13. {
    14. grabGesture(Qt::PanGesture);
    15. grabGesture(Qt::SwipeGesture);
    16. grabGesture(Qt::PinchGesture);
    17. installEventFilter(this);
    18. }
    19.  
    20. bool ChildListWidget::event(QEvent *e)
    21. {
    22. qDebug() << "ChildListWidget::event(): type(): " << e->type();
    23. if (e->type() == QEvent::Gesture) {
    24. qDebug() << "QEvent::Gesture";
    25. return gestureEvent(static_cast<QGestureEvent*>(e));
    26. }
    27. if (e->type() == QEvent::MouseButtonPress) {
    28. qDebug() << "QEvent::MouseButtonPress";
    29. return true;
    30. // do nothing
    31. }
    32. if (e->type() == QEvent::MouseButtonRelease) {
    33. qDebug() << "QEvent::MouseButtonRelease";
    34. // do nothing
    35. return true;
    36. }
    37. return QListWidget::event(e);
    38. }
    39.  
    40. void ChildListWidget::mousePressEvent(QMouseEvent *event)
    41. {
    42. Q_UNUSED(event);
    43. event->accept();
    44. }
    45.  
    46. void ChildListWidget::mouseReleaseEvent(QMouseEvent *event)
    47. {
    48. Q_UNUSED(event);
    49. event->accept();
    50. }
    51.  
    52. void ChildListWidget::mouseMoveEvent(QMouseEvent *event)
    53. {
    54. Q_UNUSED(event);
    55. event->accept();
    56. }
    57.  
    58. bool ChildListWidget::gestureEvent(QGestureEvent *event)
    59. {
    60. Q_UNUSED(event);
    61. qDebug() << "ChildListWidget::gestureEvent()";
    62. qDebug() << "Gesture type: " << event->gestures().at(0)->gestureType();
    63. if (QGesture *swipe = event->gesture(Qt::SwipeGesture)) {
    64. //swipeTriggered(static_cast<QSwipeGesture *>(swipe));
    65. } else if (QGesture *pan = event->gesture(Qt::PanGesture)) {
    66. //panTriggered(static_cast<QPanGesture *>(pan));
    67. }
    68. return true;
    69. }
    70.  
    71. void ChildListWidget::swipeTriggered(QSwipeGesture* gesture)
    72. {
    73. Q_UNUSED(gesture);
    74. qDebug() << "ChildListWidget::swipeTriggered()";
    75. }
    76.  
    77. void ChildListWidget::panTriggered(QPanGesture* gesture)
    78. {
    79. Q_UNUSED(gesture);
    80. qDebug() << "ChildListWidget::panTriggered()";
    81. }
    82.  
    83. bool ChildListWidget::eventFilter(QObject *obj, QEvent *event)
    84. {
    85. Q_UNUSED(obj);
    86. qDebug() << "ChildListWidget::eventFilter()";
    87. if (event->type() == QEvent::KeyPress) {
    88. QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
    89. qDebug() << "Ate key press" << keyEvent->key();
    90. return true;
    91. } else {
    92. return false;
    93. }
    94. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp:
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QEvents subclassing QListWidget - not getting mouse press or release

    So I have looked further and found that the event() function in QWidget is not virtual, which I'm not sure why that's that way.
    That is not true - its virtual protected:
    (documentation):
    bool QWidget::event ( QEvent * event ) [virtual protected]
    This is not realated (that I can see) to the problem you are talking about but:
    Qt Code:
    1. installEventFilter(this);
    To copy to clipboard, switch view to plain text mode 

    This code makes no sense.
    You use an event filter when you want anotehr object to filter this objects events - each object receives its own events any way.

    Why do you use Q_UNUSED when you are clearly using the variable?

    At the moment I don't see why some events will not be caught in your event() handler, in respect to your original post (except that the code for these events is not in the function as you are now using the specialized handlers).
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Oct 2009
    Posts
    38
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: QEvents subclassing QListWidget - not getting mouse press or release

    Yes, I see that, as far as the online documentation is concerned, but when I open qwidget.h (and I'm sure that I'm opening the correct one because in QtCreator, I have #include <QWidget> and then I type the word QWidget in a function and then right click and choose "Follow Symbol Under Cursor") I see (for 4.7.1) at line 635 that "bool event(QEvent *);" is what is declared, not "virtual book event(QEvent *);".

    Thanks, I see what you mean about the installEventfilter(this).

    As to the Q_UNUSED, that was something I put in there before putting the accept, and forgot to remove it. The Q_UNUSED, as I understand it, just tells the compiler not to print a warning, so I'm not sure that's causing any issues. Chalk it up to moving too quickly.

Similar Threads

  1. QlistView mouse press
    By dima in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2010, 15:51
  2. Problem with mouse press on QGraphicsView.
    By pastispast in forum Qt Programming
    Replies: 10
    Last Post: 13th September 2010, 06:52
  3. How catch key press and key release for entire application
    By hubbobubbo in forum Qt Programming
    Replies: 4
    Last Post: 1st June 2010, 20:53
  4. Mouse press event in a QGraphicsScene
    By Lykurg in forum Qt Programming
    Replies: 3
    Last Post: 19th June 2009, 10:28
  5. Checking for key press on mouse event
    By Cruz in forum Newbie
    Replies: 1
    Last Post: 24th January 2009, 18:18

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.