Search code examples
c++performanceqtparent

Qt Program runs slow if object parent is the window


I got a strange behaviour in Qt and I would like to know why thing are happening this way, if someone can clarify this to me it will be most welcome.

I have a object that belongs to the mainWindow, when I construct this object in the mainWindow's initialization list, I send 0 as the parent and the program runs fast, as it should.

BUT if I pass this in the constructor, making the main window the parent of the object, the program runs incredibly slow!

I mean, the GUI runs slow, the buttons take time to respond. There is NOTHING being processed or executed, since processing just start when I click something, and still the buttons take time to become pressed or highlighted.

One thing to add, in the mainWindow's constructor the object is moved to another thread. When I trigger the object processing routine, the GUI comes back to life...not as fast as it was when parent was 0, but a lot faster.

Does anyone know why's this is happening? I am not using the parent to do anything, so this must be Qt's inner workings.

I would post my code in here, but it's huge and I have no idea which part is related to this.


Solution

  • You shouldn't be able to move objects to a thread if they have a parent. The docs: The object cannot be moved if it has a parent.

    You should also get a warning message if you try to do so. If you look at the source code of QObject::moveToThread you will find the following lines:

    if (d->parent != 0) {
            qWarning("QObject::moveToThread: Cannot move objects with a parent");
            return;
        }