I'm using Qt Creator to create a gui for a mineseeper game. How can I know a QpushButton clicked with rightclick? for flag in the game. In other word, which signal used for rightclick?
相关问题
- QML: Cannot read property 'xxx' of undefin
- QTextEdit.find() doesn't work in Python
- QT Layouts, how to make widgets in horizontal layo
- QT GUI freezes even though Im running in separate
- QGraphicsView / QGraphicsScene size matching
相关文章
- ubuntu20.4中c#通过c++库调用python脚本
- Qt槽函数自动执行多遍
- Is there a non-java, cross platform way to launch
- How to get a settings storage path in a cross-plat
- How to set the font size of the label on pushbutto
- Why doesn't valgrind detect a memory leak in m
- QTreeView remove decoration/expand button for all
- qt界面拥挤
I just wrote this little helper adapter to make any existing button right-clickable with no need to subclass it:
Usage:
From now on, the
clicked
signal will be triggered by the right click as well as left click. There's no need to delete this object - it usesui->pushButton
as parent and will be auto-deleted by Qt when the parent is destroyed.Obviously, you can write 2 lines of code (literally) to declare a new signal here and emit that signal upon right click instead of
clicked
, if desired.I think QPushButton is internally implemented to listen to left mouse clicks only. But you can easily extend QPushButton and re-implement let's say the mouse release event and do your thing if the right mouse button was pressed, e.g. emit a custom
rightClicked()
signal for example:... or you can create an overload of the clicked signal that forwards the mouseEvent pointer so you can do the same check outside of the button.
Then you do the check in the slot you connect the button's
clicked(QMouseEvent *)
signal to and proceed accordingly.Create your own button with filter at mousePressEvent slot.
qrightclickbutton.h
qrightclickbutton.cpp
Now connect like this
Create a slot in MainWindow.cpp.
It works for me!