python - 在 QT for Python 中注册用户不活动?
问题描述
当我的主窗口中的任何空间或对象收到鼠标点击时,我试图触发一个函数。这样做的目的是在记录交互时重置计时器,以便如果计时器达到 X,它将打开一个密码请求窗口(例如,5 分钟后没有交互,计时器达到 300 并且 if 函数关闭/打开窗口)
QWidgets 没有“按下”或“释放”信号,因此尝试使用 mousePressEvent 函数解决此问题。然而,这不会发生,因为任何子小部件都会有自己的 mousePressEvent 事件处理程序,并且这些子小部件中有许多最终将被动态创建。
我创建了一个测试程序来解释我想要做什么:
import sys
from PySide2.QtWidgets import *
from PySide2.QtGui import *
from PySide2.QtCore import *
class MainWindow(QWidget):
def __init__(self):
QWidget.__init__(self)
self.label = QLabel("0")
self.button = QPushButton("press")
self.label2 = QLabel("TESTING")
self.label2.setStyleSheet("background: orange;")
#self.button.released.connect(self.reset)
self.y = 0
self.layout = QVBoxLayout()
self.layout.setAlignment(Qt.AlignCenter)
self.timer = QTimer(self)
self.timer.start(1000)
self.timer.timeout.connect(self.update_label)
self.layout.addWidget(self.label)
self.layout.addWidget(self.button)
self.layout.addWidget(self.label2)
self.setLayout(self.layout)
def mousePressEvent(self, event):
#self.button.underMouse()
self.reset()
def update_label(self):
self.y += 1
print(self.y)
if self.y == 5:
print("hello!")
self.label.setText(str(self.y))
def reset(self):
self.y = 0
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
为了更好地理解我的意思,如果您单击此窗口中的任何位置,该reset
函数将被调用并self.y
重置为 0,但是如果您单击该按钮,则不会。
根据以下文档:https://doc.qt.io/qtforpython/PySide2/QtWidgets/QWidget.html?highlight=qwidget#events子小部件可以通过放置underMouse()
在mousePressEvent
' 中来调用其 mousePressEvent 但这似乎不是这个案子(或者,至少我无法让它工作)。我也读过很多关于这个问题的类似帖子,但大多数都是用 C++ 编写的(我不明白),或者对我来说不太有意义。
reset
如果按下窗口内的任何地方,我如何调用该函数?
解决方案
所以我一直在阅读和阅读事件结构,主要是eventFilter
函数,以及从中创建对象的许多建议和示例,但是大多数示例都是用 C++ 编写的。不幸的是,我无法完全理解它,所以做了这个小解决方法:
import sys
from PySide2.QtWidgets import *
from PySide2.QtGui import *
from PySide2.QtCore import *
def update():
window.reset()
class mybutton(QPushButton):
def __init__(self):
QPushButton.__init__(self)
self.setText("Press")
def clickprint(self):
print("hello2")
class MainWindow(QWidget):
def __init__(self):
QWidget.__init__(self)
self.label = QLabel("0")
self.button = mybutton()
self.label2 = QLabel("TESTING")
self.label2.setStyleSheet("background: orange;")
self.button.pressed.connect(self.button.clickprint)
self.button.pressed.connect(update)
self.x = 0
self.layout = QVBoxLayout()
self.layout.setAlignment(Qt.AlignCenter)
self.timer = QTimer(self)
self.timer.start(1000)
self.timer.timeout.connect(self.update_label)
self.layout.addWidget(self.label)
self.layout.addWidget(self.button)
self.layout.addWidget(self.label2)
self.setLayout(self.layout)
def mousePressEvent(self, event):
self.x = 0
def update_label(self):
self.x += 1
if self.x == 5:
print("hello!")
self.label.setText(str(self.x))
print(self.x)
def reset(self):
self.x = 0
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
我只是在任何对象可以调用的任何类之外放置了一个函数。函数唯一的命令是调用reset
里面的函数window
。这也意味着用于 GUI 的每个元素的每个信号也必须有第二个信号调用此update
函数。
虽然这可行,但我觉得它是一种廉价的解决方案,可以做得更好......
推荐阅读
- ansible - 有没有办法在集群中克隆服务配置?
- python - 在 Raspberry Pi 和 Python 上流式传输 YouTube 播放列表
- java - 来自带有 Jewel/Topaz512 NFC 标签的 ACR122U 阅读器的“超时,目标尚未回答”
- javascript - 工作出色后,今天 Font Awesome 停止加载。网络::ERR_CERT_COMMON_NAME_INVALID
- javascript - 如何在我的 html 网站上显示每 10 分钟更新一次的文本文件?
- jetty - 从码头中的 ResourceHandler 访问会话
- python - 如何从 64 位创建 32 位版本的 Python 代码
- html - URL 编码 CSS 参数
- javascript - 如何在工作箱或 sw 中强制缓存文件?
- javascript - 查找 a 标签后的所有 jquery 输入字段