简体   繁体   English

如何在功能结束之前让Items显示在QListWidget中?

[英]How can I get Items to show in my QListWidget before the end of the function?

I want my QListWidget to update with the new item as it is added, but it only updates with all of the items once the function has ended. 我希望我的QListWidget在添加新项目时进行更新,但仅在函数结束后才对所有项目进行更新。 I have tried using update() and repaint() , but neither work. 我尝试使用update()repaint() ,但是都没有用。 I actually had to use repaint() on the Widget itself just to get it to show up before the end, but none of the items do. 我实际上不得不在Widget本身上使用repaint()只是为了使其在结束之前显示,但是没有任何一项起作用。 Here is a brief view of the first item to add: 这是要添加的第一项的简要视图:

def runPW10(self):
    self.PWList.setVisible(True)
    self.PWList.setEnabled(True)

    # This repaint() has to be here for even the List to show up
    self.PWList.repaint()

    ....

    self.PWList.addItem('Executing Change Password Tool')

    # This does not help
    self.PWList.repaint()

    ....

There is more to the function, but it is long and this should include what it needed. 该功能还有更多,但很长,应该包括所需的功能。 Please let me know if more is required. 请让我知道是否需要更多。

What am I doing wrong that makes this List not update as the item is added? 我做错了什么,导致该列表在添加项目时不会更新?

Add QApplication.processEvents() . 添加QApplication.processEvents()

QCoreApplication.processEvents (QEventLoop.ProcessEventsFlags flags = QEventLoop.AllEvents)

Processes all pending events for the calling thread according to the specified flags until there are no more events to process. 根据指定的标志处理调用线程的所有未决事件,直到没有更多事件可处理为止。

You can call this function occasionally when your program is busy performing a long operation (eg copying a file). 当程序忙于执行长时间的操作(例如,复制文件)时,可以偶尔调用此函数。

Your widget originally will be shown but unresponsive. 您的窗口小部件最初将显示,但没有响应。 To make the application responsive, add processEvents() calls to some whenever you add an item. 为了使应用程序具有响应能力,每当添加项目时,都向其中一些添加processEvents()调用。

Do keep in mind that this can affect performance a lot . 请记住,这可能会影响性能提升不少 This lets the whole application loop execute including any queued events. 这使整个应用程序循环得以执行,包括所有排队的事件。 Don't add this to performance sensitive loops. 不要将此添加到对性能敏感的循环中。

Also consider that this allows your user to interact with the application, so make sure that any interactions that can happen either are not allowed, such as somebutton.enabled(False) , or are handled gracefully, like a Cancel button to stop a long task. 还应考虑到这允许您的用户与应用程序进行交互,因此请确保不允许发生任何可能发生的交互somebutton.enabled(False)例如somebutton.enabled(False)或被优雅地处理(如“ Cancel按钮)以停止长时间的任务。

See the original C++ docs for further information, since pyqt is a direct port. 有关更多信息,请参见原始C ++文档 ,因为pyqt是直接端口。

To complete Drise's answer on this point: 要在这一点上完成Drise的答案:

Also consider that this allows your user to interact with the application, so make sure that any interactions that can happen either are not allowed, such as somebutton.enabled(False) , or are handled gracefully, like a Cancel button to stop a long task. 还应考虑到这允许您的用户与应用程序进行交互,因此请确保不允许发生任何可能发生的交互somebutton.enabled(False)例如somebutton.enabled(False)或被优雅地处理(如“ Cancel按钮)以停止长时间的任务。

You may want to use the QEventLoop.ExcludeUserInputEvents flag this way: QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents) to refresh the GUI while preventing the user to activate any widgets. 您可能希望通过以下方式使用QEventLoop.ExcludeUserInputEvents标志: QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)刷新GUI,同时阻止用户激活任何窗口小部件。

QEventLoop.ExcludeUserInputEvents

0x01

Do not process user input events, such as ButtonPress and KeyPress . 不要处理用户输入事件,例如ButtonPressKeyPress Note that the events are not discarded; 请注意,事件不会被丢弃; they will be delivered the next time processEvents() is called without the ExcludeUserInputEvents flag. 它们将在下次调用processEvents()且没有ExcludeUserInputEvents标志的情况下ExcludeUserInputEvents

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM