Results 1 to 6 of 6

Thread: Problem using QFile inside QThread

  1. #1
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    24
    Thanked 17 Times in 14 Posts

    Question Problem using QFile inside QThread

    Hi,

    I have this code inside the run() of a QThread:

    Qt Code:
    1. outfile.setFileName(outputFile);
    2. if (!outfile.open(QIODevice::WriteOnly | QIODevice::Text))
    3. return;
    4.  
    5. outStream.setDevice(&outfile);
    6. outStream << "Some data here!" << "\n";
    7.  
    8. outfile.close();
    To copy to clipboard, switch view to plain text mode 

    Both outfile and outStream are private members of the thread class:
    Qt Code:
    1. QFile outfile;
    2. QTextStream outStream;
    To copy to clipboard, switch view to plain text mode 

    When I ran it. The out file is created but it has 0 bytes.

    Funny thing is that when I use QFile.Write() it works.

    Any idea why?

    Thanks,
    Carlos.
    Last edited by qlands; 3rd May 2011 at 16:09.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Problem using QFile inside QThread

    what happens if you add flush() before you close the file?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    24
    Thanked 17 Times in 14 Posts

    Default Re: Problem using QFile inside QThread

    The same thing happens: The file is created but QTextStream fail to move the data into the file.

    If I copy the same code outside the thread class into a the main() for example, it works just fine.

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

    Default Re: Problem using QFile inside QThread

    Warning: Threads are not my strong field!
    But maybe it has to do, that outfile and outStream are not in the same thread as the run() method because they are private members of the class? So try to declare and define them in the run method.

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Problem using QFile inside QThread

    If I copy the same code outside the thread class into a the main() for example, it works just fine.
    Its hard to say without seeing more code.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    24
    Thanked 17 Times in 14 Posts

    Default Re: Problem using QFile inside QThread

    The full code is:

    sqlimport.h
    Qt Code:
    1. class sqlimport : public QThread
    2. {
    3. Q_OBJECT
    4. public:
    5. void run(); //The main thread process
    6. QString outputFile;
    7.  
    8. private:
    9. QFile outfile;
    10. QTextStream outStream;
    11. };
    To copy to clipboard, switch view to plain text mode 

    sqlimport.cpp
    Qt Code:
    1. void sqlimport::run()
    2. {
    3. outfile.setFileName(outputFile);
    4. if (!outfile.open(QIODevice::WriteOnly | QIODevice::Text))
    5. return;
    6.  
    7. outStream.setDevice(&outfile);
    8. outStream << "Some data here!" << "\n";
    9.  
    10. outfile.close();
    11. }
    To copy to clipboard, switch view to plain text mode 

    That's it. If you do:

    Qt Code:
    1. sqlimport mysubproc;
    2. mysubproc.outputFile = "/data/myfile.sql";
    3. mysubproc.start(); //Run the thread
    To copy to clipboard, switch view to plain text mode 

    The output file is created but QTextStream fails to write the data.

    Carlos.

Similar Threads

  1. Replies: 2
    Last Post: 1st February 2011, 22:52
  2. QFile locks debug environment in QThread
    By nickolais in forum Qt Programming
    Replies: 1
    Last Post: 12th February 2009, 08:20
  3. Replies: 4
    Last Post: 26th June 2008, 19:41
  4. how to use QHttp inside QThread in Qt3
    By alusuel in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2006, 12:19
  5. QThread, QFile and QTcpSocket freezes GUI
    By naresh in forum Qt Programming
    Replies: 1
    Last Post: 24th June 2006, 00:25

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.