PDA

View Full Version : QThreads and the main GUI Thread



coderbob
20th January 2010, 11:35
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.

coderbob
22nd January 2010, 08:15
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:


function main(ui) {

this.ui = ui;

ui.myQTextEdit.setText.connect(mySetText);
}

function mySetText() {

// text to be signaled to the ui object across threads

}


If I do something like:


ui.myQTextEdit.setText('my new text');


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

vcernobai
22nd January 2010, 08:36
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

coderbob
22nd January 2010, 08:42
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

coderbob
22nd January 2010, 12:00
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