Results 1 to 15 of 15

Thread: Detect Button Press / Release in toolbar

  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,
    _

  11. #10
    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

    OK... I am a bit lost... ( Maybe more that a bit )

    I created the toolbar in QT Creator and added the actions to them.

    Where can I get pointerToToolButton from the QAction?

    Thanks again,
    Louis

    OK... I am a bit lost... ( Maybe more that a bit )

    I created the toolbar in QT Creator and added the actions to them.

    Where can I get pointerToToolButton from the QAction?

    Thanks again,
    Louis


    Added after 1 39 minutes:


    OK another step forward...

    I can add a QPushButton to the toolbar and get the pressed() and released() signals as follows...


    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QWidget>
    4. #include <QWidgetAction>
    5. #include <QPushButton>
    6. #include <QToolButton>
    7. #include <QDebug>
    8.  
    9.  
    10. MainWindow::MainWindow(QWidget *parent) :
    11. QMainWindow(parent),
    12. ui(new Ui::MainWindow)
    13. {
    14. ui->setupUi(this);
    15.  
    16. QPushButton* myPushButton = new QPushButton("Hi");
    17.  
    18. ui->mainToolBar->addWidget(myPushButton);
    19.  
    20. connect(myPushButton,SIGNAL(pressed()),this,SLOT(ShowButtonPressed()));
    21. connect(myPushButton,SIGNAL(released()),this,SLOT(ShowButtonReleased()));
    22. }
    23.  
    24. MainWindow::~MainWindow()
    25. {
    26. delete ui;
    27. }
    28.  
    29. void MainWindow::ShowButtonPressed()
    30. {
    31.  
    32. qDebug() << "Buttton Pressed";
    33. }
    34.  
    35. void MainWindow::ShowButtonReleased()
    36. {
    37. qDebug() << "Buttton Released";
    38. }
    To copy to clipboard, switch view to plain text mode 

    What I am missing is the link between the QAction created in Qt Creator and its object so I can use the pressed() and released() signals.

    At least I think that is what I am missing...

    Again, thanks for your help and patience.

    Louis
    Last edited by irish_guy@hotmail.com; 9th September 2015 at 18:30.

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

    Default <SOLVED>Re: Detect Button Press / Release in toolbar

    One more update...

    Changing the QPushButton to QToolButton and it works perfectly programmatically.

    Qt Code:
    1. QToolButton *qtbPushRelease = new QToolButton;
    2. qtbPushRelease->setIcon(QPixmap(":/images/button.png"));
    3.  
    4. ui->mainToolBar->addWidget(qtbPushRelease);
    5.  
    6. connect(qtbPushRelease,SIGNAL(pressed()),this,SLOT(ShowButtonPressed()));
    7. connect(qtbPushRelease,SIGNAL(released()),this,SLOT(ShowButtonReleased()));
    To copy to clipboard, switch view to plain text mode 

    So, if you have the object it works but how to get the object from the Action Editor in Qt Creator is still unknown to me.

    I can live with this but it would be nice to know how to achieve same result in Qt Creator.

    Thanks for your help. I really appreciate it.

    Can I mark as solved?

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

    Well, first, you don't need an action if you don't need any of the action features. QToolBar supports adding widgets for a reason.

    But if you have an action and want the toolbutton it is associated with, have a look at QToolBar::widgetForAction().

    In your case the direct option is of course better, why create an action that is then not used.

    Cheers,
    _

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

    irish_guy@hotmail.com (10th September 2015)

  15. #13
    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

    Hey... thank you. That is exactly what I was looking for...

    For anybody else who is interested in using Qt Creator and not in text, the solution works great. See code below. Replace 'actionOpen' with your action.

    To do it programmatically see last few posts..

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QWidget>
    4. #include <QWidgetAction>
    5. #include <QPushButton>
    6. #include <QToolButton>
    7. #include <QDebug>
    8.  
    9.  
    10. MainWindow::MainWindow(QWidget *parent) :
    11. QMainWindow(parent),
    12. ui(new Ui::MainWindow)
    13. {
    14. ui->setupUi(this);
    15.  
    16. QWidget *myWidgit;
    17.  
    18. myWidgit = ui->mainToolBar->widgetForAction(ui->actionOpen);
    19.  
    20. connect(myWidgit,SIGNAL(pressed()),this,SLOT(ShowButtonPressed()));
    21. connect(myWidgit,SIGNAL(released()),this,SLOT(ShowButtonReleased()));
    22. }
    23.  
    24. MainWindow::~MainWindow()
    25. {
    26. delete ui;
    27. }
    28.  
    29. void MainWindow::ShowButtonPressed()
    30. {
    31.  
    32. qDebug() << "Buttton Pressed";
    33. }
    34.  
    35. void MainWindow::ShowButtonReleased()
    36. {
    37. qDebug() << "Buttton Released";
    38. }
    To copy to clipboard, switch view to plain text mode 

  16. #14
    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

    Final post on this topic...

    Using widgetForAction method allows the detection of mouse presses as per 'stampede' post.


    Qt Code:
    1. QWidget *myWidgit;
    2.  
    3. myWidgit = ui->mainToolBar->widgetForAction(ui->actionZoom_In);
    4.  
    5. myWidgit->installEventFilter(&FO);
    To copy to clipboard, switch view to plain text mode 

    Thanks Guys...
    Louis

  17. #15
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Detect Button Press / Release in toolbar

    Yes, but as anda_skoa said previously, in this case you can get all you want using signals and slots. Installing an event filter is overkill and runs the risk of disrupting the normal behavior of the widget if you don't handle the events properly. Typically the only time you would resort to an event filter is if you want to add to / override or otherwise change the built-in behavior of a widget and you need to get down into the guts of event management to accomplish it.

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.