首页 > 解决方案 > 在 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如果按下窗口内的任何地方,我如何调用该函数?

标签: pythonpython-3.xqwidgetpyside2

解决方案


所以我一直在阅读和阅读事件结构,主要是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函数。

虽然这可行,但我觉得它是一种廉价的解决方案,可以做得更好......


推荐阅读