相关文章推荐
玩手机的楼梯  ·  [IntelliJ IDEA + ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a QTextEdit widget in a private slot, which I update regularly with setText() and insertPlainText().

I have found that setText()/insertPlainText() does not update the QTextEdit widget immediately. Instead, the QTextWidget is updated when the slot function returns. To test this, I have put a sleep() just after the setText()/insertPlainText().

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;

This is a problem for me because I need to print a message in my QTextEdit widget BEFORE launching a time-consuming process (using QProcess). Currently, this message is not being printed until after QProcess process has returned.

Does anyone know how I can get the QTextEdit widget to show its contents right after setText()/insertPlainText()?

Using Qt5 on Fedora 29.

Never execute a task that consumes a lot of time in the GUI thread. In general, the solution is to execute that task in another thread, but in your case it indicates that you use QProcess, so I assume that you are using one of the methods waitForFinished(), waitForStarted() or waitForReadyRead(), instead you should use the signals:

#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"
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.