0

I'm printing the filenames in textBrowser_filename . But on commenting the snippet which prints the filename in textBrowser it runs smoothly. So is there any upper limit of text which can be printed/stored inside the textBrowser in Qtcreator? following is the code which runs smoothly but not, if comment is removed.

void MainWindow::on_pushButton_browse_clicked()
{
    ui->textBrowser_filename->setLineWrapMode(QTextEdit::NoWrap);
    ui->textBrowser_filename->setText("");
    QString dir= QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                                                   "/home",
                                                   QFileDialog::ShowDirsOnly
                                                   | QFileDialog::DontResolveSymlinks);
    ui->lineEdit_dir->setText(dir);
    QDirIterator it(dir, QDirIterator::Subdirectories | QDirIterator::FollowSymlinks);
    /*
    while(it.hasNext())
    {
        QString path=it.fileName();
        ui->textBrowser_filename->append(path);
        it.next();
    }
    */
}

It would be great help if you could help me with another problem which is, why my file name also prints the following(which is commented)?

sample.txt
.          //why this is printed
helloWorld.png
..         //why this is printed

thank you!

  • To hide dot files you want to use QDir::NoDotAndDotDot filter http://doc.qt.io/qt-5/qdiriterator.html#QDirIterator-2 – Mateusz Drost Oct 05 '17 at 19:53
  • unfortunately, after applying QDir::NoDotAndDotDot filter nothing is printed.:( – Rohit Kumar Oct 05 '17 at 20:11
  • I added it like `QDirIterator it(dir, QDir::NoDotAndDotDot , QDirIterator::Subdirectories | QDirIterator::FollowSymlinks);` //which is pretty correct as per the definition – Rohit Kumar Oct 05 '17 at 20:13
  • resolved by using `QDirIterator it(dir, QDir::NoDotAndDotDot | QDir::AllEntries , QDirIterator::Subdirectories | QDirIterator::FollowSymlinks);` – Rohit Kumar Oct 05 '17 at 20:38

1 Answers1

1

As to your second question, why . and .. are printed. That's because they are files in the current directory. They correspond to the current directory and the parent, respectively. You can ignore them if you want using QDir::NoDotAndDotDot using QDir::setFilter(). But they exist and the QDirIterator will iterate over them unless you specify otherwise.

As for your main question, why the QTextBrowser is not running "smoothly". I don't know exactly what you mean. Is it slow? Does it work at all? How many files do you have in the directory?

If it's just slow, you could try to join all the filename strings into a single one, and only then call QTextBrowser::append() with the joined string. For example:

dir.setFilter(QDir::NoDotAndDotDot); // ignore . and ..
QDirIterator it(dir, QDirIterator::Subdirectories | QDirIterator::FollowSymlinks);
QStringList files;
while (it.hasNext()) {
    files << it.fileName();
    it = it.next();
}
ui->textBrowser_filename.append(files.join('\n'));
bnaecker
  • 6,152
  • 1
  • 20
  • 33
  • 1
    That would be my suggestion also. I suspect that every time text is added, the whole text browser has to have its internal state recomputed. But you don't see anything happening, because the main event loop is not being called, so no UI updates are shown until right at the end. – Kevin Boone Oct 05 '17 at 20:04
  • by "smoothly" i mean without any console error, it worked fine. – Rohit Kumar Oct 05 '17 at 20:15
  • but If Comments are removed than it just stuck and a console error says the program ended with exit status 1. its not 'slow' if my words mis leaded you – Rohit Kumar Oct 05 '17 at 20:16
  • Well, you never mentioned that in your original post! I'd suggest stepping through with a debugger, or printing out `qDebug()` messages as you're adding new lines to the browser. Are you sure the error occurs because of the commented-out code? – bnaecker Oct 05 '17 at 20:18
  • no i just guessed it, because when I commented _that part_ it worked _smoothly_:) – Rohit Kumar Oct 05 '17 at 20:26
  • Ok, then definitely use a debugger or print console messages as the program progresses, so that you can figure out what the *real* problem is. The code you posted here looks fine to me, so update your question with whatever errors you discover in that process. – bnaecker Oct 05 '17 at 20:27
  • I edited the line `QDirIterator it(dir, QDir::NoDotAndDotDot , QDirIterator::Subdirectories | QDirIterator::FollowSymlinks);` but nothing is now printed inside the textBrowser – Rohit Kumar Oct 05 '17 at 20:28
  • The `QDir::NoDotAndDotDot` filter can't be passed to the `QDirIterator` constructor like that. You must use `QDir::setFilter()`. Look at my updated post. – bnaecker Oct 05 '17 at 20:35