How can I distinguish wheel button click event from mouse press event? - qt

I am wondering how i can distinguish wheel click event from mouse press event. Because i want to do different handling for these two events in pyside. Currently, every time I click the wheel button, the event is catched by mousepressevent. Can anyone explain ?
Edit: I want to implement this in a subclass of
qglwidget class

From its name, the mousePressEvent is responsible for mouse clicks while the wheelEvent is for scrolling solely. The wheelEvent will not catch the wheel button click. It is how the Qt's API is designed when it comes to mouse events processing.
In order to separate which mouse button is pressed (right, wheel or left), use button property of QMouseEvent.
This is how the code would look like using C++ (I imagine it is easy to translate it to pyside)
void GLWidget::mousePressEvent(QMouseEvent *event) // redefine the mouse event
{
switch( event->button() ) {
case Qt::LeftButton:
// do stuff for left button pressed
break;
case Qt::MiddleButton:
// do stuff for wheel button pressed
break;
// ...
}
}
So, for pyside, you only need to compare the button property of event in mousePressEvent and see whether it is Qt.LeftButton , Qt.RightButton or Qt.MidButton. Hope that helps.

Related

How to hide a QWidget when mouse clicked on out side that widget

I want hide a QWidget when mouse clicks out of that widget just like it have a popup flag:
auto widget = new QWidget(this);
widget->setWindowFlag(Qt::Popup); // this widget will hide, when mouse click out of that widget.
For some reason, I can't set these flags and must implement some thing myself which behaves like this.
Or can I get a mouse event out of that widget?
Solution:
As I said, I can't use Qt flags and have to implement the similar behavior myself.My solution is installEventFilter to QApplication, in the override method eventFilter,I filter the QMouseEvent and send a signal.
Yes, you can get a mouse event out of the widget.
Make a custom widget and reimplement mousePressEvent, which will catch the last click (the ouside-the-widget click that hides the "popup"). But be careful to add the call to QWidget::mousePressEvent(event) in the end, otherwise the last click will be lost and your widget will remain onscreen.
CustomWidget::CutomWidget(QWidget *parent) : QWidget(parent)
{
setWindowFlags(Qt::Popup);
}
void CustomWidget::mousePressEvent(QMouseEvent *event)
{
if (!this->underMouse()) {
//if the click is not on the widget, i.e. if it's the click that hides it,
// you caught it, do what you want to do here.
}
QWidget::mousePressEvent(event);
}
Hope it helps.

ZKOSS: onClick() working differently for mouse click vs mouse tap

I am trying to disable double clicking a Help label which is an anchor to open the Help window.
<p:a id="helpClick" onClick="help()">
<label value="Help" style="color:#FFFFFF;" />
</p:a>
When onClick() event is triggered once, either by Mouse click or Tapping the touchpad once, the help() method is being invoked.
void help() {
flag = true;
this.helpClick.setDisabled(true);
Window popupWindow = null;
popupWindow = (Window) Executions.createComponents("/zul/mainHelp.zul",
null, null);
this.popupWindow.setClosable(true);
popupWindow.addEventListener("onClose", new EventListener() {
void onEvent(Event event) throws Exception {
this.helpClick.setDisabled(false);
}
});
}
is the code which i added to handle the anchor tag with the id helpClick.
This is working perfectly fine when i use mouse clicks. For the first click, the window gets opened and simultaneously the Label is not taking any more click events.
When i try the same with mouse tap(using the touchpad), two single clicks are being triggered.
I have used onClick() to capture the event.
I am trying to disable the Label once it is clicked and the window is opened. Only after the window gets closed, i am enabling the label.
This is working totally fine when i use mouse clicks but not when i use tap.
With tapping, the label is taking multiple clicks which isnt the case with Mouse Click.
Without seeing code it's difficult to provide advice but maybe you can capture the onDoubleClick event and ignore it or forward it to the to the same listener as your onClick event.
... forward="onClick=onHelpClick,onDoubleClick=onHelpClick" ...
After question edit:
It sounds like a bug if you can double click a disabled component. One thing you could try is set your link to autodisable <p:a id="helpClick" onClick="help()" autodisable="self"> as per A component documentation

Qt mousePressEvent left button

I am using Qt but have run into a problem.
I want to register when the mouse is pressed and released on a QTableWidget. There are signals only for pressed, not released. Therefore I use mousePressEvent and mouseReleaseEvent instead.
MyWidget.h
protected:
void mousePressEvent(QMouseEvent * event);
void mouseReleaseEvent(QMouseEvent * event);
MyWidget.cpp
void MyWidget::mousePressEvent(QMouseEvent *event)
{
qDebug() << "Pressed";
}
void MyWidget::mouseReleaseEvent(QMouseEvent *event)
{
qDebug() << "Released";
}
It prints "Released" when the left, right or middle mouse button is released over the table. However, it prints "Pressed" only when the right or middle button is pressed.
What could be wrong? How can I register when the left button is pressed?
The cell widget probably consumes the mousePressEvent on the left mouse button, so you (the parent widget) are not receiving it.
In the documentation of QMouseEvent, it is said that:
A mouse event contains a special accept flag that indicates whether the receiver wants the event. You should call ignore() if the mouse event is not handled by your widget. A mouse event is propagated up the parent widget chain until a widget accepts it with accept(), or an event filter consumes it.
Because of that, we can suppose that probably the events you mentioned are consumed by some other actors.
It would help knowing who are those actors.
I found the solution (with help from the other answers):
There was a QFrame on top of the widget that consumed the event. I didn't think of that since it worked for the other buttons. It was solved by:
ui->frame->setAttribute(Qt::WA_TransparentForMouseEvents, true);

How to detect a QGraphicsItem moving performing by mouse?

I have a child class of QGraphicsItem with Selectable and Movable flags. When I select many items and moving them, all recieves itemChange event. Is there any way to detect in itemChanged that mouse's button is still pressed?
Please refer to the QApplication::mouseButtons() function that will return the current state of the mouse buttons Qt::MouseButtons.
Qt::MouseButtons btns = QApplication::mouseButtons();
if (btns & Qt::LeftButton) {
// The left button is pressed.
[..]
}

How to catch QComboBox popup close event

A am using QComboBox derived class to show my items. My combo box is read only. But how can I catch the event when popup view of combo box closes?.For example, when user clicks a mouse button somewhere out of my combo box?
Thank you very much in advance.
What for do you want this event? If the QComboBox closes without selection nothing changed. The signals given will only be activated when a selection has been made.
If you insist on reading a "close-event", you could subclass focusOutEvent(QFocusEvent*) or use an event handler for the focus out event and emit a custom signal. Eventually you want to have a boolean flag set on hadEditFocus() before, so you can see if the dropdown would be opened.
Edit:
Eventually it would be easier to subclass and reimplement showPopup() and hidePopup() as:
void MyClass::showPopup()
{
QComboBox::showPopup();
emit signalPopupShown();
}
void MyClass::hidePopup()
{
QComboBox::hidePopup();
emit signalPopupHidden();
}
but I am not sure if hidePopup() gets called on focus-loose.

Resources