【问题标题】:QTextEdit refresh after setText()/insertPlainText()QTextEdit 在 setText()/insertPlainText() 之后刷新
【发布时间】:2019-06-14 23:53:29
【问题描述】:

我在私人插槽中有一个 QTextEdit 小部件,我会定期使用 setText() 和 insertPlainText() 对其进行更新。

我发现 setText()/insertPlainText() 不会立即更新 QTextEdit 小部件。相反,当 slot 函数返回时,QTextWidget 会更新。为了测试这一点,我在 setText()/insertPlainText() 之后放置了一个 sleep()。

class MyWindow : public Widget
{
    MyWindow()
    {
        my_button = new QPushButton(this);
        my_edit   = new QTextEdit(this);

        connect(my_button, 
                &QPushButton::clicked, 
                this, 
                &MyWindow::my_callback);
    }

    private slots:

        void my_callback()
        {
            my_edit->setText("sample text");

            // nothing happens; the QTextEdit 
            // widget does not show "sample text"

            sleep(10); 

            // the QTextEdit widget will show
            // "sample text" AFTER the sleep,
            // when my_callback returns.
         }

    private:
        QPushButton* my_button;
        QTextEdit*   my_edit;
}

这对我来说是个问题,因为我需要在启动一个耗时的过程(使用 QProcess)之前在我的 QTextEdit 小部件中打印一条消息。目前,直到 QProcess 进程返回后才会打印此消息。

有谁知道如何让 QTextEdit 小部件在 setText()/insertPlainText() 之后立即显示其内容?

在 Fedora 29 上使用 Qt5。

【问题讨论】:

    标签: qt qt5 qtextedit


    【解决方案1】:

    永远不要在 GUI 线程中执行耗费大量时间的任务。通常,解决方案是在另一个线程中执行该任务,但在您的情况下,它表明您使用 QProcess,所以我假设您使用的是方法 waitForFinished()、waitForStarted() 或 waitForReadyRead() 之一,而不是您应该使用信号:

    #include <QtWidgets>
    
    class Widget: public QWidget{
        Q_OBJECT
    public:
        Widget(QWidget *parent=nullptr):
            QWidget(parent)
        {
            button.setText("Press me");
            QVBoxLayout *lay = new QVBoxLayout{this};
            lay->addWidget(&button);
            lay->addWidget(&textedit);
            connect(&button, &QPushButton::clicked, this, &Widget::onClicked);
            connect(&process, &QProcess::readyReadStandardError, this, &Widget::onReadyReadStandardError);
            connect(&process, &QProcess::readyReadStandardOutput, this, &Widget::onReadAllStandardOutput);
        }
    private Q_SLOTS:
        void onClicked(){
            textedit.setText("sample text");
            process.start("ping 8.8.8.8");
        }
        void onReadyReadStandardError(){
            textedit.append(process.readAllStandardError());
        }
        void onReadAllStandardOutput(){
            textedit.append(process.readAllStandardOutput());
        }
    private:
        QPushButton button;
        QTextEdit textedit;
        QProcess process;
    };
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Widget w;
        w.show();
        return a.exec();
    }
    #include "main.moc"
    

    【讨论】:

      【解决方案2】:

      不知道有没有打电话

      QCoreApplication::processEvents() 
      

      在 ->setText("sample text") 之后就可以解决您的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-04
        • 1970-01-01
        • 2014-07-20
        相关资源
        最近更新 更多