Page 2 of 2 FirstFirst 12
Results 21 to 30 of 30

Thread: Saving a file using a relative path

  1. #21
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Saving a file using a relative path

    Is it possible that the class constructor is called before QDir::setCurrent(QCoreApplication::applicationDirP ath()); ?


    Added after 5 minutes:


    Quote Originally Posted by admkrk View Post
    I think I understand what you are saying, but am not sure how that would affect this since I am not running it from command line.

    I did as you suggested before and used
    Qt Code:
    1. QDir::setCurrent(QCoreApplication::applicationDirPath());
    To copy to clipboard, switch view to plain text mode 
    in main(). Is that not supposed to make QDir::current() constant? The only difference is where in the class I put
    Qt Code:
    1. QString path = QDir::current().currentPath();
    2. path.append("/data/completer.txt");
    To copy to clipboard, switch view to plain text mode 
    If I put it in the constructor, the result is: path_to_Build_folder/data/completer.txt.
    If I put it in my function, the result is: path_to_Build_folder/debug/data/completer.txt.
    The only difference I can see is when it is called. What I do not understand is how the directory is affected by that.

    I am starting to think that having it in the constructor is not a good idea anyway, since the list will not be updated if it is changed somewhere else. I do not really want to do a read every time the user edits a cell either, though.
    The IDE is running application that way (simulation) :
    Qt Code:
    1. cd path_to_Build_folder<ENTER>
    2. debug/app_name<ENTER>
    To copy to clipboard, switch view to plain text mode 
    It is like example number 2 from my post.
    Last edited by Lesiok; 18th July 2017 at 07:36.

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

    Default Re: Saving a file using a relative path

    I did notice that the constructor was called sooner than I thought it would be, but I still thought it would have been after setCurrent(). Thinking about it now, I see how that was not necessarily right.

    Quote Originally Posted by Lesiok View Post
    The IDE is running application that way (simulation) :
    Qt Code:
    1. cd path_to_Build_folder<ENTER>
    2. debug/app_name<ENTER>
    To copy to clipboard, switch view to plain text mode 
    It never crossed my mind to think about how the IDE runs the application. I made a very simple IDE, as a school project, a couple of years ago. I do not remember how I handled it, but I can see how that could be the case.

    Thank you for your patience with me.

  3. #23
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Saving a file using a relative path

    It never crossed my mind to think about how the IDE runs the application.
    A common "gotcha!". The last word in IDE is "environment", and it is configured in such a way as to establish the current working directory, paths to executables and runtime binaries needed for development and debugging, and so forth.

    The IDE's environment may be different from the environment obtained when running from a command prompt, which may be different from the environment obtained when the program is run from a file system browser (like Windows Explorer), which may be different from the environment obtained when running a program from a Desktop icon or the Start menu.

    And if you use a file browser from within your program (to allow the user to select files to open), this also can change the current working directory.
    <=== 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. #24
    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!

  5. #25
    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?

  6. #26
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 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.

  7. #27
    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.

  8. #28
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 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.

  9. #29
    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.

  10. #30
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.