Results 1 to 2 of 2

Thread: How to access the same file in different scopes within a function

  1. #1
    Join Date
    Oct 2015
    Posts
    35
    Thanks
    11
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default How to access the same file in different scopes within a function

    Hello All,

    Please take a look at the below code. I posted a question earlier but had some changes after that.

    Qt Code:
    1. FILE *filename1;
    2.  
    3.  
    4. void MainWindow::on_checkBox_clicked()
    5. {
    6. QFile file(QString fname);//LINE1
    7.  
    8. if(ui->checkBox->isChecked())
    9. {
    10.  
    11. filename = QFileDialog::getSaveFileName(...);//LINE2
    12. if(!filename.isEmpty())
    13. {
    14. if(QFile::exists(filename)){
    15.  
    16. //Append 2 empty lines and append data(some data)
    17. }else { // I want to access the filename from LINE2 create a file with name entered by user and append some data
    18. file(filename);
    19. file.open(QIODevice::Append);
    20. //
    21. // Data to be appended
    22. //
    23. }
    24. }
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

    Can someone please help me understand how to access filename from LINE2 and do some stuff on that file in a different location within the function on_checkBox_clicked(). I'm trying to declare a file in LINE1 and use it to perform different checks, this is throwing some errors. Please explain the syntax for using the filename in a different scope/sub-scope(or what ever they call it) within the same function with above example. And if I want to access the same file in different function of my project, how can I access it. Thanks in advance.

  2. #2
    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 access the same file in different scopes within a function

    Have you read the errors? You will likely find that they tell you why line 6 is not what you expected. It reads like a declaration of a function called file() that takes a QString argument and returns a QFile, and not a local variable of type QFile. Remove the stuff from ( to ) and see how much further you get. You then need to focus on lne 18.

Similar Threads

  1. Access parent's function from child widget
    By el33t in forum Qt Programming
    Replies: 4
    Last Post: 16th March 2011, 14:20
  2. Two QProcess object access to same function
    By pronetin in forum Newbie
    Replies: 3
    Last Post: 20th February 2011, 11:44
  3. Replies: 2
    Last Post: 31st August 2009, 13:03
  4. access main window from function
    By eric in forum Qt Programming
    Replies: 6
    Last Post: 19th January 2008, 21:29

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.