PyQt GUI 操作顺序 [英] PyQt GUI order of operations

查看:41
本文介绍了PyQt GUI 操作顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题:

在 PyQt 制作的 GUI 中按下按钮后,我必须执行两个操作:

upon pressing a button in PyQt made GUI I have to do two actions:

  1. 立即更新 QTextBrowser
  2. 运行一个方法,该方法将等待一段时间并在之后启用一些按钮.

我得到的是 1 和 2 在等待期后同时完成.

What I get is that 1 and 2 are done at the same time, after a waiting period.

部分代码是:

    #in the signals definition...
    signalUpdateProgressDialog = QtCore.pyqtSignal(str) # signal definition

    #in the connections definition...
    self.btnStopOpt.clicked.connect(self.clickStop1)
    self.btnStopOpt.clicked.connect(self.clickStop)

def updateProgressDialog(self, dialog):
    self.ProgressDialog.setHtml(dialog)

def clickStop1(self):
    # notify
    self.signalUpdateProgressDialog.emit('Message')

def clickStop(self):

    # shut down thread...

    print "Thread Stopped"

    time.sleep(5)
    # enable run button
    self.btnRun.setEnabled(True)

我在一个 clickStop 方法中尝试了所有方法,我尝试了有和没有为 updateProgress 发出信号.始终,只有在等待期过后才会刷新 GUI.

I tried all in one clickStop method, I tried with and without emiting signal for updateProgress. Always, GUI is refreshed only after the waiting period.

尽管如此,我之前遇到过这个问题,我想我不明白它是如何与 GUI 一起工作的.通常,如何获得所需的行为:执行代码行时更新 GUI?

Nevertheless, I encountered this problem before, I think I do not understand how it works with the GUI. In general, how to get the required behaviour: GUI is updated when the code line is executed?

推荐答案

在控制权返回到 Qt 事件循环之前,不会更新/绘制 GUI.事件循环在主线程中运行,用于处理与 GUI 的交互并协调信号/槽系统.

The GUI is not updated/drawn until control is returned to the Qt event loop. The event loop runs in the main thread, and is what handles interactions with the GUI and coordinates the signals/slot system.

当您在像 clickStop1() 这样的插槽中调用 Qt 函数时,Qt 调用会运行,但不会立即重绘 GUI.在这种情况下,控制不会返回到事件循环,直到 clickStop() 完成运行(也就是处理 clicked 信号的所有插槽.

When you call a Qt function in a slot like clickStop1(), the Qt call runs, but the GUI is not redrawn immediately. In this case, control doesn't return to the event loop until clickStop() has finished running (aka all the slots for the clicked signal are processed.

您的代码的主要问题是您在主线程中有一个 time.sleep(5),它阻止了用户的 GUI 交互以及重绘.您应该保持槽的执行时间较短以保持响应式 GUI.

The main problem with your code is that you have a time.sleep(5) in the main thread, which is blocking GUI interaction for the user as well as the redrawing. You should keep the execution time of slots short to maintain a responsive GUI.

因此我建议您修改 clicked() 以便它触发 在您指定的超时后单发 QTimer.QTimers 不会阻塞主线程,因此将保持响应性.但是,请注意用户可能同时与 GUI 交互!在您等待 QTimer 执行时,确保它们不会通过一些用户交互来破坏您的程序状态.

I would thus suggest you modify clicked() so that it fires a singleshot QTimer after your specified timeout. QTimers do not block the main thread, and so responsiveness will be maintained. However, be aware that the user may interact with the GUI in the mean time! Make sure they can't compromise the state of your program with some user interaction while you wait for the QTimer to execute.

这篇关于PyQt GUI 操作顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆