window右键出不来相关菜单

在右键中出来Notepad++
https://www.cnblogs.com/ray-mr-huang/p/10446638.html

git bash windows右键问题
https://jingyan.baidu.com/article/86fae34618ab313c49121a34.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过继承QTableWidget类并重写mousePressEvent方法来实现右键菜单的弹出。 首先,创建一个新类并继承QTableWidget: ```python class MyTableWidget(QTableWidget): def __init__(self, parent=None): super().__init__(parent) ``` 然后,重写mousePressEvent方法: ```python class MyTableWidget(QTableWidget): def __init__(self, parent=None): super().__init__(parent) def mousePressEvent(self, event): if event.button() == Qt.RightButton: pos = event.pos() if self.horizontalHeader().rect().contains(pos): menu = QMenu(self) menu.addAction("Action 1") menu.addAction("Action 2") menu.exec_(self.horizontalHeader().mapToGlobal(pos)) ``` 在mousePressEvent方法中,我们首先检查鼠标按下的是右键,然后检查鼠标的位置是否在水平表头内。如果是,我们创建一个QMenu并添加一些动作,然后在鼠标位置处显示菜单。 最后,我们需要将表格部件设置为我们新创建的表格类: ```python table = MyTableWidget() ``` 完整的示例代码: ```python from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidget, QMenu from PyQt5.QtCore import Qt class MyTableWidget(QTableWidget): def __init__(self, parent=None): super().__init__(parent) def mousePressEvent(self, event): if event.button() == Qt.RightButton: pos = event.pos() if self.horizontalHeader().rect().contains(pos): menu = QMenu(self) menu.addAction("Action 1") menu.addAction("Action 2") menu.exec_(self.horizontalHeader().mapToGlobal(pos)) class MainWindow(QMainWindow): def __init__(self): super().__init__() table = MyTableWidget() table.setRowCount(4) table.setColumnCount(3) self.setCentralWidget(table) if __name__ == '__main__': app = QApplication([]) window = MainWindow() window.show() app.exec_() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值