QScriptEngine and signal/slot processing



  • I have an application that processes long-running Qt scripts. The QScriptEngine runs in its own thread.

    The main thread in this application is an http REST server that processes remote status and control messages. The remote program may for example, download a new script, start or stop the script, etc.

    The script engine is extended by several QObjects added to the engine's global property.

    I have set a short event processing interval using QScriptEngine:: setProcessEventsInterval().

    A few things are not behaving as I would expect them to. I'm hoping to determine whether I have an incorrect understanding of how it should work or if I just have bugs in my code.

    One of the QObjects added to the engine context includes a QTcpServer. I have properties on the object which allow me to set the port and read the number of connections and bytes, and a slot that allows me to tell the server listen. The constructor of this object connects the newConnection signal to a slot on the same object, and in the new connection I set up a readyRead signal handler, typical QTcpSocket/QTcpServer stuff.

    I can set the properties and start the listener from a script as expected. The listener runs. I can telnet to the port and I get a connection. However it seems that the newConnection signal does not get serviced until the script stops. (I say this because I have a QDebug() statement in the new connection handler and the output appears when I stop the script.

    I guess the question I have is whether QScriptEngine has its own local event loop, enabling it to run correctly on a secondary thread. If not I suppose I can only have one interruptable scriptEngine in my application.

    Thanks in advance for any and all insights.I appreciate that the details of the ScriptEngine event processing is not exactly a mainstream Qt question.



  • It does sound like you have a "blocking" issue somewhere.
    I do not quite have a clear view of your code from your description, could you paste in some code snippets. It would help to see the following:

    Your thread generation (moveToThread, etc...) and startup.
    Your connections (slots and signals)

    Note: if you emit a signal to an object in another thread it will be queued by default, and it will not be actions until current "actions" in the pipe are finished - i.e. last in last out. So we have to be careful that the thread itself is not somehow blocking.



  • Hi
    Anyone have a Server Client project in GUI.

    Regards
    Pawan



  • @Pawan Pal ... umm, pretty sure that is not related to this question?? - make a new post (and maybe provide more details :o )



  • Hi Code,

    Thanks for the reply.
    The thread creation and signal slot are all good. The question is whether the QScriptEngine has a real Qt event loop at its base (can run on any thread), or if it just process events on the main thread's event loop by calling the static processEvents() method periodically. It looks like the latter is the case.



  • Well, I have not used QScriptEngine specifically, but I see no reason that it can not run in its own thread just like all the other Qt objects (and therefore have its own event queue).

    Once you move the object containing / inheriting-from QScriptEngine into another thread then signals that are processed in this thread are seperated from the main thread... i.e. if the main thread is sleeping/blocked then the QScriptEngine thread can still be running.

    If you are not seeing this behavior, then there may be something wrong with the way that your object/thread is setup... (unless there is something different about QScriptEngine that I don't know about).



  • how to take buffer to send the data in console



  • Use the "signals and slots":http://qt-project.org/doc/qt-4.8/signalsandslots.html mechanism for passing data around :)



  • Hi

    I am working serial port project.I want when i selected the text from combo box its not show the text editor, when i clicked the button the text are send to the console.

    its possible.

    Regards
    Pawan



  • how count the character in number and display.
    this is my code. its not working.

    void MainWindow::textcount()
    {

    string s;
       cout << flush;
       //ui->textEdit->toPlainText().count();
       getline(cin,s);
       const int size=s.length();
       cout << text_count << size << endl;
        text_count = ui->textEdit->toPlainText().length();
       serial->write(text_count.data());
    

    }

    Thanks
    Pawan pal


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.