PySide2 Non-Blocking QMessageBox不显示文本

时间:2019-03-05 21:51:34

标签: python pyside2

尽管我的应用程序做了一些耗时的事情,但我想在应用程序繁忙时向用户显示一个消息框。我不希望有任何按钮(例如OKCancel),并且我不能在消息框上调用exec_(),因为那样阻塞了。

我检查了多个qt网站,而我所需的代码似乎可以归结为:

    message_box = QMessageBox()
    message_box.setText(str('Reading Device, Please Wait...'))
    message_box.show()
    # do work here
    message_box.close()

运行代码时,出现消息框,但没有文本。我在做什么错了?

我在下面提供了一个工作示例:

#!/usr/bin/env python3

import sys
import time
from PySide2.QtWidgets import (QLineEdit, QPushButton, QApplication,
    QVBoxLayout, QDialog, QMessageBox)

class Form(QDialog):

    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        self.button = QPushButton("Click Me")
        layout = QVBoxLayout()
        layout.addWidget(self.button)
        self.setLayout(layout)
        # Add button signal to dowork slot
        self.button.clicked.connect(self.dowork)

    def dowork(self):
        message_box = QMessageBox()
        message_box.setText(str('Reading Device, Please Wait...'))
        message_box.show()
        delay = 2.5
        while delay:
           sys.stdout.write('Working...\n')
           time.sleep(0.5)  # do some time-consuming stuff...
           delay -= 0.5
        message_box.close()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    form = Form()
    print('starting app...')
    form.show()
    sys.exit(app.exec_())

如果单击按钮,则会弹出消息框,并在完成“工作”时显示该消息框。 “工作”完成后,消息框将再次消失-应当消失。但是消息框中没有显示任何文本。

这里有一个类似的问题:qmessagebox-not-show-text-when-call-show,但这不能回答我的问题。

2 个答案:

答案 0 :(得分:1)

您不能有一个消耗大量时间(超过30毫秒)的任务,因为它会阻塞GUI事件循环,从而阻止Qt正常执行其任务,而是使用信号旁边的线程从控制台更新GUI。其他线程:

import sys
import threading
import time
from PySide2 import QtCore, QtWidgets

class Form(QtWidgets.QDialog):
    started = QtCore.Signal()
    finished = QtCore.Signal()

    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        self.button = QtWidgets.QPushButton("Click Me")
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.button)
        # Add button signal to dowork slot
        self.button.clicked.connect(self.on_clicled)

        self._message_box = QtWidgets.QMessageBox()
        self._message_box.setText(str('Reading Device, Please Wait...'))
        self._message_box.setStandardButtons(QtWidgets.QMessageBox.NoButton)
        self.started.connect(self._message_box.show)
        self.finished.connect(self._message_box.accept)

    @QtCore.Slot()
    def on_clicled(self):
        thread = threading.Thread(target=self.dowork, daemon=True)
        thread.start()

    def dowork(self):
        delay = 2.5
        self.started.emit()
        while delay:
           sys.stdout.write('Working...\n')
           time.sleep(0.5)  # do some time-consuming stuff...
           delay -= 0.5
        self.finished.emit()

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    form = Form()
    print('starting app...')
    form.show()
    sys.exit(app.exec_())

答案 1 :(得分:0)

几乎可以肯定,您应该使用QProgressDialog。将最小值和最大值设置为零,以获得“不确定的”外观。

https://doc.qt.io/qt-5/qprogressdialog.html

尽管在您的try <- analyze.wavelet(transform(WaveletSite1Oxygen2, date=Date), my.series="AverageOxygen", loess.span = 1/25, dt = 1, dj = 1/250, lowerPeriod = 2, upperPeriod = 16, make.pval = TRUE, method = "white.noise", params = NULL, n.sim = 10, verbose = TRUE) #code for analyzing function wt.image(try, periodlab = "periods", + legend.params = list(lab = "wavelet power levels"), + label.time.axis = TRUE, + show.date = TRUE, date.format = "%Y-%m") # code for graph 之后强制重绘可能会产生奇迹:

setText()