How to detect if both mouse buttons are pressed in Qt? - c++

Right now I'm able to detect when only one button is clicked. But I want to detect when both are pressed together. The purpose is to select some items from a QTableView. I'm trying to select them in such a way that when left button is clicked on an item while right button is already kept pressed then the item will be among the selected.
The following code only shows the message that the right button is clicked, but it doesn't show that both the buttons are clicked. How can I manage to detect when both of them are clicked?
bool MainWindow::eventFilter(QObject* obj, QEvent *ev)
{
if(obj = ui->listOfImages->viewport())
{
QMouseEvent * mouseEv = static_cast<QMouseEvent*>(ev);
if(mouseEv->buttons() == Qt::RightButton)
{
qDebug()<<"Right Button clicked!";
if(mouseEv->buttons() == Qt::LeftButton)
{
qDebug()<<"Both button clicked!";
return QObject::eventFilter(obj,ev);
}
}
}
return QObject::eventFilter(obj,ev);
}
Thanks.

Try
if( (mouseEv->buttons() & Qt::RightButton) &&
(mouseEv->buttons() & Qt::LeftButton) )
{
...
}
Hint:
When you just checked buttons() is equal to Qt::RightButton, how could it be equal to Qt::LeftButton?

The QMouseEvent::buttons() function returns a combination of OR'd states of the mouse buttons.
Therefore, to test the left button is pressed, you should be doing this: -
if(mouseEv->buttons() & Qt::LeftButton)
and for the right button: -
if(mouseEv->buttons() & Qt::RightButton)
As the Qt docs state: -
For mouse press and double click events this includes the button that caused the event. For mouse release events this excludes the button that caused the event.
So you can keep track of when buttons are held down and released.

Related

Qt: accept mouseRelease only when there was no drag and drop

I have a subclass of QGraphicsView that should accept two kinds of mouse events: drag and drop for scrolling and simple clicks for item selection/highlight.
So I use
setDragMode(QGraphicsView::ScrollHandDrag);
to enable scrolling the view with the "Hand". And I have a function like this:
void GraphView::mouseReleaseEvent(QMouseEvent* e)
{
if (e->button() == Qt::LeftButton)
emit leftClicked(mapToScene(e->pos()));
else
emit rightClicked(mapToScene(e->pos()));
QGraphicsView::mouseReleaseEvent(e);
}
which creates signal whenever the user clicks on the scene.
However, the problem is: when I stop dragging and release the mouse button, the mouseReleaseEvent function is called, and if the cursor happens to be over some element of the scene, it will get highlighted.
How can I changed the mouseReleaseEvent function so that the signals are created only if there was no previous drag of the mouse?
If you use mousePress and mouseMove in combination with mouseRelease, then you can determine what mouse action the user just performed.
If you have mousePress then mouseRelease, then it must be a simple click.
If you have mousePress, mouseMove, and then mouseRelease, then it must be a drag.
The Qt documentation contains an example of interpreting combinations of mouse events in action in a scribbling program.
http://doc.qt.io/qt-4.8/qt-widgets-scribble-example.html
You can extend the principle to something like this:
private bool MyWidget::dragging = false;
private bool MyWidget::pressed = false;
void MyWidget::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
pressed=true;
}
QGraphicsView::mousePressEvent(event)
}
void MyWidget::mouseMoveEvent(QMouseEvent *event)
{
if ((event->buttons() & Qt::LeftButton) && pressed)
{
dragging=true;
}
QGraphicsView::mouseMoveEvent(event)
}
void MyWidget::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton && pressed) {
pressed = false;
if (dragging)
{
dragging = false;
}
else
{
// plain mouse click
// do something here
}
}
QGraphicsView::mouseReleaseEvent(event)
}
Note that this does not address edge cases where a user's mouse action is performed only partially inside the widget. I must also admit that I am relatively new to Qt and have not yet used ScrollHandDrag, but this is how one would go about identifying a certain combination of mouse events.

Qt - mouse events on a QLabel

I've got this test piece of code in mainwindow.cpp:
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::MouseMove)
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
qDebug() << QString("Mouse move (%1,%2)").arg(mouseEvent->pos().x()).arg(mouseEvent->pos().y());
}
return false;
}
I just want to get mouse position when clicked, and send the coordinates to another thread with an OpenCV loop that'll pull HSV information and do things accordingly. I'm using mouse over just for testing.
The problem is that I have no idea how to attach this (tracking, clicking) to a QLabel labelKalibracja, one I use to display video frames, instead of the whole window.
ui->labelKalibracja->installEventFilter(this);
is supposed to work, but doesn't, but
qApp->->installEventFilter(this);
Will make the whole window a mouse track zone.
You should check the object of the event filter :
if (qobject_cast<QLabel*>(obj)==ui->labelKalibracja && event->type() == QEvent::MouseMove)
{
...
}
Now you can make sure that the event is for the label. Note that the event filter could be installed on multiple objects and it's your duty to identify the combination of objects and events.

How to install event filters

I have a custom widget A within another custom widget B. Widget A normally has a mousePressEvent where I open a context menu. But now as it is in widget B, I want to open widget B's context menu and intercept the event so that only B knows it.
If I use this code in class B after adding an instance of A to B:
this->installEventFilter(instanceOf_A);
Or
instanceOf_A->installEventFilter(this);
Where
A::eventFilter(QObject *obj, QEvent *e){
if (e->type() == QEvent::MouseButtonPress){
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(e);
if (mouseEvent->button() == Qt::RightButton){
qDebug()<<" Right mouse button press detected. \
Return true, now child should not get the mouse event";
return true;
}
}
Nothing works. Also return false , e->accept(), e->ignore() don't work or show any impact. How do you setup an eventFilter? Where? In A or B?
EDIT: replaced the e->MouseButtonPress

Qt how to check which mouse button is pressed

I have problems in PySide while trying to determine which mouse button is pressed in event function. I need it in particular for ignoring mouse move event, because it's doing job on both mouse buttons, left and right.
I want to ignore mouse move event if the right button on scene is pressed. Any help?
All of mouse events have two methods (button and buttons) to determine which of buttons are pressed. But for only move event the documentation says:
Note that the returned value is always Qt::NoButton for mouse move events.
for mouseMoveEvent you should use buttons method.
void mouseMoveEvent(QMouseEvent *e)
{
if(e->buttons() == Qt::RightButton)
qDebug() << "Only right button";
}
In order to ignore move events you need to do this work in eventFilter of course.
QApplication::mouseButtons() will return the status of mouseButton,
so, you can get the status of mouse in KeyPressEvent.
you can check, which mouse button is pressed via Qt::RightButton. Sorry for c++ code, but i hope, you would understand idea anyway:
void mousePressEvent(QMouseEvent *event)
{
if (event->button()==Qt::RightButton){
qDebug() << "right button is pressed
}
}
You could use a boolean:
void mousePressEvent(QMouseEvent *event)
{
if (event->button()==Qt::RightButton){
qDebug() << "right button is pressed
pressed=true; //<-----
}
}
and on mouseMoveEvent
void GLWidget::mouseMoveEvent(QMouseEvent *event)
{
float dx = event->x() - lastPos.x(); // where lastpos is a QPoint member
float dy = event->y() - lastPos.y();
if (dx<0) dx=-dx;
if (dy<0) dy=-dy;
if (event->buttons() & Qt::LeftButton) { //if you have MOVEd
...do something
}
if (event->buttons() & Qt::RightButton) {
if (pressed==true) return;
else{
...do
}
}
}
On mouserelease you have to set pressed=false; ( "pressed" must be a member of the class)
Hope it helps,let me know

QIcon inside combobox

I want to include a "remove" icon on entries in my QComboBox, but I am having trouble catching the mouse press event. I've tried to catch it on the combobox, and I've tried reimplemting the QIcon class to catch the mousepress there. No dice. Does anybody know how to do this?
-D
I've written code a bit like this, where I wanted to put a tree view inside a combo box and I needed to take an action when the check box on the tree was clicked. What I ended up doing was installing an event filter on the combo box to intercept mouse clicks, figure out where the mouse click was happening, and then take an action. Probably you can do the same kind of thing with your icon. Here is the code:
bool TreeComboBox::eventFilter(QObject* object, QEvent* event)
{
if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease)
{
QMouseEvent* m = static_cast<QMouseEvent*>(event);
QModelIndex index = view()->indexAt(m->pos());
QRect vrect = view()->visualRect(index);
if(event->type() == QEvent::MouseButtonPress &&
(model()->flags(index) & Qt::ItemIsUserCheckable) &&
vrect.contains(m->pos()))
{
// Your action here
ToggleItem(index);
UpdateSelectionString();
}
if (view()->rect().contains(m->pos()))
skipNextHide = true;
}
return QComboBox::eventFilter(object, event);
}
Maybe you can reimplement QComboBox::mousePressEvent(QMouseEvent *e) and use e.x() together with QComboBox::iconSize() to find if the event occurred over the icon.
This will off cause break if a Qt style decides to switch label and icon position in combo boxes. Don't know if that is possible?