Results 1 to 2 of 2

Thread: QMouseevent

  1. #1
    Join Date
    Mar 2011
    Posts
    35
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default QMouseevent

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10.  
    11.  
    12.  
    13. }
    14.  
    15. MainWindow::~MainWindow()
    16. {
    17. delete ui;
    18. }
    19. class Mouse : public QObject
    20. {
    21. Q_OBJECT
    22.  
    23.  
    24. protected:
    25. bool eventFilter(QObject *obj, QEvent *event);
    26. };
    27. Mouse *mouse = new Mouse();
    28. bool Mouse::eventFilter(QObject *obj, QEvent *event)
    29. {
    30. if (event->type() == QEvent::MouseButtonPress) {
    31.  
    32. return true;
    33. } else {
    34. // standard event processing
    35. return QObject::eventFilter(obj, event);
    36. }
    37. }
    38. QLabel*label=new QLabel();
    39. label->installEventFilter(mouse);
    To copy to clipboard, switch view to plain text mode 


    I wanted to install event filter in Qlabel of my ui application ....I have posted the code above but I am having problem with it...I might be declaring the class in wrong section please help me how to install eventfilter in qlabel for mouseevent handling

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QMouseevent

    Code related to eventFilter looks ok, what are you doing with label after this ? Maybe you just display wrong label in ui. Another thing is that I'm not really sure how your code looks like, is this all one header ? If yes then I'd suggest separate the implementation to .cpp files.
    Just tested this code:
    Qt Code:
    1. // app.h
    2. #include <QObject>
    3.  
    4. class Mouse : public QObject
    5. {
    6. Q_OBJECT
    7. protected:
    8. bool eventFilter(QObject *obj, QEvent *event);
    9. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. // app.cpp
    2. #include "app.h"
    3. #include <QtGui>
    4.  
    5. bool Mouse::eventFilter(QObject *obj, QEvent *event)
    6. {
    7. if (event->type() == QEvent::MouseButtonPress) {
    8. qDebug() << "mouse press";
    9. return true;
    10. } else {
    11. // standard event processing
    12. return QObject::eventFilter(obj, event);
    13. }
    14. }
    15.  
    16. int main(int argc, char *argv[])
    17. {
    18. QApplication a(argc, argv);
    19. QWidget * w = new QWidget();
    20. QVBoxLayout * l = new QVBoxLayout(w);
    21. w->setLayout(l);
    22. QLabel * lbl = new QLabel(w);
    23. lbl->installEventFilter(new Mouse());
    24. l->addWidget(lbl);
    25. w->show();
    26.  
    27. return a.exec();
    28. }
    To copy to clipboard, switch view to plain text mode 
    When clicking on the label I can see "mouse press" text in console window.

Similar Threads

  1. how to catch qMouseEvent
    By pakine in forum Newbie
    Replies: 4
    Last Post: 27th June 2012, 11:47
  2. QMouseEvent on QPaintEvent
    By GUIman in forum Newbie
    Replies: 7
    Last Post: 1st March 2011, 09:51
  3. QMouseEvent
    By Fallen_ in forum Qt Programming
    Replies: 1
    Last Post: 22nd September 2010, 06:17
  4. QMouseEvent
    By shenakan in forum Newbie
    Replies: 1
    Last Post: 20th August 2009, 15:53
  5. QToolBox and QMouseEvent
    By mickey in forum Qt Programming
    Replies: 6
    Last Post: 1st February 2006, 12:57

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.