Results 1 to 4 of 4

Thread: Qfile Question - hard coded path

  1. #1
    Join Date
    Aug 2009
    Posts
    16
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Qfile Question - hard coded path

    Currently I'm reading content out of a USB stick that is hard coded in my software.


    Qt Code:
    1. QFile file("W:/TemperatureReadings/date.xls");
    To copy to clipboard, switch view to plain text mode 


    I want to change this so it isn't a hard coded path anymore and the software will recgonize any drive letter with folder (TemperatureReadings) and file name (date.xls)

    Is there an easy way to do this? I can't figure it out from the documentation.

    Thanks a lot!

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Qfile Question - hard coded path

    Get all drives via QDir::drives(), then "search" in all drives for a folder TemperatureReading using QDir::cd() (return value) and then check for the file. You can also use QFile::exists() with a constructed path directly.

  3. The following user says thank you to Lykurg for this useful post:

    fortyhideout12 (27th September 2010)

  4. #3
    Join Date
    Aug 2009
    Posts
    16
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qfile Question - hard coded path

    Thanks for the reply Lykurg!

    Could I just do? -

    Qt Code:
    1. QFile file("/TemperatureReadings/date.xls");
    2. file.open(IO_WriteOnly | IO_Translate | IO_Append);
    To copy to clipboard, switch view to plain text mode 

    I've never used QDir before so I'm kind of confused on how I would write out what you suggested.

    Thanks again

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Qfile Question - hard coded path

    Quote Originally Posted by fortyhideout12 View Post
    Could I just do? -

    Qt Code:
    1. QFile file("/TemperatureReadings/date.xls");
    2. file.open(IO_WriteOnly | IO_Translate | IO_Append);
    To copy to clipboard, switch view to plain text mode 
    I thought you want search the coputer drives for a that folder and file. Can't see that your code is doing that.

    Since you want to learn something a little bit more elaborate:
    1. Get all drive names (Like c:, d:, f: etc.)
    2. Construct the path depending on the drives
    3. Check for file.


    QDir::drives returns a list of QFileInfos. See QList on how to iterate over a list. For each drive construct the path for drive (one list entry) and your specified folder and file name. (QDir::separator() is useful). Check if the file exist via QFile::exists(). If it exist, read it.

Similar Threads

  1. QFile open question
    By chenxuelian in forum Qt Programming
    Replies: 9
    Last Post: 2nd April 2010, 10:00
  2. Replies: 8
    Last Post: 17th October 2009, 08:10
  3. Include path question
    By MarkoSan in forum Qt Programming
    Replies: 3
    Last Post: 28th September 2009, 13:21
  4. Qfile question
    By dreamer in forum Qt Programming
    Replies: 1
    Last Post: 3rd May 2008, 15:32
  5. Replies: 16
    Last Post: 4th October 2007, 22:04

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.