Unsolved How to display the result of an operation in incremental fashion strating from 0?
-
or..
just add a
QApplication::processEvents();right after
ui->product->display(temp);
-
@LeLev please find the below code:
void MainWindow::on_controlButton_clicked()
{
double num1 = ui->num1->value();
double num2 = ui->num2->value();
double product = num1 * num2;
double currentDisplay=0;
QTimer *timer = new QTimer();
QObject::connect(timer, &QTimer::timeout,&{
if(currentDisplay>=product){
timer->stop();
}
ui->product->display(currentDisplay);
QApplication::processEvents();
if(currentDisplay<meetingCost)currentDisplay++;
});
timer->start(500);
}
} -
@Santhoshpl
It helps others if you put literal code in this forum's code markers.Meanwhile, I don't know, @Lelev writes
QObject::connect(timer, &QTimer::timeout,[&](){
while you write
QObject::connect(timer, &QTimer::timeout,&{
so do you know what you're doing there with your lambda? Is there any reason you change his code for this?
Otherwise run under debugger and look at the context/stack trace when the crash occurs.
-
@JonB said in How to display the result of an operation in incremental fashion strating from 0?:
[&]
Please take a c++ book about lambdas - you're passing by reference and it will therefore go out of scope...
-
@Gerd said in How to display the result of an operation in incremental fashion strating from 0?:
QApplication::processEvents();
This is an ugly work-around and should be avoided. It is better to program in asynchronous way instead of calling processEvents().
-
lol
then you should tell the qt people to remove such examples from the examples in qt sources... -
@Gerd said in How to display the result of an operation in incremental fashion strating from 0?:
then you should tell the qt people to remove such examples from the examples in qt sources...
you're right, it shouldn't be there, can you name/link such an example?
-
Qt\5.12.6.src\qtbase\examples\widgets\dialogs\findfiles
Qt\5.12.6.src\qtbase\examples\widgets\itemviews\pixelator
Qt\5.12.6.src\qtbase\examples\widgets\painting\fontsampler -
@Gerd Can you supply these as clickable links to the on-line docs?
-
heres one of them:
findfiles -
for these it isnt in the doc pages but in the sourcecode..
pixelator
fontsampler -
@Gerd
Well these examples haven't been touched in years, are very basic but never the less badly designed.One can redesign those examples, without the need for a processEvent calls, which will make them bigger (source code size).
But IMHO that's something that should be done. -
@LeLev
I just read your response and it seemed what I need as well.
However, I just get a crash with this code:I have been having the same problems as the OP (and everyone else, I guess)
This code snippet is just for testing messages written to a QPlainTextEdit widget, nothing else.void MainWindow::writeLeft() { int32_t cnt = 100; QString msg, cno; QTimer *ltimer = new QTimer(); do{ QTextStream(&cno) << cnt; msg = ("Hello left no : "+ cno); //message has been constructed, stop the process and display it QObject::connect(ltimer, &QTimer::timeout,[&](){ if(cnt > 0){ ltimer->stop(); } ui->memo1->appendPlainText(msg); }); cno.clear(); QThread::sleep(1); ltimer->start(10); cnt--; }while(cnt > 0); }
If I remove the QTimer statements then I just get the whole lot dumped to the QPlainTextEdit in one go.
-
@Colins2
You are re-connecting the timer every time round the loop? -
@Colins2 im not 100% sure what you need to do but see
int32_t cnt = 100; QString msg, cno; QTimer *ltimer = new QTimer(); //do{ // QTextStream(&cno) << cnt; //msg = ("Hello left no : "+ cno); QObject::connect(ltimer, &QTimer::timeout,[&](){ cnt--; if(cnt == 0){ // < ltimer->stop(); } qDebug()<< ("Hello left no : " + QString::number(cnt)); }); cno.clear(); // QThread::sleep(1); ltimer->start(10); // }while(cnt > 0);
-
@Colins2
Glancing only at @LeLev's latest post, this makes much more sense. Give it a go?! -
@J-Hilk
I totally disagree with that. What you are doing here is to replace a simple loop routine with an overcomplicated timer driven solution.
In such simple programs there is absolutly no needance for that.
Keep it simple and its runs (fast)... -
@LeLev
I don't think your solution is what I want.What I am trying to achieve is quite simple - or should be.
I have a PlainTextEdit widget that I am using as a log output.
At various times through different routines, I want to output a message.
What happens is that I get no output until the end of the routine, which isn't much use.I just cobbled the above routine to try to get just the message output working. It has no real relevance to my project other than the method.
My loop just created a message with the counter attached and I would expect it to print to the PlainTextEdit widget when called.
It doesn't do that. Stepping through the code, the statement executes and then the loop continues but nothing appears in the edit field. At the end of the loop it all gets dumped in one go.Re. the QTimer, It's new to me. I've never had to use these types of function to get output on the screen.
As Gerd says in his post, it should be such a simple thing to do but it appears not to be. -
@JonB
The idea was to trigger the timer stop so that I could print the current message and then resume.
I see what you mean though, it shouldn't be necessary to re-connect every time, just start and stop. -
@Colins2 said in How to display the result of an operation in incremental fashion strating from 0?:
At various times through different routines, I want to output a message.
If I understand you correctly this will not happen in a loop in your main thread - is that correct?
Is your current implementation with the loop just proof of concept (or an example)?
As I explained already your loop is blocking the event loop that's why it behaves like it does.
So, the question is: how are these messages generated and exposed?