PDA

View Full Version : Detect Button Press / Release in toolbar



irish_guy@hotmail.com
8th September 2015, 19:40
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

stampede
8th September 2015, 20:53
Install event filter (http://doc.qt.io/qt-4.8/qobject.html#eventFilter) on the button object and wait for QEvent::MouseButtonPress (http://doc.qt.io/qt-4.8/qevent.html#Type-enum) and MouseButtonRelease events.

anda_skoa
8th September 2015, 23:21
A QToolButton is a QAbstractButton, it has a QAbstractButton::pressed() and QAbstractButton::released() signal.

Cheers,
_

irish_guy@hotmail.com
9th September 2015, 11:14
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

anda_skoa
9th September 2015, 11:39
Why do you use an event filter?
What is the reason you are not using the signals of the button?

Cheers,
_

irish_guy@hotmail.com
9th September 2015, 13:41
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

anda_skoa
9th September 2015, 14:38
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:


connect(pointerToToolButton, SIGNAL(pressed()), pointerToReceiver, SLOT(someSlotNameHandlingPress()));

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

irish_guy@hotmail.com
9th September 2015, 16:00
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::pressed()

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

QObject::connect: No such signal QAction::pressed() 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

anda_skoa
9th September 2015, 16:31
Ah, yes, I should have written QToolButton, not QAction.

Oh, wait, I did.

Cheers,
_

irish_guy@hotmail.com
9th September 2015, 18:31
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...



#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWidget>
#include <QWidgetAction>
#include <QPushButton>
#include <QToolButton>
#include <QDebug>


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

QPushButton* myPushButton = new QPushButton("Hi");

ui->mainToolBar->addWidget(myPushButton);

connect(myPushButton,SIGNAL(pressed()),this,SLOT(S howButtonPressed()));
connect(myPushButton,SIGNAL(released()),this,SLOT( ShowButtonReleased()));
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::ShowButtonPressed()
{

qDebug() << "Buttton Pressed";
}

void MainWindow::ShowButtonReleased()
{
qDebug() << "Buttton Released";
}

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

irish_guy@hotmail.com
9th September 2015, 19:02
One more update...

Changing the QPushButton to QToolButton and it works perfectly programmatically.


QToolButton *qtbPushRelease = new QToolButton;
qtbPushRelease->setIcon(QPixmap(":/images/button.png"));

ui->mainToolBar->addWidget(qtbPushRelease);

connect(qtbPushRelease,SIGNAL(pressed()),this,SLOT (ShowButtonPressed()));
connect(qtbPushRelease,SIGNAL(released()),this,SLO T(ShowButtonReleased()));

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?

anda_skoa
9th September 2015, 21:58
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,
_

irish_guy@hotmail.com
9th September 2015, 22:15
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..


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWidget>
#include <QWidgetAction>
#include <QPushButton>
#include <QToolButton>
#include <QDebug>


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

QWidget *myWidgit;

myWidgit = ui->mainToolBar->widgetForAction(ui->actionOpen);

connect(myWidgit,SIGNAL(pressed()),this,SLOT(ShowB uttonPressed()));
connect(myWidgit,SIGNAL(released()),this,SLOT(Show ButtonReleased()));
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::ShowButtonPressed()
{

qDebug() << "Buttton Pressed";
}

void MainWindow::ShowButtonReleased()
{
qDebug() << "Buttton Released";
}

irish_guy@hotmail.com
10th September 2015, 08:14
Final post on this topic...

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



QWidget *myWidgit;

myWidgit = ui->mainToolBar->widgetForAction(ui->actionZoom_In);

myWidgit->installEventFilter(&FO);

Thanks Guys...
Louis

d_stranz
11th September 2015, 15:46
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.