I am converting a QT4 project to QT6 and ran into the following issue, the original code uses Desktop among other things, which was deprecated in QT6, and I have been trying to follow the suggested replacements.
Some of these are ‘simple’ replacements, such as setMargin having become setContentsMargins with 4 parameters instead of one, but the desktop ones have me stumped.
The original QT4 code is as follows :
if (QApplication::desktop()->isVirtualDesktop())
scr = QApplication::desktop()->screenNumber(pos);
else
scr = QApplication::desktop()->screenNumber(this);
setParent(QApplication::desktop()->screen(scr));
desktop’s suggested replacement is to use ‘PrimaryScreen’ instead, and the isVirtual is gone entirely. I am not sure if the Virtual check is even required?
I have attempted to replace the above with :
setParent(QGuiApplication::screenAt(pos));
which gives me the following error :
No known conversion from QScreen* to QWidget*
I have tried a static cast to QWidget, which didn’t work, and I seem entirely unable to replicate the original intent behind the desktop() usage.
Elsewhere in the project; I have the similar issue with the original code being :
QWidget(qApp->desktop, FLAGS)
And the replacement code giving the same casting error
QWidget(qApp->primaryScreen(), FLAGS)
I am not entirely sure where I am going wrong…
The original code is a little bit fishy. It looks like it tried to set QDesktopWidget as a parent, but QDesktopWidget wasn’t really a widget, it broke LSP with its inheritance. Originally, in Qt3 and earlier, it was really intended to be a widget that takes control of system desktop, allowing painting on it, but even then it worked only in X11, and even that was unstable. That’s why they replaced it with QScreen: it performs all work with screen geometries and numbers, but doesn’t pretend to be a paintable Widget.
So you’d better replace QApplication::desktop()->screen()
with some real Widget, preferably this
if you construct a widget in another widget or your main window if you do it in main
. Elsewhere, you have QApplication::activeWindow
and other activeSomething functions, allWidgets
and topLevelWidgets
, etc. Or, of course, you can set parent to nullptr
and handle memory management yourself, with smart pointers, aggregation or automatic object.