Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to append strings in QText browser by using any loop
QtWS25 Last Chance

How to append strings in QText browser by using any loop

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Trojan Arun
    wrote on last edited by
    #1

    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)

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You're blocking the Qt Eventloop. Don't block it and use e.g. a QTimer instead.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      7
      • T Offline
        T Offline
        Trojan Arun
        wrote on last edited by
        #3

        How will you stop the timer based on condition?

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • T Offline
            T Offline
            Trojan Arun
            wrote on last edited by
            #5

            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");
            }

            Gojir4G 1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Inside funct() you have to append your new text to your textbrowser widget. Currently you simply set "hello world" there.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • T Trojan Arun

                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");
                }

                Gojir4G Offline
                Gojir4G Offline
                Gojir4
                wrote on last edited by
                #7

                @Trojan-Arun

                //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;
                    }
                }
                
                
                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved