3
QString FilePath2 = QFileDialog::getOpenFileName(this, tr("Open Directory"), "C:/", tr("Text files (*.txt)"));
QDir a = FilePath2;
qDebug() << a.absolutePath();

Code works fine. But absolutePath() is still returning the path + filename instead of only path.

C:/Users/Blastcore/Desktop/text.txt instead of only C:/Users/Blastcore/Desktop

Any idea?

Blastcore
  • 360
  • 7
  • 19
  • 5
    Try with `QFileInfo` instead of `QDir` [http://doc.qt.digia.com/qt/qfileinfo.html#absolutePath] – Kirween Dec 09 '12 at 01:46
  • 1
    Counterintuitively, `QDir` **can** hold a path to a file. If you need to select a directory, not a file, you should use `getExistingDirectory()` instead of `getOpenFileName()`. If you just need the dir in that particular place, use `QFileInfo` as Kirween suggested. – TC1 Dec 09 '12 at 01:53

1 Answers1

0

You could use QFileInfo instead of QDir. With your Example:

QString FilePath2 = QFileDialog::getOpenFileName(this, tr("Open Directory"), "C:/", tr("Text files (*.txt)"));
QFileInfo a(FilePath2);
qDebug() << a.absolutePath();
Angie Quijano
  • 4,167
  • 3
  • 25
  • 30