Search code examples
qtpathfilenames

Store file path into a QString [Qt]


I want to open a file via a Dialog File and then store the file path into a text file. The problem is that when I use the absoluteFilePath() function I get an error. Here is my code:

filename = QFileDialog::getOpenFileName(this, "Open File","*.H86;;*.glo");

//enregistrer le dernier chemin utilisé pour ouvrir un fichier
FILE* fichier = NULL;
fichier = fopen("LastPath.txt","w");
if(fichier != NULL)
{

    QString filename_fichier_choisi = filename.QFileInfo::absoluteFilePath();
    fputs(filename_fichier_choisi.toLatin1(),fichier);

    //fichier->write(filename.toLatin1());
}
fclose(fichier);

The error I get is "QFileInfo is not a base of QString. Which function could I use to get the path as a String ?


Solution

  • First of all, why are you mixing Qt and FILE* and not using QFile directly?

    You can do the following to get the file path:

    QString file = QFileInfo(filename).absoluteFilePath();
    

    or if you only want the folder:

    QString folder = QFileInfo(filename).absolutePath();
    

    It seems that you are new to C++. You first need an object of a specific type before you can call functions on that type. Thus QFileInfo(filename) creates an QFileInfo object and then the .absoluteFilePath() calls the function on the created object.