我正在动态创建垂直布局中的 QPushButton 列表。我目前正在为一个功能使用“点击”信号。但我想使用鼠标右键来获得附加功能。前任。通过右键单击打印鼠标悬停的 QPushButton 的工具提示。
but = QtGui.QPushButton()
but.setText(cur);but.setCheckable(True)
but.setStyleSheet(_fromUtf8("text-align:right;background-color: rgb(50, 50, 50);"))
but.setToolTip(longName + "." + cur)
我正在查看“QMouseEvent”、“setMouseTracking”、“mousePressEvents”。但我不确定如何正确使用它们来获得我想要的结果。 我也会在“右键单击”时接受 QPushButton 的自定义信号。
最佳答案
通常,鼠标右键单击连接到上下文菜单。与
but.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
but.customContextMenuRequested.connect(handle_right_click)
您可以连接一个处理程序,它可以用于鼠标右键单击。
如果你想要一个纯粹的鼠标右键单击 Action ,你应该扩展 QPushButton 并覆盖 mousePressEvent(self, event)
和 mouseReleaseEvent(self, event)
.然后你需要设置按钮的ContextMenuPolicy到 QtGui.Qt.PreventContextMenu
以确保它们的交付:
The widget does not feature a context menu, and in contrast to NoContextMenu, the handling is not deferred to the widget's parent. This means that all right mouse button events are guaranteed to be delivered to the widget itself through QWidget::mousePressEvent(), and QWidget::mouseReleaseEvent().
关于python - PyQt Python - 为 QPushButton 创建鼠标右键单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30627923/