問題描述
我使用 Qt Designer 開發了一個相當復雜的 GUI 工具.
有關該工具的更多詳細信息,請參閱:
然后點擊添加按鈕,然后點擊提升按鈕.
對于另一個QDoubleSpinBox
,右鍵單擊并選擇DoubleSpinBox
選項所在的新Promote To選項.
您可以在這里找到一個示例
I have developed a fairly complex GUI tool using the Qt Designer.
For more details about the tool see: https://github.com/3fon3fonov/trifon
I have defined many QDoubleSpinBox entries and by default the Qt Designer sets their right-click menu policy to:
setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
Now I want to add few more actions to this menu, but I simply cannot understand how this works! There is nothing in the Qt Designer which will allow me to make a "CustomContextMenu". I understand that for this I may need some coding (with which I will need help, and thus I am asking for help here), but I also need to make it globally for all SpinBox-es.
Sorry for not posting the code since it is fairly large for this form. If interested, please look at the github under "gui.py". However, there and in the .ui file there is no sign of any possibility to control the contextmenu policy for these buttons. Instead I am posting an image of the tool (sorry for the bad image but PrtSc does not seem to work when the right button in clicked and the menu is displayed)
see GUI image here
As we want to add a QAction
to the default context menu we first overwrite the contextMenuEvent
event and use a QTimer
to call a function that filters the toplevels
and get the QMenu
that is displayed and there we add the QAction
:
doublespinbox.py
from PyQt5 import QtCore, QtWidgets
class DoubleSpinBox(QtWidgets.QDoubleSpinBox):
minimize_signal = QtCore.pyqtSignal()
def __init__(self, *args, **kwargs):
super(DoubleSpinBox, self).__init__(*args, **kwargs)
self.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
def contextMenuEvent(self, event):
QtCore.QTimer.singleShot(0, self.add_actions)
super(DoubleSpinBox, self).contextMenuEvent(event)
@QtCore.pyqtSlot()
def add_actions(self):
for w in QtWidgets.QApplication.topLevelWidgets():
if isinstance(w, QtWidgets.QMenu) and w.objectName() == "qt_edit_menu":
w.addSeparator()
minimize_action = w.addAction("minimize this parameter")
minimize_action.triggered.connect(self.minimize_signal)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = DoubleSpinBox()
w.show()
sys.exit(app.exec_())
To use DoubleSpinBox in Qt Designer, first place doublespinbox.py next to your .ui:
├── ..
├── rvmod_gui.ui
├── doublespinbox.py??
├── ...
then you must promote the widget to do so right click on the QDoubleSpinBox and select the option "Promote to ..." by adding the following to the dialog:
Then click on the Add button and then the Promote button.
For the other QDoubleSpinBox
, right click and select the new Promote To option where the DoubleSpinBox
option is.
You can find an example here
這篇關于如何在右側 cilck 上的 Qdoublespinbox 上向 QtCore.Qt.DefaultContextMenu 添加操作?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!