Results 1 to 11 of 11

Thread: how to specify variable file name?

  1. #1
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default how to specify variable file name?

    hello everyone....
    i'm opening a file as shown below....
    how can I specify variable file name......different time, user may give different file name.....
    When i'm trying to change the following format, i got error....
    Please help me

    Qt Code:
    1. QFile file(":/input.txt");
    2.  
    3. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    4. return;
    5. ui->label_4->setText("FILE NAME:"+filename);
    6. while (!file.atEnd())
    7. {
    8.  
    9. //DOING REST OPERATION...
    10. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 11th October 2011 at 10:00. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to specify variable file name?

    Qt Code:
    1. QString filename = ":/input.txt";
    2. QFile file(filename);
    3.  
    4. ui->label_4->setText(QString("FILE NAME: %1").arg(filename));
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    aurora (11th October 2011)

  4. #3
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to specify variable file name?

    but i'm getting error while opening variable name...
    Qt Code:
    1. QFile file(":/input.txt");
    2.  
    3. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    4. return;
    To copy to clipboard, switch view to plain text mode 
    Here instead of ":/input.txt", i want to specify a variable. Is that possible?
    Last edited by aurora; 11th October 2011 at 10:17.

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how to specify variable file name?

    Did you read Wysota's example?

  6. #5
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to specify variable file name?

    Quote Originally Posted by ChrisW67 View Post
    Did you read Wysota's example?
    ya...i dont want to open ":/input.txt" every time....
    it must be a variable....but when i changed that format ":/...." i got error...

  7. #6
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: how to specify variable file name?

    Check the Qt examples svgviewer and svggenerator, if you're using windows they are in
    C:\QtSDK\Examples\4.7\painting\svgviewer
    and
    C:\QtSDK\Examples\4.7\painting\svggenerator
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

  8. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how to specify variable file name?

    Quote Originally Posted by aurora View Post
    ya...i dont want to open ":/input.txt" every time....
    it must be a variable....but when i changed that format ":/...." i got error...
    filename is a variable. If changing it generated a error, presumably trying to open the file, it was probably because it no longer pointed at a valid file.

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to specify variable file name?

    Quote Originally Posted by aurora View Post
    ya...i dont want to open ":/input.txt" every time....
    it must be a variable....but when i changed that format ":/...." i got error...
    Maybe your real question is "how do I allow the user to choose a fle to open?"

    If so, then the answer is use QFileDialog::getOpenFileName().

    Qt Code:
    1. QString filename = QFileDialog::getOpenFileName();
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. The following user says thank you to wysota for this useful post:

    aurora (12th October 2011)

  11. #9
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to specify variable file name?

    No....i want to open a file, that file name is entered by user without any path...and my program will search in specific directory.
    Now i just confusing how can i read file name and search in directory?

    Quote Originally Posted by wysota View Post
    Maybe your real question is "how do I allow the user to choose a fle to open?"

    If so, then the answer is use QFileDialog::getOpenFileName().

    Qt Code:
    1. QString filename = QFileDialog::getOpenFileName();
    To copy to clipboard, switch view to plain text mode 
    This is also fine...but how can i select multiple files....it is just allowing me to select single file at one time
    Last edited by aurora; 17th October 2011 at 06:02.

  12. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how to specify variable file name?

    Did you read the friendly manual? QFileDialog::getOpenFileNames()

  13. #11
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to specify variable file name?

    Quote Originally Posted by ChrisW67 View Post
    Did you read the friendly manual? QFileDialog::getOpenFileNames()
    Thank u i got it...

Similar Threads

  1. Updating variable
    By felo188 in forum Qt Programming
    Replies: 3
    Last Post: 16th August 2011, 16:55
  2. A variable in setTable()
    By thefatladysingsopera in forum Newbie
    Replies: 6
    Last Post: 2nd August 2011, 08:37
  3. variable
    By Atuti2009 in forum Qt Programming
    Replies: 8
    Last Post: 18th November 2009, 09:04
  4. Entering variable in db
    By subratatamal in forum Qt Programming
    Replies: 1
    Last Post: 13th December 2008, 17:50
  5. pro-file's INSTALLS variable and make uninstall
    By roxton in forum Qt Programming
    Replies: 1
    Last Post: 4th July 2008, 09:16

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.