Results 1 to 6 of 6

Thread: Threads - General issue!

  1. #1
    Join Date
    Jun 2007
    Location
    India
    Posts
    33
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Threads - General issue!

    Hello all,
    I have an application with separate .ui file for the GUI.
    Now the UI has a statistics page with few tables which needs to update some data periodically. Data is fetched from backend[written in C] using a simple [update]function. This function involves certain manipulations on UI elements(ex: ui.trafficStatsTable->setItem(row, 1, resourceItem2); ).

    Now i want to update the table repeatedly in a separate thread. I havent used threads before.
    I tried pthreads and am not able to implement the required function as void*

    Then I went for QThread. Again, faced several problems. I was not able to access the UI elements.

    I found this in our documentation.
    In GUI applications, the main thread is also called the GUI thread because it's the only thread that is allowed to perform GUI-related operations.
    Could someone please explain what this means? Does it mean that simple operations like filling a table with data cannot be done in a different thread?

    Could someone provide some help please? Any pointers to some examples involving threads where UI is done separately would be of much help.

    Thanks in advance.
    Deepak

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Threads - General issue!

    you can not directly acces the gui elements in seperate thread...

    but you can use singals and slots that will update the gui... in other words... in your thread class make a signal like

    void addTextToTable(QString text);

    and then emit it whenever you want to change the text in gui...
    in the main gui thread... create a slot and connect it to the above signal..
    thats it.

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

    deepakn (24th August 2009)

  4. #3
    Join Date
    Jun 2007
    Location
    India
    Posts
    33
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Threads - General issue!

    Thank you MrDeath, that was exactly some guidance i was searching for. I will try it now and post back the results.

  5. #4
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Threads - General issue!

    Take a look at the Queued Custom Type example. It will show you how can you create a worker thread and send custom data to the GUI using signals/slots across threads.

  6. The following user says thank you to victor.fernandez for this useful post:

    deepakn (27th August 2009)

  7. #5
    Join Date
    Jun 2007
    Location
    India
    Posts
    33
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Re: Threads - General issue!

    Queued Custom Type Example was of much help. thanks a lot victor.
    Now I am stuck with another issue, and I think its again a silly one. Still gotta find a solution!

    Here is my code:
    master.cpp
    Qt Code:
    1. thread = new RenderThread();
    2. connect(ui.updateStatsButton, SIGNAL(clicked()), this, SLOT(updateStats()));
    3. ....
    4.  
    5. //in updateStats()
    6. thread->startOffThread(sessionName, logPos, event_id);
    To copy to clipboard, switch view to plain text mode 

    thread.cpp
    Qt Code:
    1. void RenderThread::startOffThread(const QString &str, int logPos, Event_Id event_id)
    2. {
    3. ...
    4. //some manipulations on variables supplied
    5. start();
    6. }
    7.  
    8. //in RenderThread::run()
    9. do {
    10. ...
    11. emit sendString(str, str2);
    12. sleep(1);
    13. }while (some_condition);
    To copy to clipboard, switch view to plain text mode 

    Now here is my problem - I have multiple Events on which the above actions have to be performed independently.
    suppose i have Event01 and Event02. I clicked updateStats button for Event01 and code is executed properly and stats are being updated. Meanwhile I clicked updateStats for Event02 (where in functioncall
    Qt Code:
    1. thread->startOffThread(sessionName, logPos, event_id);
    To copy to clipboard, switch view to plain text mode 
    the variables supplied differ) Event01 updation stops and Event02 starts!


    I want Event01 and Event02 (and many many more) to run parallelly without affecting each other.

    How do I implement multiple threads?
    How do start and stop them? I could not find anything similar to thread_id from pthreads.
    someone please help...
    Last edited by deepakn; 31st August 2009 at 12:10. Reason: updated contents

  8. #6
    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: Threads - General issue!

    Read about QtConcurrent::run()
    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.


Similar Threads

  1. Replies: 8
    Last Post: 27th March 2013, 11:51
  2. Why do some QWidgets create own threads?
    By donglebob in forum Qt Programming
    Replies: 5
    Last Post: 6th July 2010, 17:01
  3. How to wait for events produced by non-Qt threads?
    By Palpatine in forum Qt Programming
    Replies: 3
    Last Post: 21st May 2009, 21:45

Tags for this Thread

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.