Be Careful With Signal Slots - GSoC'22 post #9

⌂ home      ⇧ one level up
Sat Jul 23, 2022 · 298 words · 2 min

Here's something interesting thing I stubled across today. (more like, struggled with for a day and half) This might be something obvious to regular Qt programmers, but I'm not a professional.

In my last post I told how Tobias helped me get cacheSpaceHierarchy() function working as expected. The fix was easy. That doesn't mean I'll take half a minute to code and commit.

cacheSpaceHierarchy() was being called correctly as expected. But now this function stopped functioning. It worked until two days back when it was being called at the wrong time. But today when I call it at the right place, it decides it won't work.

Whats funny to me is that the reason behind this malfunction was similar to the problem last week.

I used the following snipped to wait for a room to load and then populate its room list if its a space.

connect(neoChatRoom, &Room::baseStateLoaded, neoChatRoom, [this, neoChatRoom, connection]() {
    if (neoChatRoom->isSpace()) {
        this->populateSpaceHierarchy(connection, neoChatRoom->id(), false);
    }
});

The issue, you ask? &Room::baseStateLoaded signal is already been fired before this connection is made and as a result, the inner block is never executed.

Solution?

Just check if relevant data is loaded. If yes, use that data. If not, then set up the connection and wait for the data to be loaded.

if (neoChatRoom->isSpace()) {
    this->populateSpaceHierarchy(connection, neoChatRoom->id(), false);
} else {
    connect(neoChatRoom, &Room::baseStateLoaded, neoChatRoom, [this, neoChatRoom, connection]() {
        if (neoChatRoom->isSpace()) {
            this->populateSpaceHierarchy(connection, neoChatRoom->id(), false);
        }
    });
}

This works, because neoChatRoom->isSpace() can be true only if room's base state is loaded.

There is one potential issue though. This will set up a good number of unused connections. I'll have to discuss it with my mentors and get their views if there's a better way to accomplish this goal.


hire me! · blog · about · resume · github · kde invent · endeavour · email · home

🪻🌼