Getting proper folder path



  • In my qt c++ application when the button is clicked the file dialog opens and when the select folder button in the file dialog is clicked the folder path is to be taken to the line edit in my application. But though the folder path appears the current folder does not appear at the end!
    following is my code
    void MainWindow::on_Button_clicked()
    {
    QString Filepath = QFileDialog::getExistingDirectory(this, "Get Any File");
    QDir d = QFileInfo(Filepath).absoluteDir();
    QString absolute=d.absolutePath();
    ui->Path->setText(absolute);

        }
    

    If the current folder is B and the parent folder is A(which is on the desktop) it should depict "Desktop/A/B" on the line edit but currently it shows only "Desktop/A"!
    How can i correct this issue?



  • @Lasith hi,

    I don't quite understand why you go the detour over QFileInfo,

    this, should do exactly what you want to happen

    void MainWindow::on_Button_clicked()
    {
       QString Filepath = QFileDialog::getExistingDirectory(this, "Get Any File");
        ui->Path->setText(Filepath);
    }
    

    However if you insist on the useage QFileInfo, try it with QString QFileInfo::absoluteFilePath()

    void MainWindow::on_Button_clicked()
    {
       QString Filepath = QFileDialog::getExistingDirectory(this, "Get Any File");
        QDir d = QFileInfo(Filepath).absoluteDir();
        QString absolute=d.absoluteFilePath();
        ui->Path->setText(absolute);
    }
    


  • @J-Hilk Your code gives the method to get a file path but I need the folder path! my code works but does not show current folder in the absoulte path :(



  • @Lasith

    Taken from here:

    QFileInfo fi("c:/temp/foo"); => fi.absoluteFilePath() => "C:/temp/foo"
    

    Pretty much what you want to happen, as far as I understand it



  • @J.Hilk Thanx mate :) I had been too complex



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.