【问题标题】:How to run code just after signals and events in QT QWidget initiation are processed?QT QWidget启动中的信号和事件处理后如何运行代码?
【发布时间】:2020-06-11 21:59:29
【问题描述】:
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())

【问题讨论】:

  • 我不太明白你的问题,你能不能给我解释一下。

标签: python pyqt pyqt5 qt-signals


【解决方案1】:

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

如果你看here:

    # 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-21
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多