如何在函数结束之前获取要在QListWidget中显示的项?

2024-06-28 11:18:06 发布

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

我希望我的QListWidget在添加新项时使用新项进行更新,但它只在函数结束后使用所有项进行更新。我试过使用update()repaint(),但都不起作用。实际上,我不得不在小部件本身上使用repaint(),只是为了让它在结束之前显示出来,但是没有一个项目可以。以下是要添加的第一项的简要视图:

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()

    ....

函数还有很多,但是很长,应该包括它所需要的。如果需要更多,请告诉我。你知道吗

我做错了什么,使得此列表不会随着项目的添加而更新?你知道吗


Tags: to项目函数self视图true部件def
2条回答

要完成德里斯对这一点的回答:

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.

您可能希望使用^{}标记:QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)来刷新GUI,同时防止用户激活任何小部件。你知道吗

QEventLoop.ExcludeUserInputEvents

0x01

Do not process user input events, such as ButtonPress and KeyPress. Note that the events are not discarded; they will be delivered the next time processEvents() is called without the ExcludeUserInputEvents flag.

添加^{}。你知道吗

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 (e.g. copying a file).

您的小部件最初将显示,但没有响应。要使应用程序具有响应性,请在添加项时向某些应用程序添加processEvents()调用。你知道吗

请记住,这会对性能产生很大影响。这允许整个应用程序循环执行,包括任何排队事件。不要将此添加到性能敏感循环中。你知道吗

还要考虑到这允许您的用户与应用程序进行交互,因此请确保可能发生的任何交互要么是不允许的,比如somebutton.enabled(False),要么是优雅地处理的,比如用Cancel按钮来停止长任务。你知道吗

有关更多信息,请参见original C++ docs,因为pyqt是一个直接端口。你知道吗

相关问题 更多 >