python - PyQt Python - 为 QPushButton 创建鼠标右键单击

标签 python pyqt signals-slots right-click qpushbutton

我正在动态创建垂直布局中的 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) .然后你需要设置按钮的ContextMenuPolicyQtGui.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/

相关文章:

python - 使用自定义小部件填充 QListWidget

iphone - Android Activity/Intent vs. Qt Signal/Slot vs. iPhone ?/?

c++ - 插槽在 QT 应用程序中未接收到信号

c++ - std::function 和信号/槽系统

python - 为什么OpenCV分解的单应性平移矩阵有三个值?

python - 使用Open CV断言失败

python - 为什么我只得到第一个元素?

python - 如何在 Django 中发送具有特定 mimetype 的电子邮件?

python - 将导入的函数连接到 Qt5 进度条,无需依赖

python - pyqt中标签随时间变化