在QT for Python中注册用户不活动?

2024-09-30 20:27:10 发布

您现在位置:Python中文网/ 问答频道 /正文

当主窗口中的任何空间或对象收到鼠标点击时,我试图触发一个函数。这样做的目的是在记录交互时重置计时器,以便如果计时器达到X,它将打开密码请求窗口(例如,5分钟后无交互,计时器达到300,if函数关闭/打开窗口)

QWidgets没有“按下”或“释放”信号,所以我们尝试使用MousePresseEvent函数来解决这个问题。但是,这不会发生,因为任何子小部件都有自己的MousePresseEvent事件处理程序,并且有许多子小部件最终将动态创建

我创建了一个测试程序来解释我想做什么:

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内来调用MousePresseEvent,但情况似乎并非如此(或者,至少我无法让它工作)。我也在这个问题上读过一些类似的帖子,但是大多数都是C++的(我不明白),或者对我来说没有什么意义。p>

如果按下窗口中的任何位置,我如何调用reset函数


Tags: 函数fromimportself部件defbuttonlabel
1条回答
网友
1楼 · 发布于 2024-09-30 20:27:10

我一直在阅读和阅读事件结构,主要是^ {CD1>}函数,以及许多创建它的对象的建议和例子,但是大多数例子都是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函数

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

相关问题 更多 >