在while(1)循环中将文本插入到QT中的texteditor [英] Insert text in while(1) loop to texteditor in QT

查看:237
本文介绍了在while(1)循环中将文本插入到QT中的texteditor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图连续"n"次将某些文本"打印到QTextBrowser.其中"n"是整数.为此,我使用了QTimer :: SingleShot进行计时.一旦触发了超时,则将FLAG设置为false,并且在FLAG为false时,在while循环中监视此"FLAG"以中断,并应插入文本,直到FLAG设置为FALSE. FLAG的初始值为true.

Am trying to print "Some text" to QTextBrowser, continuously for "n" time. Where "n" is integer. For this I have used QTimer::SingleShot for timing. Once the timeout is triggered a FLAG is set to false and this "FLAG" is monitored in while loop to break when FLAG is false and it shall insert the text till FLAG is set to FALSE. Initial value for FLAG is true.

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QThread>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    FLAG = true;

}

void MainWindow::on_pushButton_clicked()
{
    ui->pushButton->setEnabled(false);
   RunTheTimer();
    int counter = 0;

    do
    {
        ui->textBrowser->insertPlainText(QString("Inside While loop %1 \n").arg(counter++));
        counter++;
    }while(FLAG);

    FLAG = true;

}
void MainWindow::RunTheTimer()
{
     ui->textBrowser-> insertPlainText("Timer Started");
    QTimer::singleShot(60000, this, SLOT(Update()));// One Minute

}

void MainWindow::Update()
{
   ui->textBrowser-> insertPlainText("Timeout signal triggered");
    ui->pushButton->setEnabled(true);
   FLAG = false;
}
MainWindow::~MainWindow()
{
    delete ui;
}

当我单击按钮"时,应用程序正在挂起. 经过调试后,我观察到,一旦将执行输入到while(1)循环中,并且应用程序无法在while(1)循环中插入任何文本,超时就不会触发.为什么会这样呢?我在做什么错了?

Application is getting Hang, When I click Pushbutton. After debugging I observed, timeout is not triggering once the execution is entered to while(1) loop and application is not able to insert any text inside while(1) loop. Why this behavior? What am I doing wrong?

谢谢.

推荐答案

您没有将控制权返回给事件循环,Qt中的许多内容都没有在没有事件循环的情况下无法正常工作,请查看

You are not returning control to the event loop, many things in Qt are not designed to work without an event loop, Have a look at this page from the Qt wiki, in your case:

  • QTextBrowser将无法显示新添加的文本,因为这要求小部件能够接收绘画事件(如果没有事件循环,这是不可能的.)
  • 将标志设置为false的计时器将无法触发,因为您的程序始终忙于执行while循环,并且它将无法执行其他任何操作(除非它从那退出) while循环,如果它没有将标志设置为false,这是不可能的.
  • QTextBrowser won't be able to show the newly added text, since this requires the widget to be able to receive paint events (and this is impossible without an event loop).
  • The timer that sets your flag to false won't be able to fire, since your program is always busy executing your while loop, and it won't be able to do anything else (unless it gets out from that while loop and this is impossible if it does not set your flag to false. . .).

如果您想尽可能重复地执行某些操作,则可以使用QTimer并设置其

Instead of using an endless loop, if you want to execute something as repeatedly as possible, you can use a QTimer and set its interval property to 0, this is a special value that causes the timer to timeout as soon as the event loop finishes processing all events in the event queue.

使用上述方法而不是无休止的循环,您可以使用另一个计时器在特定的时间后停止上述计时器,并且您不必担心事件未到达以及自事件循环以来计时器不会触发一直在执行.

Using the above approach instead of your endless loop, you can use another timer to stop the above timer after a specific amount of time, and you don't have to worry about events not arriving and timers not firing since the event loop is always executing now.

以下是上述方法的一种可能的实现方式:

Here is a possible implementation of the above approach:

#include <QtWidgets>

int main(int argc, char* argv[]){
    QApplication a(argc, argv);

    //set up GUI
    QWidget widget;
    QVBoxLayout layout(&widget);
    QTextBrowser textBrowser;
    QPushButton button("Add Text");
    layout.addWidget(&textBrowser);
    layout.addWidget(&button);

    //timer with 0 interval instead of while loop
    QTimer workTimer;
    workTimer.setInterval(0);
    int counter=0;
    QObject::connect(&workTimer, &QTimer::timeout, [&]{
        //add text to textBrowser whenever the workTimer fires
        textBrowser.append(QStringLiteral("Additional Text %1").arg(counter++));
    });
    //when the button is clicked
    QObject::connect(&button, &QPushButton::clicked, [&]{
        //start work timer
        workTimer.start();
        button.setEnabled(false);
        //stop work timer after 5 seconds
        QTimer::singleShot(5000, [&]{
            workTimer.stop();
            button.setEnabled(true);
        });
    });
    widget.show();

    return a.exec();
}

这篇关于在while(1)循环中将文本插入到QT中的texteditor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆