In Qt, how to make a slider move event fire when using the arrow keys? - qt

I have a slider:
QSlider *mySlider;
And I have a slot wired to it:
void on_mySlider_sliderMoved(int position);
It's wired in setupUI when this is called:
QMetaObject::connectSlotsByName(MainWindow);
When I slide the slider with the mouse, this slot fires correctly and all is well. However, if I click it once with the mouse to "select" it and then use the arrows on the keyboard, I see the slider moving, but the slot never fires.
What am I doing wrong?

QAbstractSlider::sliderMoved signal is emitted when you drag the slider. From the docs: This signal is emitted when sliderDown is true and the slider moves. This usually happens when the user is dragging the slider.
Use QAbstractSlider::valueChanged signal if you want to know every time the value of the slider is changed.

Related

Simulate pushbutton Clicked

I write code in QT to detect mouse clicks in server and simulate them in client side. I can detect click on buttons as left click and in client, it clicks on position of button but client doesn't realize it as a push button click and related slot doesn't run.
In client it is realized as a left click and mouseReleaseEvent function being ran. how code can detect it as a pushbutton click?
Try to use Qt UI testing:
http://doc.qt.io/qt-5/qtest.html It helpful for generate keyboard and mouse events.
QWidget widget;
QTest::mouseClick(&widget, Qt::LeftButton, Qt::NoModifier, QPoint(clicked,point));

Remap context menu call on qwidget

I have my custom widget inherited from QWidget, and I've connected the widget's menu-calling signal to my slot.
connect(m_ontologyView, SIGNAL(customContextMenuRequested(QPoint)), SLOT(showContextMenuSlot(QPoint)));
Now I want user to be able to change button calling the context menu. Normally it's called with right mouse button, but how do I tell the widget to call the menu with a button of my choice?
I'm on Qt 5.4.0
Instead of using QWidget::customContextMenuRequested, you will need to reimplement the widgets mouse event functions, QWidget::mousePressEvent, QWidget::mouseReleaseEvent and QWidget::mouseMoveEvent. Inside of these events, you can then show you menu using QMenu::popup. (The point can be extracted from the mouse events).

What SIGNAL is emitted in Qt when pressing the "X" on the top right corner to terminate the Window?

Which SIGNAL is emitted, when pressing the "X" in the top right corner of a Qt Window application?
I have a second QDialog widget beside my MainWindow in my Qt application. And I would like to intercept the press on the "X" in my second QDialog, how?
The QCloseEvent class contains parameters that describe a close event. Close events are sent to widgets that the user wants to close, usually by choosing "Close" from the window menu, or by clicking the X title bar button. They are also sent when you call QWidget::close() to close a widget programmatically.
To ignore it you can call ignore() method:
void YourDialog::closeEvent(QCloseEvent* iEvent)
{
// ignore close event
iEvent->ignore();
}
There is no signal for that, you need to reimplement QWidget::closeEvent() or install an event filter filtering for QCloseEvents.

How to check if the mouse has really left the QWidget?

Consider a QWidget window, what event is triggered when the mouse has left this window?
The window has QLineEdit fields on it and they have completers (QCompleter) for input suggestions. The actual goal is to make such an (open) completer disappear when the mouse leaves the window. This is mostly because in some environments, moving the mouse over a different window may focus that other window, but keyboard events are still sent to the QLineEdit field (even though its parent window isn't focused anymore), which is confusing.
I could implement QWidget::leaveEvent(QEvent *event) (in the window), find the currently shown completer popup and hide it, that closes the popup. But ironically, leaveEvent() is also triggered when the mouse is moved over that popup - hiding it (making it impossible to click on an item in that popup). I guess that makes sense because the popup is a different QWidget, even though the popup is indirectly owned by the window.
So how do I check if the mouse has actually left the QWidget window?
Reimplement QWidget::leaveEvent(QEvent *event) in your derived class but start by checking that rect().contains(mapFromGlobal(QCursor::pos())) is true.
If not, return without doing anything.
This should filter out all the events where the mouse is still over your widget.
Hope it helps!

Qt: Can mouse event handlers block one another?

I have a simple parent widget that reimplements mousePressEvent/mouseReleaseEvent. The parent's child widgets use enterEvent/leaveEvent. When I hover the mouse over the child widgets, leaveEvent/enterEvent executes, but when I click and hold the mouse, mousePressEvent executes, but enterEvent/leaveEvent goes silent (in other words, no click and drag). Any ideas about what could be causing this?
If you press and hold down the mouse button on a widget then that widget grabs mouse events until you release the button. This is not a special feature of Qt, you can find similar behaviour in every other GUI APIs I know.
Take a look at the relevant part of the Qt documentation:
QWidget / Events:
mousePressEvent() is called when a mouse button is pressed while the
mouse cursor is inside the widget, or when the widget has grabbed the
mouse using grabMouse(). Pressing the mouse without releasing it is
effectively the same as calling grabMouse().
void QWidget::grabMouse ():
Grabs the mouse input. This widget receives all mouse events until
releaseMouse() is called; other widgets get no mouse events at all.

Resources