Double Click Signal
-
I am making an app where I need to know when a slider is double clicked. When I use the QWidget signal: mouseDoubleClickedEvent, the programs errors and says that it is a protected function. I am new to c++ and can’t find documentation on this issue. I know this is very simple but please help!
-
@ostrichking371 said in Double Click Signal:
QWidget signal: mouseDoubleClickedEvent,
This is a function, not a signal. You have to reimplement this function when you want custom functionality.
-
@Christian-Ehrlicher again…new to c++. What does this mean? Aren’t signals just empty functions that can be detected by other widgets when they get called? I mean, the EMIT macro is empty, it just calls the function.
-
@ostrichking371 said in Double Click Signal:
Aren’t signals just empty functions that can be detected by other widgets when they get called?
No, see https://doc.qt.io/qt-6/signalsandslots.html
And for overriding see https://www.simplilearn.com/tutorials/cpp-tutorial/function-overriding-in-cpp
-
@Christian-Ehrlicher so they are functions... "Signals are public access functions" (Qt doc). I still dont understand how i can detect double clicking and emit a signal.
-
@ostrichking371
BecauseQWidget
does not itself emit a signal for this you must:- Subclass
QWidget
precisely so that you can... - ...
override
theprotected QWidget::mouseDoubleClickEvent()
. - Declare a function named, say,
doubleClicked()
, in thesignals:
section of the class declaration in the.h
file. emit doubleClicked()
in the body of your overriddenmouseDoubleClickEvent()
.connect()
a slot to that signal just like you would for any other inbuilt Qt signal.
- Subclass
-
@ostrichking371
In principle that should be right, what does "still doesn't work" mean? You must explain this sort of phrase when asking for help. First thing is to make sure theemit
line is being hit.QAdvSlider
Try not to start your own class names with
Q
, leave that to Qt itself. -
@JonB I fixed it!! The signal is emitted at double click, but only if i click the blank areas. If i double click the slider handle, it doesnt work, but this is what i want. Is it possible to have it work when you double click the slider handle.
-
@ostrichking371 said in Double Click Signal:
Is it possible to have it work when you double click the slider handle.
Yes, do the same: subclass QSlider, override mouseDoubleClickEvent (don't forget to call mouseDoubleClickEvent from QSlider!), emit the signal there and use your subclass instead of QSlider.
-
@jsulm said in Double Click Signal:
Yes, do the same: subclass QSlider, override mouseDoubleClickEvent (don't forget to call mouseDoubleClickEvent from QSlider!), emit the signal there and use your subclass instead of QSlider.
I think the focus was on "if I double click the slider handle"...
ThemouseEvents
are sent when clicking the widget itself, but when clicking the handle sub-control,sliderPressed()
is emitted, which will "eat" the mouse clicks.@ostrichking371 One thing you could do is employing the
sliderPressed()
signal to start a timer and measure the time span until the signal is emitted for the second time. If it's in your desired double-click range, do what you are trying to do afterwards, else do nothing so it's treated as single click to grab the slider handle.