怎么在onClick事件处理程序中外面使用mouse.modifiers
时间: 2024-03-27 12:39:59 浏览: 19
如果需要在onClick事件处理程序之外使用mouse.modifiers,可以在onMousePress或onMouseRelease事件处理程序中获取它们的状态,并将它们存储在全局变量中。然后,在onClick事件处理程序中,可以访问这些全局变量以获取修饰键的状态。例如,以下示例演示如何在Python中实现这一点:
```python
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import Qt
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
self.mouseModifiers = None
self.initUI()
def initUI(self):
self.setWindowTitle('Mouse Modifiers Example')
self.setGeometry(100, 100, 300, 200)
self.show()
def mousePressEvent(self, event):
self.mouseModifiers = QApplication.keyboardModifiers()
def mouseReleaseEvent(self, event):
self.mouseModifiers = QApplication.keyboardModifiers()
def onClick(self):
if self.mouseModifiers & Qt.ShiftModifier:
print('Shift key is pressed')
if self.mouseModifiers & Qt.ControlModifier:
print('Control key is pressed')
if __name__ == '__main__':
app = QApplication([])
window = MyWindow()
app.exec_()
```
在上面的示例中,我们在mousePressEvent和mouseReleaseEvent事件处理程序中存储了修饰键的状态,然后在onClick事件处理程序中检查它们的状态。注意,在onClick事件处理程序中需要检查每个修饰键的状态时,我们使用了位运算符&。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)