首页 > 解决方案 > QT QWidget启动中的信号和事件处理后如何运行代码?

问题描述

import sys
from PyQt5.Qt import *

app = QApplication([])


class MyDialog(QDialog):
    def __init__(self):
        super().__init__()
        layout = QVBoxLayout(self)
        self.list_widget = QListWidget()
        layout.addWidget(self.list_widget)
        self.list_widget.currentRowChanged.connect(self.on_cur_row_changed)
        self.list_widget.addItems(['apple', 'orange'])

        # code block A # start #
        self.clear_qt_currentRow()
        # code block A # end   #

        print('End of MyDialog.__init__')

    def on_cur_row_changed(self, row):
        print('Current row changed to: ', row)

    def clear_qt_currentRow(self):
        print('Clear Qt currentRow')
        self.list_widget.selectionModel().clear()


dialog = MyDialog()
dialog.show()
sys.exit(app.exec())

在上面的代码中,我不喜欢 QT 将列表小部件的 currentRow 属性设置为 0(第一行)。我想在 clear_qt_currentRow() 中运行代码,在处理完 QT QWidget 启动中的信号和事件之后(在这种情况下,将 currentRow 属性设置为 None)但是代码块 A 中的 self.clear_qt_currentRow() 运行得太早(之前QT 改变 currentRow)。结果显示为:

Clear Qt currentRow
End of MyDialog.__init__
Current row changed to:  0

然后,我尝试了以下两种代码块 A 的替代方案,但仍然没有让 self.clear_qt_currentRow() 运行得足够晚。

# alternative 1
app.processEvents()
self.clear_qt_currentRow()

# result:
# Clear Qt currentRow
# End of MyDialog.__init__
# Current row changed to:  0
# alternative 2
QTimer.singleShot(0, self.clear_qt_currentRow)

# result:
# End of MyDialog.__init__
# Clear Qt currentRow
# Current row changed to:  0

我知道 QTimer.singleShot(10000, self.clear_qt_currentRow) 可以做到。但这不可能是“真正的”解决方案,因为用户可能拥有不同规格的计算机。

对我有什么建议吗?

编辑:在粗体文本中添加我想要做的事情。

编辑:在@zariiii9003 的帮助下,我更改为下面的代码,它在类内完成工作。

import sys
from PyQt5.Qt import *

app = QApplication([])


class MyDialog(QDialog):
    def __init__(self):
        super().__init__()
        layout = QVBoxLayout(self)
        self.list_widget = QListWidget()
        layout.addWidget(self.list_widget)
        self.list_widget.currentRowChanged.connect(self.on_cur_row_changed)
        self.list_widget.addItems(['apple', 'orange'])
        # code block A # start #
        QTimer.singleShot(0, self.clear_qt_currentRow)
        # code block A # end   #
        print('End of MyDialog.__init__')

    def on_cur_row_changed(self, row):
        print('Current row changed to: ', row)

    def clear_qt_currentRow(self):
        print('Process events')
        # add here
        app.processEvents()
        print('Clear Qt currentRow')
        self.list_widget.selectionModel().clear()


dialog = MyDialog()
dialog.show()
sys.exit(app.exec())

标签: pythonpyqtpyqt5qt-signals

解决方案


你可以看看 Spyder,一个用 Qt 实现的 IDE:

如果你看这里

    # Main window
    main = MainWindow(options)

    main.show()
    main.post_visible_setup()

他们首先初始化 MainWindow,然后调用show(),然后执行post_visible_setup().

对于您的情况,这是:

dialog = MyDialog()
dialog.show()
app.processEvents()
dialog.clear_qt_currentRow()
sys.exit(app.exec())

结果是

End of MyDialog.__init__
Current row changed to:  0
Clear Qt currentRow
Current row changed to:  -1

推荐阅读