Results 1 to 15 of 15

Thread: Detect Button Press / Release in toolbar

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2014
    Posts
    8
    Thanks
    2
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Detect Button Press / Release in toolbar

    Hi,
    I am using a toolbar in an application and it works great for 'normal operations'.

    I am having problems because I need to call one function when a button is pressed and another function when a button is released.

    I would like it act like a QPushButton with the Pressed and Released signals.

    Is this possible?

    If so, any ideas / hints would be very much appreciated.

    Thanks,
    Louis

  2. #2
    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: Detect Button Press / Release in toolbar

    Install event filter on the button object and wait for QEvent::MouseButtonPress and MouseButtonRelease events.

  3. #3
    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: Detect Button Press / Release in toolbar

    A QToolButton is a QAbstractButton, it has a QAbstractButton::pressed() and QAbstractButton::released() signal.

    Cheers,
    _

  4. #4
    Join Date
    Jul 2014
    Posts
    8
    Thanks
    2
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Detect Button Press / Release in toolbar

    Hi Guys and thanks for your replies...

    I have made some progress with the event filter...

    If I do this in mainwindow.cpp

    FilterObject FO;

    ui->mainToolBar->installEventFilter(&FO);

    and then in FilterObject

    bool FilterObject::eventFilter(QObject *object, QEvent *event)
    {
    if (event->type() == QEvent::MouseButtonPress) {
    qDebug() << "Mouse Press On Toolbar";
    return true;
    }

    return false;
    }

    I can detect the mouse press on the toolbar which is great.

    However when I try to detect the mouse pressed from a button / action on the toolbar it no longer works

    I replace ui->mainToolBar->installEventFilter(&FO); with ui->actionDoWork->installEventFilter(&FO);

    The action DoWork is a button generated by the Action Editor.

    So basically it works to detect mouse events on the toolbar but not from an action button on the toolbar.

    What I really need is to read the mouse events from the buttons on the toolbar.

    Again, thanks and any ideas / hints are welcome.

    Regards,
    Louis

  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: Detect Button Press / Release in toolbar

    Why do you use an event filter?
    What is the reason you are not using the signals of the button?

    Cheers,
    _

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

    irish_guy@hotmail.com (10th September 2015)

  7. #6
    Join Date
    Jul 2014
    Posts
    8
    Thanks
    2
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Detect Button Press / Release in toolbar

    Thanks Anda,

    I used the event filter because I was unable to figure out how to use these signals of the button on a toolbar.

    I can use the signals from a normal push button but I can't understand how to get the same signals from the toolbar buttons.

    Is there any example of using the QAbstractButton in this way or a similar way?

    I am new to this so sorry if it is obvious....

    Thanks Again,
    Louis

  8. #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: Detect Button Press / Release in toolbar

    Quote Originally Posted by irish_guy@hotmail.com View Post
    I can use the signals from a normal push button but I can't understand how to get the same signals from the toolbar buttons.
    There is no difference. The signal/slot mechanism works the same way for all QObject subclasses

    With SIGNAL/SLOT macros:
    Qt Code:
    1. connect(pointerToToolButton, SIGNAL(pressed()), pointerToReceiver, SLOT(someSlotNameHandlingPress()));
    To copy to clipboard, switch view to plain text mode 
    The circumstance that the button is on a toolbar doesn't change anything, connect() doesn't even care that the sender is a QWidget derived class.

    Cheers,
    _

  9. #8
    Join Date
    Jul 2014
    Posts
    8
    Thanks
    2
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Detect Button Press / Release in toolbar

    Thanks again & Almost there I think...

    When I try to connect the signals and slots... I get a run time message that there is No such Signal QAction:ressed()

    connect(ui->actionZoom_In, SIGNAL(pressed()), this, SLOT(MousePress()));

    QObject::connect: No such signal QAction:ressed() in ../CameraControlGUI/mainwindow.cpp:66
    QObject::connect: (sender name: 'actionZoom_In')
    QObject::connect: (receiver name: 'MainWindow')

    When I connect to the 'triggered signal'

    connect(ui->actionZoom_In, SIGNAL(triggered()), this, SLOT(MousePress()));

    it works fine but I really need pressed and released...

    Regards
    Louis

  10. #9
    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: Detect Button Press / Release in toolbar

    Ah, yes, I should have written QToolButton, not QAction.

    Oh, wait, I did.

    Cheers,
    _

Similar Threads

  1. how to capture the F1 - key press/release event
    By sajis997 in forum Qt Quick
    Replies: 1
    Last Post: 6th January 2015, 22:25
  2. Mouse Press/Release Events
    By Vivek1982 in forum Newbie
    Replies: 27
    Last Post: 22nd August 2014, 13:17
  3. Want to detect keyboard key press other than modifier keys
    By Rajesh.Rathod in forum Qt Programming
    Replies: 3
    Last Post: 25th October 2013, 07:19
  4. Replies: 6
    Last Post: 21st August 2010, 21:09
  5. 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

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.