Results 1 to 20 of 30

Thread: Saving a file using a relative path

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2014
    Posts
    59
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Saving a file using a relative path

    I might have to check all that out, when I get the time. I should not have a problem with any of that in this application though, except for this case where it seems I was trying to use a directory before it was established. At the very least, it something I am aware of it now.

    Thanks!

  2. #2
    Join Date
    Apr 2014
    Posts
    59
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Saving a file using a relative path

    OK, now I am even more confused. Using QFile::copy() and a relative path, it only seems to work sometimes.

    I have this in one function, and it works fine
    Qt Code:
    1. QFileInfo temp(imagePath);
    2. QString fileName = temp.fileName();
    3. QFile::copy(imagePath, projectPath + "/" + fileName);
    To copy to clipboard, switch view to plain text mode 
    In another function I had to change to this to get it to work
    Qt Code:
    1. QFileInfo temp(bomPath);
    2. QString fileName = temp.fileName();
    3. //QFile::copy(bomPath, projectPath + "/" + fileName);
    4. QString newPath = QDir::current().currentPath();
    5. newPath.append(projectPath + "/" + fileName);
    6. QFile::copy(bomPath, newPath);
    To copy to clipboard, switch view to plain text mode 
    bomPath and imagePath both come from QFileDialogs. In both cases
    Qt Code:
    1. qDebug() << "Current: " << QDir::current().currentPath();
    To copy to clipboard, switch view to plain text mode 
    prints the same thing. projectPath holds a relative path, to the same folder "/projects/Test". The only difference, I can see, is fileName, which includes different extensions; .jpg and .csv. What am I missing here? It makes me wonder if I switch the first one, to match the second for consistency, if it will still work?

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,315
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Saving a file using a relative path

    When you use your QFileDialogs do you invoke them using the static methods, eg. QFileDialog::getSaveFileName(), or do you create a QFileDialog instance on the stack, connect slots to the signals, and then call exec()?

    Qt Code:
    1. // mDBName is a member variable that keeps track of the last-opened database, stored in QSettings
    2. // mDBDir is a member variable that holds the path containing the mDBName, stored in QSettings
    3.  
    4. void MyMainWindow::onOpenDB()
    5. {
    6. QFileDialog fd( this, "Select Database File", mDBDir, "Database Files (*.db *.sqlite)" );
    7. fd.setFileMode( QFileDialog::ExistingFile );
    8. fd.setAcceptMode( QFileDialog::AcceptOpen );
    9. if ( !mDBName.isEmpty( ) )
    10. fd.selectFile( mDBName );
    11.  
    12. connect( &fd, &QFileDialog::fileSelected, this, &MyMainWindow::onDBFileSelected ); // updates mDBName and opens it
    13. connect( &fd, &QFileDialog::directoryEntered, this, &MyMainWindow::onDBDirectoryChanged ); // updates mDBDir
    14. connect( &fd, &QFileDialog::currentChanged, this, &MyMainWindow::onDBCurrentChanged );
    15. fd.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

    If you don't do something like this, then it might be helpful to reorganize your code so that it does. By looking at what is being sent to each of your slots and comparing it to what your logic thinks the current directory is might help you understand why your relative paths aren't what you think they should be.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Apr 2014
    Posts
    59
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Saving a file using a relative path

    The whole section for copying the image looks like this
    Qt Code:
    1. QString dir = QStandardPaths::writableLocation(
    2. QStandardPaths::PicturesLocation);
    3. QString imagePath = QFileDialog::getOpenFileName(
    4. this, "Select Image", dir, "Images(*.jpg);;All files(*.*)");
    5. if(imagePath == "")
    6. return;
    7.  
    8. QString projectPath = projectFiles.value("projectPath");
    9.  
    10. // Copy image to project directory.
    11. QFileInfo temp(imagePath);
    12. QString fileName = temp.fileName();
    13. QFile::copy(imagePath, projectPath + "/" + fileName);
    To copy to clipboard, switch view to plain text mode 
    The other one is the same, except it looks for a different file type. projectFiles is a QHash that, in this case, gives the path where the file will be copied to. it may, or may not, be relative, but it is relative for this. Once the file gets copied, there are no problems with the rest of it, where it works on the copied file. I am not doing anything inside the dialog, other than getting the path to the file, so even if the current directory changes during that time, it should revert back once I exit it. Unless I am completely missing something.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,315
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Saving a file using a relative path

    it should revert back once I exit it. Unless I am completely missing something.
    That's exactly the point. You don't really know if the QFileDialog is changing what Qt thinks is the current path or not, and making the assumption that somehow QFileDialog pushes the current path onto some kind of stack and then pops it back off when it goes out of scope might be a wrong assumption.

    Modify your code to use the example I posted to set up the QFileDialog. Take your code that occurs from line 5 onward and move it into the slot that handles the QFileDialog::fileSelected() signal as I showed above, and check your paths there. Also do it following the fd.exec() call. You could put all of the code in my example method inside a sub-block (i.e. enclose it in braces ({}). Then, outside the braces, check the paths again. The QFileDialog would have gone out of scope at that point, so you can see whether or not the state is restored.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. #6
    Join Date
    Apr 2014
    Posts
    59
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Saving a file using a relative path

    Quote Originally Posted by d_stranz View Post
    That's exactly the point. You don't really know if the QFileDialog is changing what Qt thinks is the current path or not, and making the assumption that somehow QFileDialog pushes the current path onto some kind of stack and then pops it back off when it goes out of scope might be a wrong assumption.
    Yeah I can be pig headed sometimes. The next couple days are going to be busy for me, but I will report back with the results when I get the chance.

    I did check the current path in several places; before and after the QFileDialog call, for example, but did not think to scope it out.

  7. #7
    Join Date
    Apr 2014
    Posts
    59
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Saving a file using a relative path

    Change of plans so...

    I setup the dialog as suggested, and scoped it out from the declaration to exec(). Checked the current directory before the block, at the end of the block, after the block, and in the slot. The current directory never changed from the application directory. The file still would not copy, until I added the whole path. Before using the whole path, I also changed it so I could get the error message, which was the same as originally; Error: "Cannot create /projects/Test/7segment2.csv for output".

Similar Threads

  1. Replies: 0
    Last Post: 14th September 2013, 10:14
  2. Replies: 16
    Last Post: 24th January 2013, 02:52
  3. Downloading a file and saving in a path
    By StarRocks in forum Qt Programming
    Replies: 19
    Last Post: 3rd January 2013, 06:43
  4. qmake absolute vs relative LIBS path
    By TheShow in forum Newbie
    Replies: 7
    Last Post: 12th October 2010, 14:40
  5. Replies: 8
    Last Post: 17th October 2009, 08:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.