Rohit Kumar
Rohit Kumar

Reputation: 29

why my qt program get stuck?

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!

Upvotes: 0

Views: 368

Answers (1)

bnaecker
bnaecker

Reputation: 6430

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'));

Upvotes: 1

Related Questions