Results 1 to 12 of 12

Thread: do something while a button is pressed

  1. #1
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default do something while a button is pressed

    I am willing to regularly call an instruction while the left mouse is pressed on a button. I tried to do this with pressed or clicked and taking care of the mouse left button by defining a QMouseEvent inside of the slot.

    Qt Code:
    1. if (event->buttons() & Qt::LeftButton)
    2. {
    To copy to clipboard, switch view to plain text mode 

    there is also no way to pass the qmouseevent as a parameter of clicked or pressed. how can I solve this?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: do something while a button is pressed

    If you need to check the event itself, you'll have to overwrite mousePressEvent()

    Cheers,
    _

  3. #3
    Join Date
    Sep 2013
    Posts
    33
    Thanks
    5
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Post Re: do something while a button is pressed

    You have to implement mouse press event as well as click slot.If you want the same functionality, then call the same function from both.

    i.e=

    slot_btn_clkd()
    {
    taskMethod();
    }
    keyPressEvent(QKeyEvent* event)
    {
    if( event->key() == Qt::Key_Up ){ taskMethod(); }
    else event->accept();
    }

    taskMethod()
    {

    //Do stuffs here
    }

  4. #4
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: do something while a button is pressed

    Quote Originally Posted by blue_sky View Post
    You have to implement mouse press event as well as click slot.If you want the same functionality, then call the same function from both.

    i.e=

    slot_btn_clkd()
    {
    taskMethod();
    }
    keyPressEvent(QKeyEvent* event)
    {
    if( event->key() == Qt::Key_Up ){ taskMethod(); }
    else event->accept();
    }

    taskMethod()
    {

    //Do stuffs here
    }
    Negative. Think about where you saying wrong.


    Added after 6 minutes:


    Quote Originally Posted by anda_skoa View Post
    If you need to check the event itself, you'll have to overwrite mousePressEvent()

    Cheers,
    _
    Yes, I need to overwrite it. But, I need to tell it a scenario in which mouse left button is pressed and also the very button is also clicked. I do not know how to mention button click from inside mousepressevent. However, there are some ways to do that, but I don't consider them the main solutions.
    Last edited by saman_artorious; 11th November 2013 at 08:24.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: do something while a button is pressed

    I am not sure what the problem is, all mouse events got through the mouse event handler methods.

    Maybe you could describe what happens, i.e. what is the order of user interactions you need to discover.

    Cheers,
    _

  6. #6
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: do something while a button is pressed

    Quote Originally Posted by anda_skoa View Post
    I am not sure what the problem is, all mouse events got through the mouse event handler methods.

    Maybe you could describe what happens, i.e. what is the order of user interactions you need to discover.

    Cheers,
    _
    imagine I have a few button. How would you identify which button was clicked inside mousePressEvent? otherwise, if I click any mouse event the event would trigger for any buttons clicked!

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: do something while a button is pressed

    Quote Originally Posted by saman_artorious View Post
    imagine I have a few button. How would you identify which button was clicked inside mousePressEvent?
    I am afraid I still don't understand what you are trying to do.
    Each button has its own mousePressEvent handler, so it is obvious which one received the mouse event.

    Also, each button has its own clicked signal, so you can determined which one was clicked even without custom event handling.

    Cheers,
    _

  8. #8
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: do something while a button is pressed

    Quote Originally Posted by anda_skoa View Post
    I am afraid I still don't understand what you are trying to do.
    Each button has its own mousePressEvent handler, so it is obvious which one received the mouse event.

    Also, each button has its own clicked signal, so you can determined which one was clicked even without custom event handling.

    Cheers,
    _
    but there is only one mousePressEvent() and that is for my MainWindow class. If I write my function inside mousePressEvent(), it is triggered each time user hold the left mouse button. But, this event occurs for every click on the ui! I want it to be only for one button. while that button is clicked and the mouse left button is held, do the task.
    blue_sky suggested to call the task function both in the click signal SLOT() and also in the mousePressEvent. But this way is wrong, because you have no idea which button was clicked and for every mouse left button event the task is executed.

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

    Default Re: do something while a button is pressed

    I don't know if I understand correctly, but since by default you can press the QPushButton only with left mouse button, try:
    Qt Code:
    1. connect(btn, SIGNAL(pressed()), obj, SLOT(startWork()));
    2. connect(btn, SIGNAL(released()), obj, SLOT(stopWork()));
    To copy to clipboard, switch view to plain text mode 
    in order to protect yourself from the scenario:
    - user clicked left, but not yet released
    - mouse pointer left the widgets boundaries while user is holding the btn down
    you can reimplement button's leaveEvent and stop the work there too.
    I don't really remember if the released() wouldn't be called anyway if mouse pointer is outside the button, but that's quite easy to test.

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: do something while a button is pressed

    Quote Originally Posted by saman_artorious View Post
    but there is only one mousePressEvent() and that is for my MainWindow class.
    No.
    mousePressEvent() is a virtual method of QWidget, not just of QMainWindow. All widgets have that one. How do you think QPushButton is implementing mouse clicks?

    Quote Originally Posted by saman_artorious View Post
    If I write my function inside mousePressEvent(), it is triggered each time user hold the left mouse button. But, this event occurs for every click on the ui! I want it to be only for one button.
    Might make sense to implement the mousePressEvent of the widget where you want to detect it, no?

    Cheers,
    _

  11. #11
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: do something while a button is pressed

    so, for that I need to first define a class inheriting a qpushbutton, and declare my qpushbutton of that type, then re implement the qMouseEvent inside my class for the button, right?

  12. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: do something while a button is pressed

    Yes, that would be the cleanest option.

    Cheers,
    _

Similar Threads

  1. I press a button and receive 2 pressed() signals
    By JuanMO in forum Qt Programming
    Replies: 4
    Last Post: 21st November 2010, 06:58
  2. Replies: 6
    Last Post: 4th October 2010, 03:19
  3. Raise key pressed event when button is clicked?
    By newstead in forum Qt Programming
    Replies: 7
    Last Post: 5th June 2009, 13:12
  4. Replies: 2
    Last Post: 13th May 2009, 19:01
  5. Getting the row for button pressed
    By steg90 in forum Qt Programming
    Replies: 2
    Last Post: 28th November 2007, 15:45

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.