Results 1 to 3 of 3

Thread: [solved] Understanding QML / C++ Event Loops

  1. #1
    Join Date
    Oct 2009
    Posts
    33
    Thanks
    2

    Default [solved] Understanding QML / C++ Event Loops

    Hi,

    I'm trying to load a C++ object from qml, but it freezes the UI. Here's what I tried/understood:

    Method One: I have a simple qml button, and when I click it, I directly invoke the C++ object's load slot. Loading takes a couple of seconds, during which the UI is frozen. It should be frozen; the thread that's running the UI is now processing my C++ load function.

    Method Two: I have a simple qml button, and when I click it, I directly invoke the C++ object's special placeholder function. All this function does is emit a signal from the C++ object, which is connected to the C++ object's Load slot. Again, the UI freezes for a couple of seconds. I thought that the C++ object lies in a different thread, the UI thread should only emit the signal, but this doesn't happen... the UI thread executes the Load slot too, since it freezes up for the same time as Method One.

    Method Three: Tried to set it up so that the QML emits a signal to the C++ object instead of invoking a C++ object slot directly. Problem is that my QML Element lies inside a QML Loader{} from another QML file. I tried, but I couldn't use QObject's findChild() method to find the specific object I need to receive the signal from.

    When I check the running process with gdb, I see that two threads are started up. I thought one thread was for the QML view, and the other for the rest of the app. Is this wrong? Do I need a third thread?

    edit: Adding a thread solved this problem.

    main.cpp: contains my declarative view and main object
    I throw my main object in another thread, and everything works as it should
    Last edited by kachofool; 20th March 2011 at 21:48.

  2. #2
    Join Date
    Jan 2010
    Posts
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: [solved] Understanding QML / C++ Event Loops

    Hi,

    I am also facing this same problem.. can u please show some code how did u solved ur problem. ie how did you placed ur object into another thread .. plz help me on this issue.

    Thanks with Regards,
    Vishal

  3. #3
    Join Date
    Oct 2009
    Posts
    33
    Thanks
    2

    Default Re: [solved] Understanding QML / C++ Event Loops

    Hi Vishal,

    I don't currently have any working code I can show you, but I can explain what I did and it should be straightforward to implement the code yourself. The code you have that's freezing up the UI should be implemented in a separate worker thread. Subclass the QThread class for this. My QThread class doesn't do anything special. I just reimplemented the run() function as follows:

    Qt Code:
    1. void MyThread::run()
    2. { exec(); }
    To copy to clipboard, switch view to plain text mode 

    You can then use QObject's moveToThread function to have that object's events processed in another thread (the one you created earlier).

    Qt Code:
    1. MyThread* myBusyThread = new MyThread;
    2. customQObject* myBusyObject = new customQObject;
    3. myBusyObject->moveToThread(myBusyThread);
    4. myBusyThread->start();
    To copy to clipboard, switch view to plain text mode 

    Invoke the methods that cause your GUI to normally freeze up (the heavy processing ones) using signals and slots and the processing for that object should be done in a separate thread, leaving the UI responsive.

Similar Threads

  1. Replies: 10
    Last Post: 15th January 2010, 14:35
  2. Program loops at one line
    By Platoon in forum Newbie
    Replies: 7
    Last Post: 3rd October 2009, 16:02
  3. Combining Event Loops
    By Gimpyfuzznut in forum Qt Programming
    Replies: 5
    Last Post: 10th July 2009, 06:08
  4. for loops in c++
    By baray98 in forum General Programming
    Replies: 16
    Last Post: 19th January 2009, 14:09
  5. QApplication and widgets' loops
    By Placido Currò in forum Qt Programming
    Replies: 7
    Last Post: 31st January 2008, 12:13

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.