Results 1 to 5 of 5

Thread: QThreads and the main GUI Thread

  1. #1
    Join Date
    Oct 2007
    Posts
    78
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QThreads and the main GUI Thread

    There and many producer and consumer threads that share separate circular buffers that need to communicate data back to the main GUI thread for display.

    The main GUI is made up of QtDesigner files that process their data using QtScript files. ( I mention this since my C++ must fairly generic since the .ui can change at any time and the computations to match the .ui are scriptable.)

    I was having the scripts process the circular buffer from within the threads but referencing the .ui object in the main thread from another thread and inside a script seems to be a big no no. Since there are many threads no matter where I create the object it will always be out of context.

    My questions is since running a circular buffer with threads and the main gui thread defeats the purpose, I am going to need a way to check the buffer and run scripts on it from within the main gui thread.

    My best guess was to QMutex a counter for the buffer and then have a QTimer from the main gui fire off every second or so and process any data. This is no where as efficient as a producer/consumer in separate threads but I am unsure how to keep the main gui responsive and achieve the same results.

    Any suggestion are appreciated.

  2. #2
    Join Date
    Oct 2007
    Posts
    78
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QThreads and the main GUI Thread

    Here is a more refined version of the question.

    I have a .ui object with a QTextEdit in the main thread.

    I have a separate thread with a script that needs to add data to the QTextEdit. I know I can communicate across threads with signal/slots in C++ but I am unsure if it is possible or the syntax needed to do so from a script that is signaling from a separate thread.

    Reading the docs on "Making Applications Scriptable" it makes reference to but I am unsure of the syntax. Here is a guess at it.

    Script File:
    Qt Code:
    1. function main(ui) {
    2.  
    3. this.ui = ui;
    4.  
    5. ui.myQTextEdit.setText.connect(mySetText);
    6. }
    7.  
    8. function mySetText() {
    9.  
    10. // text to be signaled to the ui object across threads
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

    If I do something like:
    Qt Code:
    1. ui.myQTextEdit.setText('my new text');
    To copy to clipboard, switch view to plain text mode 

    I get the dreaded QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QTextDocument(0x8601fc8), parent's thread is QThread(0x849a470)

    Any insight would be greatly appreciated.

    Bob

  3. #3
    Join Date
    Dec 2009
    Location
    Romania, Iasi
    Posts
    18
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThreads and the main GUI Thread

    Yes ... this is true! You can not create a QObject in a thread which has a parent in another thread. Please read carefully this: http://doc.trolltech.com/4.6/threads-qobject.html

  4. #4
    Join Date
    Oct 2007
    Posts
    78
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QThreads and the main GUI Thread

    I appreciate the response vcernobal, but please re-read my post as I have read the docs and my questions is about signals across threads in a scriptable object and the syntax needed.

    Bob

  5. #5
    Join Date
    Oct 2007
    Posts
    78
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QThreads and the main GUI Thread

    Could not find any references in any of the scriptable docs but did find http://bugreports.qt.nokia.com/browse/QTBUG-2524 so I guess threaded communication using signal/slots from a script is not possible at this time.

    Bob

Similar Threads

  1. Replies: 9
    Last Post: 28th November 2009, 20:31
  2. Replies: 16
    Last Post: 7th October 2009, 08:17
  3. Replies: 6
    Last Post: 29th April 2009, 18:17
  4. Main thread - worker thread communication.
    By kikapu in forum Newbie
    Replies: 25
    Last Post: 23rd May 2007, 22:09
  5. Main Thread (GUI Thread)
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 18th November 2006, 16:07

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.