Results 1 to 7 of 7

Thread: QThread and GUI

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2012
    Posts
    173
    Thanks
    48
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QThread and GUI

    Hello,

    I am trying to use QThread with GUI application, so the GUI wont stall on me.

    in th GUI, i have a window through witch i load a Txt file and parse it and do processing on it, they are in this manner

    Qt Code:
    1. loadFile (... )
    2. {
    3. readFile(..);
    4. parseFile(..);
    5. prossValues(...);
    6. }
    To copy to clipboard, switch view to plain text mode 

    when i run the loadFile slot, it takes long time, and the GUI stalls.

    what i did so far, is

    I sub-classed the QThread,
    override the run method and,
    emit a signal called loadFile(). in the run method.


    In the class where i need to load the file ,
    I create a mythread and,
    connect the loadFile signal to a loadFile slot.

    the the signal triggers fine and i load the file, but the gui still stalls,
    which i think it means that im still running on the main thread. not on the thread i created.

    so, Please let me know what im missing and what the mistake im making and how can i fix it.

    Thanks

    Jesse.
    Last edited by jesse_mark; 11th January 2013 at 18:48.

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

    Default Re: QThread and GUI

    We'd need to see the actual code. Bear in mind that QThread object lives in the main thread and not in the thread it represents, that's probably your issue.

    Anyway, I suggest you use QtConcurrent::run() instead of a standalone QThread. For such simple usecases it's much easier to use.

    Qt Code:
    1. QString loadTextFile(const QString &fileName) {
    2. QFile f(fileName);
    3. if(!f.open(...)) return QString();
    4. QTextStream stream(&f);
    5. return stream.readAll();
    6. }
    7.  
    8. // ...
    9. QFutureWatcher<QString> *futureWatcher = new ...; // optional
    10. connect(futureWatcher, SIGNAL(finished()), this, SLOT(...))); // optional
    11. QFuture<QString> future = QtConcurrent::run(loadTextFile, QStringLiteral("somefile.txt"));
    12. futureWatcher->setFuture(future); // optional
    13.  
    14. // ... do something in the meantime
    15.  
    16. QString content = future.result(); // will block if the result is not ready (alternative to useing QFutureWatcher)
    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:

    jesse_mark (11th January 2013)

Similar Threads

  1. Replies: 1
    Last Post: 4th October 2012, 14:49
  2. Need a little help with QThread
    By Slewman in forum Qt Programming
    Replies: 4
    Last Post: 15th March 2010, 20:04
  3. Replies: 4
    Last Post: 26th June 2008, 18:41
  4. Spawn a QThread in another QThread
    By david.corinex in forum Qt Programming
    Replies: 2
    Last Post: 19th December 2007, 12:54
  5. QThread?
    By vishal.chauhan in forum Newbie
    Replies: 4
    Last Post: 26th March 2007, 13:43

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.