I got the following solution
connect(qGuiApp, &QGuiApplication::focusWindowChanged, this, [this](QWindow* focusWindow) {
if (!focusWindow) {
return;
}
auto windowList = QGuiApplication::allWindows();
windowList.removeIf([focusWindow](QWindow* w) {
return !w->isVisible() || w->transientParent() || w == focusWindow;
});
// Attempt to restore windows in the order in which they were opened.
std::sort(windowList.begin(), windowList.end(), [](QWindow* l, QWindow* r) {
return l->property(FOCUS_TIME_PROPERTY).value<qint64>() <
r->property(FOCUS_TIME_PROPERTY).value<qint64>();
});
for (auto window : windowList) {
qDebug() << "Raise" << window->title();
window->raise();
}
focusWindow->raise();
focusWindow->setProperty(FOCUS_TIME_PROPERTY, QDateTime::currentMSecsSinceEpoch());
});