简体   繁体   English

为什么QTextBrowser中的文本不能一一显示,而是一次全部显示?

[英]Why text inside QTextBrowser is not displayed one-by-one instead it displays all-at-once?

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. 它遍历所有目录,而不是像我所说的那样一一显示文件名,而是一次显示textBrowser的内容。 What's wrong is happening? 怎么了

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. 问题是您处在紧密循环中( while(it.hasNext()) ),Qt将无法呈现您要求的任何内容,直到退出该循环并控制UI线程返回为止到Qt事件循环。

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 您可能可以使用QCoreApplication::processEvents使其在每个循环上呈现,但是根据您添加到文本浏览器中的文件数量而定,它可能会变慢

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
        }
    }

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. 我同意Steve回答的第一部分:GUI的更新非常昂贵,并且仅在控件返回到Qt主应用程序事件循环时才发生。

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. 但是,与其建议在循环内手动调用processEvents() (通常应避免这样做),我建议通过加入要添加的所有文件名来构造单个字符串,然后仅对QTextBrowser::append()进行单个调用QTextBrowser::append()函数。

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. 如果确实需要很长时间(例如,如果您有成百上千个文件),则可以运行在后台线程内创建字符串列表的代码段(使用QThreadPoolQRunnableQtConcurrent等),以及何时完成后,使用大字符串更新文本浏览器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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