Hi, I am new for Qt, i am trying to display 0 to 999 in text Browser but when i am executing following code it is directly showing 999 output can anyone help me, thank you in advance
-
@Rameshwar
First, my friend, kindly think about the title of any thread you start here. It should be a couple of words summary, e.g.Problem displaying text
, rather than a whole sentence.https://doc.qt.io/qt-5/qtextedit.html#setText (inherited by your
QTextBrowser
) sets the complete text, overwriting whatever was there. So your code runs through and by the end all your see is the final number,999
. Also, because you do not allow the Qt "event loop" to be called in your loop, you wouldn't actually see the earlier numbers displayed anyway, you only see the final 999 because only after your loop does Qt get to actually draw it on the screen.Either you mean to put some kind of delay in between each number and allow Qt to re-paint the widget (which you would probably do with timers, signals & slots, which might be a bit advanced for what you have in mind), or perhaps you'd just like to append the next number each time:
ui->textBrowser->setText(ui->textBrowser->text() + ", " + QString::number(j));
or (slightly different):
ui->textBrowser->append(QString::number(j));
You will still only get to see the final state, but at least all the numbers will be there.
-
@JonB
But I want to display 0 and then 1 and then 2.....999 on window. you solve my problem almost but it only display after execution of totaly for loop. but i want display "1" then add 1 and display "2" and then add 1 and display "3" like this. -
@Rameshwar like @JonB said, that is possible, but will look complicated
#include <QApplication> #include <QTextBrowser> #include <QTimer> int main(int argc, char *argv[]) { QApplication a(argc, argv); QTextBrowser *browser = new QTextBrowser(); browser->setPlainText("0"); browser->show(); QTimer *t = new QTimer(); int count = 0; QObject::connect(t, &QTimer::timeout, browser, [&count, t, browser]()->void{ if(++count < 100){ browser->append(QString::number(count)); } else { t->stop(); } }); t->start(1000); return a.exec(); }
-
Hi,
How is it not useful ? The QTimer approach is the right one if you want to display a progressive counter in a way that your application user can see it.
Using a loop like you do just blocks the event loop. That's why you only see the widget content after the loop ended.
-
@Rameshwar If you need this code inside mainwindow then put it there, it isn't that hard.
-
@jsulm
but if you write code inside mainwindow then , it does not display step by step directly it displays last value that is 999 or using append it displays 0,1,2.....999 once time but i want it displays step by step first it display "1" then increment (i++) then it display "2" then and again it (i++) increment by one and display 3....999. -
@Rameshwar Then you're doing it wrong. Please show your code.
-
@Rameshwar This code is NOT what @J-Hilk suggested...
-
@Rameshwar This is exactly what was already suggested (using QTimer). Did you try?!
-
@Rameshwar said in Hi, I am new for Qt, i am trying to display 0 to 999 in text Browser but when i am executing following code it is directly showing 999 output can anyone help me, thank you in advance:
i want to display contineousaly some data after one second on window.ui
everyone here understood your requirement! Solutions are suggested ! why don't you try instead of repeating the same thing over &over ?
-
-
@J-Hilk has given you the necessary code to start from. That's why people type in answers to questions. First try that and verify it works.
-
Then change it so that it writes on your mainwindow or whatever you want instead.
-
Only after that, write back if you can't get what you want working.
-