0

The snippet I've used is as follows

QDirIterator it(dir, filters, QDir::NoDotAndDotDot | QDir::AllEntries , QDirIterator::Subdirectories | QDirIterator::FollowSymlinks);
    while(it.hasNext())
    {
        it.next();
        if(QFileInfo(it.filePath()).isFile())
        {
            QString name=it.fileName();
            ui->textBrowser_filename->append(name);
        }
    }

It goes through all directories and rather than displaying the filename one-by-one, as I said, it displays the contents of textBrowser all-at-once. What's wrong is happening?

  • add `qApp->processEvents();` after append. – eyllanesc Oct 15 '17 at 21:23
  • What makes the above method is to force the GUI loop update, the GUI for various tasks such as listening for signals, calling slots, etc. do not perform all tasks immediately. Another option is that each iteration of the while is handled by a QTimer. – eyllanesc Oct 15 '17 at 21:31

2 Answers2

0

The problem is that you're in a tight loop (while(it.hasNext())), and Qt won't be able to render anything you've asked it to until you exit that loop and control of the UI thread returns to the Qt event loop.

You might be able to get it to render on each loop using QCoreApplication::processEvents, but depending on how many files you add to your text browser, it may become sluggish

QDirIterator it(dir, filters, QDir::NoDotAndDotDot | QDir::AllEntries , QDirIterator::Subdirectories | QDirIterator::FollowSymlinks);
    while(it.hasNext())
    {
        it.next();
        if(QFileInfo(it.filePath()).isFile())
        {
            QString name=it.fileName();
            ui->textBrowser_filename->append(name);

            QCoreApplication::processEvents(); // this will cause the filename to appear
        }
    }
Steve Lorimer
  • 27,059
  • 17
  • 118
  • 213
  • so, is there any alternative for this, that is, could the performance be improved by using other widget than textBrowser (sorry I'm beginner to it, you can just tell me the widget (if any) I'll read the docs). – Rohit Kumar Oct 15 '17 at 21:32
  • @secretsinside you could only call processEvents every N files you add, as one idea – Steve Lorimer Oct 15 '17 at 21:35
0

I agree with the first part of Steve's answer: updates to the GUI are expensive and only happen when control returns to the Qt main application event loop.

However, rather than calling processEvents() manually inside the loop (which generally should be avoided), I'd recommend constructing a single string by joining all the filenames you want to add, and then making only a single call to the QTextBrowser::append() function.

Like so:

QStringList list;
while (it.hasNext()) {
    if (QFileInfo(it.filePath()).isFile()) {
        list << it.fileName();
    }
    it.next();
}
ui->textBrowser_filename->append(list.join('\n'));

If this really takes a long time, as it might if you have many hundreds or thousands of files, you can run the code segment that creates the string list inside a background thread (using QThreadPool, QRunnable, QtConcurrent, etc.), and when that is finished, update the text browser with the large string.

bnaecker
  • 6,152
  • 1
  • 20
  • 33
  • But this code is not displaying the content one-by-one, what I've asked actually, as far I read and run the above code, it is taking the filename in the list and lastly appending by joining each entry with a new line. – Rohit Kumar Oct 15 '17 at 21:44
  • Ah, I misunderstood your question. Yeah, if you want it to display one by one, then the only real option is to use Steve's answer, calling `processEvents()` after each item is added. – bnaecker Oct 15 '17 at 23:07