1

I'm building some code where I'm running a while loop and, within the loop, am trying to change the contents of a couple of textboxes with QLineEdit's setText(). However, merely calling setText within the loop does not work; the textboxes only change their actual value once the code has run through, instead of at each iteration.

I have little experience with C++ or Qt, but the project I'm working on must use them. Any help?

EDIT: I'm guessing this must be something simple that I simply am having troubles because of my lack of familiarity/knowledge, but if more information is needed I'll gladly provide it!

  • Disclaimer: I am no Qt user. Usually graphic toolkits have calls to force redrawing or (that's the same) to invalidate a window/widget. They usually must be called explicitely if you're in the code of a widget itself, but may need be called also from the application (especially if you're doing some hack...) – Epikuros Jan 18 '13 at 18:33
  • I tried something that I think is what you're describing, and it didn't work. As Joey has said in his answer, I guess QT just works differently, or something (I'm not knowledgeable enough even to analyse what is wrong or not, unfortunately! :( ). – Julio Nicolini Jan 18 '13 at 19:40

2 Answers2

4

The problem is that QT needs control to return to the UI thread's event loop in order to update the QLineEdit's visual appearance. The quick and dirty way to run the event loop is to add QCoreApplication::processEvents() after each call to setText(). The proper way to fix it is to move the blocking process that sets the value of the text box into another thread, expose an updateText(QString text) signal, connect it to the TextBox's setText(const QString & text) slot and emit that signal whenever you want the text to be updated.

Please see my answer to a similar question for more detail: unexplained delay after QProgressBar finish loading

You might also want to check out some of the documentation on QThreads and the Qt signal slot system: http://harmattan-dev.nokia.com/docs/library/html/qt4/threads-qobject.html

Community
  • 1
  • 1
Joey
  • 475
  • 1
  • 4
  • 11
  • I see, I see... Yeah, I had read about the issue with the threads, but as I said, without more familiarity it was all too confusing for me. I will try the quick and dirty way once I'm back to work, though, and look into actually doing the proper way once I have more time and the rush to have things done has passed. Thanks a lot! – Julio Nicolini Jan 18 '13 at 19:33
1

In my case, calling only repaint() or processEvents() won't do the job.

Within your function loop, call both QCoreApplication::processEvents(); and repaint();:

for (i;...)
{
    //do your calculations
    //...

    QCoreApplication::processEvents();
    repaint();
}

Calling ui->mywidget->update() didn't make any different as well.

(Tested for Qt4.8.3 on Kubuntu 12.10 and Qt5.0.1 on Windows XP)

piedramania
  • 103
  • 9