简体   繁体   中英

Changing QLineEdit TextBox's inside of loop dynamically

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!

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM