Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: help needed in QMouseEvent

  1. #1
    Join Date
    Sep 2008
    Posts
    84
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question help needed in QMouseEvent

    i have lineEdit control & 2 Button i.e Enter and Clear
    When the User Presses Clear Button for 4 sec or more the text in the lineEdit should be cleared.

    I'm not able to capture the pressEvent on the clear button.
    Help Needed.

  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: help needed in QMouseEvent

    why do you use events? QPushButton has signal clicked. you can try to use in.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: help needed in QMouseEvent

    Why ever you want to force the user to press a button 4 seconds, simply subclass QPushButton an implement QWidget::mousePressEvent ( QMouseEvent * event ). (and of course QWidget::mouseReleaseEvent ( QMouseEvent * event ) to measure the time)

    Lykurg

  4. The following user says thank you to Lykurg for this useful post:

    aj2903 (14th February 2009)

  5. #4
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: help needed in QMouseEvent

    here is how u do it buddy:

    first use setMouseTracking(true) for your button class. start a timer when the mousePressEvent comes. set a timeout for 4 seconds on the timer and on slot for timeout(), u can clear the text

    void YourButtonClass::mousePressEvent(QMouseEvent* event)
    {

    if (event->button() == Qt::LeftButton)
    {
    Timer->start(m_sec);
    m_bMouseButtonDown = true;
    }

    }

    void CAutoRepeatButton::mouseReleaseEvent(QMouseEvent* event)
    {

    if (event->button() == Qt::LeftButton)
    {
    m_pAutoRepeatTimer->stop();
    m_bMouseButtonDown = false;

    if (m_pAutoRepeatTimer->interval() == urInterval)
    {
    urSlot();
    }
    }
    }

    this should solve ur problem

  6. The following user says thank you to talk2amulya for this useful post:

    aj2903 (14th February 2009)

  7. #5
    Join Date
    Sep 2008
    Posts
    84
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: help needed in QMouseEvent

    thanks to all for quick reply.

    hi talk2amulya,
    i'm using simple QPushButton,i have one problem, in your code :

    Qt Code:
    1. void YourButtonClass::mousePressEvent(QMouseEvent* event)
    2. {
    3.  
    4. if (event->button() == Qt::LeftButton)
    5. {
    6. Timer->start(m_sec);
    7. m_bMouseButtonDown = true;
    8. }
    To copy to clipboard, switch view to plain text mode 

    how can i replace YourButtonClass with QPushButton?
    is it possible?

  8. #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: help needed in QMouseEvent

    install event filter for you button.
    Qt Code:
    1. ...
    2. m_pb = new QPushButton(tr("Clear"), this);
    3. m_pb->installEventFilter(this);
    4. ...
    5. bool MyWidget::eventFilter(QObject *o, QEvent e)
    6. {
    7. if (o == m_pb && e->type() == QEvent::QEvent::MouseButtonPress) {
    8. QMouseEvent *me = static_cast<QMouseEvent *>(e);
    9. if (e->button() == Qt::LeftButton) {
    10. Timer->start(m_sec);
    11. m_bMouseButtonDown = true;
    12. return true;
    13. }
    14. }
    15. return QWidget::eventFilter(o, e);
    16. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  9. The following user says thank you to spirit for this useful post:

    aj2903 (14th February 2009)

  10. #7
    Join Date
    Sep 2008
    Posts
    84
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: help needed in QMouseEvent

    hi spirit

    i have written the code in following manner :


    abc.h
    class abcublic QDialog,public QObject
    {
    Qt Code:
    1. protected :
    2. bool eventFilter(QObject *o,QEvent *e):
    To copy to clipboard, switch view to plain text mode 
    }

    I'm facing the following error:
    abc.h:70: error: only constructors take base initializers

    I don't know what the problem is ?
    Kindly correct me where i'm going wrong

    Thanks in Advanced

  11. #8
    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: help needed in QMouseEvent

    Qt doesnt support multiply inheritance from QObject, so you have to correct your code like this
    Qt Code:
    1. class YourDialog: public QDialog
    2. {
    3. ...
    4. protected:
    5. ...
    6. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  12. #9
    Join Date
    Sep 2008
    Posts
    84
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: help needed in QMouseEvent

    after trying
    Qt Code:
    1. class YourDialog: public QDialog
    2. {
    3. ...
    4. protected:
    5. ...
    6. }
    To copy to clipboard, switch view to plain text mode 

    same error is occuring

  13. #10
    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: help needed in QMouseEvent

    show whole header file, plz.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  14. #11
    Join Date
    Sep 2008
    Posts
    84
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: help needed in QMouseEvent

    Here is the Header File

    Qt Code:
    1. #ifndef ABC_H
    2. #define ABC_H
    3.  
    4. #include <QDialog>
    5. #include <QTimer>
    6. #include <QMouseEvent>
    7.  
    8. class abc: public QDialog
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. abc(QWidget *parent = 0);
    14. ~abc;
    15. public slots:
    16. void log_data_refresh();
    17. protected:
    18. void mousePressEvent(QMouseEvent *event);
    19. void mouseReleaseEvent(QMouseEvent *event);
    20. bool eventFilter(QObject *obj,QEvent *event):
    21.  
    22. private slots:
    23. ...
    24. void check_queue();
    25. void closeEvent(QCloseEvent *event);
    26. void showEvent(QShowEvent *event);
    27.  
    28. private:
    29. QTimer *timer;
    30. };
    31.  
    32. #endif
    To copy to clipboard, switch view to plain text mode 

  15. #12
    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: help needed in QMouseEvent

    try to change ~abc; to ~abc();
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  16. #13
    Join Date
    Sep 2008
    Posts
    84
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: help needed in QMouseEvent

    tried but same error.

  17. #14
    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: help needed in QMouseEvent

    try this change bool eventFilter(QObject *obj,QEvent *event): to bool eventFilter(QObject *obj,QEvent *event);
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  18. The following user says thank you to spirit for this useful post:

    aj2903 (14th February 2009)

  19. #15
    Join Date
    Sep 2008
    Posts
    84
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: help needed in QMouseEvent

    this was the real problem thanks spirit.

  20. #16
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: help needed in QMouseEvent

    Wow,

    first of all QWidget::setMouseTracking() is not needed. It is just for getting mouse move events which you aren't need. Then to use a QTimer is overkill because of all the signal and slots thing. Better use a simple QTime::currentTime (). Save the time stamp in press function and in the release function you just calculate the difference and if it is greater then 4 seconds emit a signal.


    Lykurg

  21. #17
    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: help needed in QMouseEvent

    Quote Originally Posted by Lykurg View Post
    Wow,

    first of all QWidget::setMouseTracking() is not needed. It is just for getting mouse move events which you aren't need. Then to use a QTimer is overkill because of all the signal and slots thing. Better use a simple QTime::currentTime (). Save the time stamp in press function and in the release function you just calculate the difference and if it is greater then 4 seconds emit a signal.


    Lykurg
    totally agree with you!
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  22. #18
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: help needed in QMouseEvent

    so that means using QTimer is more expensive than using QTime::currentTime()?

  23. #19
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: help needed in QMouseEvent

    Because I have too much time, or better to much work to get around, here is an example:

    Qt Code:
    1. #ifndef MYBUTTON_H
    2. #define MYBUTTON_H
    3.  
    4. #include <QtGui>
    5.  
    6. class myButton : public QPushButton
    7. {
    8. Q_OBJECT
    9. public:
    10. myButton(QWidget *parent = 0);
    11. void setMinimumHoldTime(int msec)
    12. {
    13. m_minimumHoldTime = msec;
    14. }
    15. int minimumHoldTime() const
    16. {
    17. return m_minimumHoldTime;
    18. }
    19. protected:
    20. void mousePressEvent(QMouseEvent *event);
    21. void mouseReleaseEvent(QMouseEvent *event);
    22. signals:
    23. void holdForMSec(int);
    24. private:
    25. QTime m_time;
    26. int m_minimumHoldTime;
    27. };
    28.  
    29. #endif // MYBUTTON_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mybutton.h"
    2.  
    3. myButton::myButton(QWidget *parent)
    4. : QPushButton(parent), m_time(), m_minimumHoldTime(4000)
    5. {}
    6.  
    7. void myButton::mousePressEvent(QMouseEvent *event)
    8. {
    9. m_time = QTime::currentTime();
    10. QPushButton::mousePressEvent(event);
    11. }
    12.  
    13. void myButton::mouseReleaseEvent(QMouseEvent *event)
    14. {
    15. int msec = m_time.elapsed();
    16. // Do time check in the button class
    17. if (m_minimumHoldTime < msec && rect().contains(event->pos()))
    18. emit holdForMSec(msec);
    19. // or let the reciver decide what to do
    20. // if (rect().contains(event->pos()))
    21. // emit holdForMSec(msec);
    22. QPushButton::mouseReleaseEvent(event);
    23. }
    To copy to clipboard, switch view to plain text mode 

    @talk2amulya: If you call QTimer::start(int) you start a new "event loop" (I don't know how QTimer is exactly implemented), to measure when the time is up and at which end a signal is emitted. And this continuous! Whereas QTime simple returns a time Object. Nothing more...

  24. The following user says thank you to Lykurg for this useful post:

    aj2903 (14th February 2009)

  25. #20
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: help needed in QMouseEvent

    hi lykurg,

    first of all, thanks for investing your time on this..i have one doubt abt what u said..does QTimer really start its own event loop..cuz as far as i've seen, exec() is the only way to start your own event loop..but its always good to know alternative ways to solve a problem..r u sure it starts its own event loop?

Similar Threads

  1. Qt4Libraries needed for using images
    By divya balachandran in forum Qt Programming
    Replies: 2
    Last Post: 23rd January 2009, 22:52
  2. Using Map in Qwt Widget..help needed
    By swamyonline in forum Qwt
    Replies: 2
    Last Post: 11th June 2008, 20:53
  3. Forward declarations needed for some?
    By doktorn in forum Newbie
    Replies: 6
    Last Post: 28th November 2007, 09:56
  4. How to get color of pixel or point by QMouseEvent
    By Krishnacins in forum Newbie
    Replies: 4
    Last Post: 28th May 2006, 02:46

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.