How to append strings in QText browser by using any loop
-
Example :
QStirng ret = "Hello"
for (int i = 0;i<=10;i++)
{
ui->text_browser->append("ret");
//here delay 2sec
}here after 20sec all 10 prints are displaying, but every 2 sec text browser wasn't append those print.
I am using QT creater 4.5.0 (community) -
You're blocking the Qt Eventloop. Don't block it and use e.g. a QTimer instead.
-
How will you stop the timer based on condition?
-
@Trojan-Arun said in How to append strings in QText browser by using any loop:
How will you stop the timer based on condition?
By e.g. using a member variable.
-
Sorry Ehrlicher i didnt understand.. here i written my sample program.. can u please explain in that.
Ex:
QString ret = "hioooo";
QTimer *timer =new QTimer(this);ui->textBrowser->setText(ret);
connect(timer, SIGNAL(timeout()),this, SLOT(funct()));
for (int i= 0; i<10;i++)
{//funct(); ui->textBrowser->setText("inmsde for"); if (i == 10) { //disconnect(timer); timer->stop(); ui->textBrowser->setText("Inside if"); } else { ui->textBrowser->setText("Inside else"); timer->start(1000); ui->textBrowser->setText("after else"); }
}
void funct()
{
ui->textBrowser->setText("hello world");
} -
Inside funct() you have to append your new text to your textbrowser widget. Currently you simply set "hello world" there.
-
Sorry Ehrlicher i didnt understand.. here i written my sample program.. can u please explain in that.
Ex:
QString ret = "hioooo";
QTimer *timer =new QTimer(this);ui->textBrowser->setText(ret);
connect(timer, SIGNAL(timeout()),this, SLOT(funct()));
for (int i= 0; i<10;i++)
{//funct(); ui->textBrowser->setText("inmsde for"); if (i == 10) { //disconnect(timer); timer->stop(); ui->textBrowser->setText("Inside if"); } else { ui->textBrowser->setText("Inside else"); timer->start(1000); ui->textBrowser->setText("after else"); }
}
void funct()
{
ui->textBrowser->setText("hello world");
}//widget.h #ifndef WIDGET_H #define WIDGET_H #include <QTimer> #include <QWidget> namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = nullptr); ~Widget(); private slots: void onTimeout(); void fill(); private: int m_counter = 0; QTimer m_timer; Ui::Widget *ui; }; #endif // WIDGET_H
//widget.cpp #include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); ui->textBrowser->setText("Hello"); connect(&m_timer, &QTimer::timeout, this, &Widget::onTimeout); connect(ui->buttonFill, &QPushButton::clicked, this, &Widget::fill); } Widget::~Widget() { delete ui; } void Widget::fill() { //Avoid restarting timer before it have finished previous sequence //We could also disable button during the process if(m_timer.isActive()) return; m_timer.start(1000); } void Widget::onTimeout() { if(m_counter < 10){ m_counter++; ui->textBrowser->append("new line"); } else { m_timer.stop(); m_counter = 0; } }