QTextEdit 在 setText()/insertPlainText() 之后刷新
QTextEdit refresh after setText()/insertPlainText()
我在专用插槽中有一个 QTextEdit 小部件,我会定期使用 setText() 和 insertPlainText() 对其进行更新。
我发现 setText()/insertPlainText() 不会立即更新 QTextEdit 小部件。相反,QTextWidget 在插槽函数 returns 时更新。为了测试这一点,我在 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。
切勿在 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"
不知道打电话给
QCoreApplication::processEvents()
在 ->setText("sample text") 之后就可以解决您的问题。
我在专用插槽中有一个 QTextEdit 小部件,我会定期使用 setText() 和 insertPlainText() 对其进行更新。
我发现 setText()/insertPlainText() 不会立即更新 QTextEdit 小部件。相反,QTextWidget 在插槽函数 returns 时更新。为了测试这一点,我在 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。
切勿在 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"
不知道打电话给
QCoreApplication::processEvents()
在 ->setText("sample text") 之后就可以解决您的问题。