Letting the User set the respective directory within a qt wigdet application



  • Hi there,

    I'm pretty new to qt and I am currently working on a university project in which I would like to let the user select a directory (in order to save files that contain information needed for the application). I accomplished this step so far, but I can't seem to figure out how I can set the chosen directory as the working directory for the application, so that the application can work on and with the files saved in said directoy. Is there a way of setting the directory selected by the user as the working directory for the application?
    I'm sorry if this is a mundane problem, I did not find an answer to my question so far, but I might've searched wrongly. At least it finally made me sign up for this forum :3 I'm looking forward to learning more about qt and c++ and I hope that someone might have an answer to my question.

    edit: I'm programming in c++, edit edit: i guess that was obvious considering the thread i posted in



  • @Pippapo
    Why don't you Google for: qt5 set current directory ? There are so many matches.....

    BTW, you don't have set the current/working directory just to save/load files, you can always pass around full paths if you prefer.



  • @JonB first of all, thank you for your reply. I did search exactly that and I set the current directory with QDir to the one the user chose. However when I want to access the current directory within a different method, the current directory seems to swap back to the default directory.

    edit: it would however be possible to save the path the user chose and pass it around.. I just though that there might be a better way to solve this since qt has so many options and tools to handle different situations.



  • @Pippapo said in Letting the User set the respective directory within a qt wigdet application:

    I set the current directory with QDir to the one the user chose.

    Please show your code. We have no idea what you did.

    the current directory seems to swap back to the default directory

    Using what? Are you using one of the "file load/save" dialogs, or your own code, or what?

    You have to show these things for us to be sure what you are doing.



  • Thank you again for your reply <3 I hope that my code is readable. If you have any questions or if it is horrible to read and you have any suggestions on how to make it more readable, I'd be happy for your input.
    Okay, so here it is: When the user clicks pushbutton 5 the whole setting directory, creating a file and saving the file-stuff happens

    void MainWindow::on_pushButton_5_clicked()
    {
    
        QString fileName = QFileDialog::getSaveFileName(this, tr("Creating new Project..."),
                                                                 "C://", "All files (*.*);; Text File (*.txt)");
        if (fileName.isEmpty()){
            return;
        }
        else{
            QFile projectFile(fileName);
            QFileInfo fi(projectFile);
            QString directory = fi.filePath();
            QDir dir(directory);
            dir.setCurrent(directory);
    
            if (!projectFile.open(QIODevice::WriteOnly | QIODevice::ReadOnly)){
                QMessageBox::information(this, tr("Unable to open file."), projectFile.errorString());
                return;
            }
            QTextStream stream (&projectFile);
            //some text 
            stream << "S U M M A R Y" << endl;
            projectFile.close();
            CityNetwork projectNetwork(fileName, directory); //Stuerzt hier irgendwie ab
        }
        ui->stackedWidget->setCurrentIndex(3);
    }
    

    Saving the file within the chosen directory is no problem, but is there a way to keep the chosen directory as the current directory throughout the whole run of the application?

    Clicking Push Button 6 is supposed to add additional Text to the respective file:

    void MainWindow::on_pushButton_6_clicked(){
        //PROBLEM: Directory is default directoy
        //Getting directory path and the respective city-file in order to add more
        //information about the cities to the file
        //I added some lines in order to check in which directory the application is operating
        QString path = QDir::currentPath();
        QFile(path).setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner);
        QFile projectFile(path);
        QMessageBox msgBox;
        msgBox.setText(path);
        msgBox.exec();
    
        //Can not access file, because the file is in the directory the user chose, 
        //while "path" shows that the directory swapped back to the default directory of qt
        if (!projectFile.open(QIODevice::ReadOnly | QIODevice::WriteOnly)){
            QMessageBox::information(this, "Unable to open File.", projectFile.errorString());
            return;
        }else{
            QTextStream stream (&projectFile);
            stream << ui->lineEdit_2->text() << "\t" << ui->lineEdit_3->text() << endl;
        }
    }
    

    I hope that the code helps to understand my problem.



  • @Pippapo said in Letting the User set the respective directory within a qt wigdet application:

        QString directory = fi.filePath();
        QDir dir(directory);
        dir.setCurrent(directory);
    

    http://doc.qt.io/qt-5/qfileinfo.html#filePath does not return a directory, it returns the full path to the file, directory plus filename, from the docs? You should check the return result from your http://doc.qt.io/qt-5/qdir.html#setCurrent, I imagine it's returning false? Try using something more like http://doc.qt.io/qt-5/qfileinfo.html#absoluteDir ?

    P.S. At least while you're new, if you've got a function like http://doc.qt.io/qt-5/qdir.html#setCurrent which returns a result always check that! And use something like qDebug() << ... to check things as you go along, e.g.

    QString directory = fi.filePath();
    qDebug() << directory;
    

    might have helped you.



  • @JonB Thank you so much for your reply. I am sorry I did not get back to you sooner, life kept me busy. I'll get right back to my project and I'm really thankful for your help and the information you provided.


Log in to reply
 

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