Results 1 to 9 of 9

Thread: QFile::resize takes forever and freezes the GUI

  1. #1
    Join Date
    May 2008
    Posts
    15
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QFile::resize takes forever and freezes the GUI

    QFile::resize takes forever for a 300MB file

    I know, that QFile::resize() writes 0x00 on every position and might need that long, that's the reason, why I have put it into a QThread, but still all action halts until it is ready.
    (I init the QThread in the main-window constructor, maybe that's wrong?)

    Also because it is not necessary to initialize the file with 0x00s, is there a way just to allocate the file-space without initializing (that should be totally fast)?

    Thanks

    (using: QT4.3 mingw XP)

    PS: how can I edit a thread-title to "[solved]..." if its solved?

  2. #2
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::resize takes forever and freezes the GUI

    When your main-thread stalls you did not create a new thread/did not start it. I suggest rerading the Documentation about QThread carefully.
    The only way to resize without setting bytes to 0 is to not use Qt here.

  3. #3
    Join Date
    May 2008
    Posts
    15
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::resize takes forever and freezes the GUI

    Hi Christian,
    thanks for answering

    Qt Code:
    1. MyMain::MyMain()
    2. {
    3. ...
    4. fileThread= new MyFileThread(this);
    5. fileThread->start();
    6. }
    7.  
    8. void MyFileThread::run()
    9. {
    10. exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    the fileThread->isRunning() returns true, and the QTimer in fileThread starting the resize, works too.

    I'm almost going to rewrite the QFile::resize with inserted qApp->processEvent() (don't know yet if it would be secure enough)

    The only way to resize without setting bytes to 0 is to not use Qt here.
    That sounds good, but could you please help me here, because I couldn't find an example how to do it, everywhere I looked, it is initialized with some value. (any link or code snippet would be highly appreciated)

    Thanks

  4. #4
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::resize takes forever and freezes the GUI

    Looks fine - and where do you do the actual resize?

    A WInAPI function to use is SetFilePointer() but you should remember that it's winapi and not protable. Also winapi isn't that easy for beginners.

  5. #5
    Join Date
    May 2008
    Posts
    15
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::resize takes forever and freezes the GUI

    Qt Code:
    1. MyFileThread::MyFileThread(QObject * parent ) : QThread(parent)
    2. {
    3. setPriority(QThread::LowestPriority);
    4. timer = new QTimer();
    5. connect(timer, SIGNAL(timeout()), this, SLOT(save()));
    6. timer->start(60000);
    7. }
    8.  
    9. void MyFileThread::save()
    10. {
    11. timer->stop();
    12. QFile file("test.dat");
    13. resizeSuccess = file.resize(data->files_info->size);
    14. }
    To copy to clipboard, switch view to plain text mode 

    I just tried it again (with fileThread= new MyFileThread()<-without the parent);, and it still stalls the gui until it's done resizing.

    A WInAPI function to use is SetFilePointer() but you should remember that it's winapi and not protable. Also winapi isn't that easy for beginners.
    I have never used winapi so it would be a new realm ..sigh.. but I will look it up
    I've read there is also a possibility doing something similar in the ext3-linux filesystem
    (but it will be primarily for win-xp)
    thanks

  6. #6
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::resize takes forever and freezes the GUI

    You use QThread wrong. The connect is done in the main thread and since it's a direct connection, the resize will also be done in the main thread and therefore the GUI is stalled.

  7. #7
    Join Date
    May 2008
    Posts
    15
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::resize takes forever and freezes the GUI

    Okay, so I have to put the connect here?
    Qt Code:
    1. void MyFileThread::run()
    2. {
    3. timer = new QTimer();
    4. connect(timer, SIGNAL(timeout()), this, SLOT(save()));
    5. timer->start(60000);
    6. }
    To copy to clipboard, switch view to plain text mode 

    that doesn't work either - I have no clue..sigh..

  8. #8
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::resize takes forever and freezes the GUI

    This should work - now QTimer is also in the thread. Plz also ready Qt::ConnectionType Documentation.

    btw: Why not use QTimer::singleShot() ?

  9. The following user says thank you to ChristianEhrlicher for this useful post:

    MaximA (27th May 2008)

  10. #9
    Join Date
    May 2008
    Posts
    15
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile::resize takes forever and freezes the GUI

    Thanks, I have some serious reading and recoding to do ('cause it's still not working)
    (Threads aren't trivial )

    the timer will later be used repeatedly for different files

    I come back later, after some "homework"

    Have a nice day.

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.