Qt6 topics merged with General

Draw in QDesktopWidget (Desktop-Background)

  • Hallo Qt Community,

    I'm trying to port an old Qt3 open-source application to Qt5. It is an application that overdraws the desktop background with a bitmap.
    It is an application that overdraws the desktop background. Unfortunately nothing is drawn. Even if I just give the widget a different color. Nothing to see.

    the main:

    int main(int argc, char** argv)
        MyApp app(argc, argv);
        return app.exec();

    and MyApp:

    MyApp::MyApp(int &argc, char **argv)
        : QApplication(argc, argv)
    void MyApp::init()
        timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), this, SLOT(recalc()));
        QTimer::singleShot(1, this, SLOT(recalc()));
        timer->start(delay * 1000);
    void MyApp::recalc()
            QRect  screenGeometry = desktop()->geometry();
            const int height = screenGeometry.height();
            const int width = screenGeometry.width();
            qInfo() << "Desktop geometry height: " << height << " width: " << width;
            QPalette palette;
            palette.setBrush(desktop()->backgroundRole(),  QBrush(*(getMyQImage())));
            //XClearWindow(QX11Info::display(), QX11Info::appRootWindow());

    Everything works fine. recalc() is always called, but unfortunately nothing is drawn.

    Do I have a thank you error somewhere? Target system is Linux. Did I miss something?

    Thanks for all your help.

  • Lifetime Qt Champion

    What is desktop() ? a QDesktopWidget ?

    Does it have a paintEvent and does it do anything special to
    " overdraws the desktop background" compared to just being a normal
    (borderless) widget that is working as an overlay ?

    Also in qt3 i believe you could do something like

       QPainter paint( QApplication::desktop()->screen( 0 ), true );

    But not sure that will actually still work even if it compiles.

  • @mrjj Thanks for your quick reply, as you can see below, yes desktop() the static function from QApplication

    QDesktopWidget *QApplication::desktop()
    Returns the desktop widget (also called the root window).
    The desktop may be composed of multiple screens, so it would be incorrect, for example, to attempt to center some widget in the desktop's geometry. QDesktopWidget has various functions for obtaining useful geometries upon the desktop, such as QDesktopWidget::screenGeometry() and QDesktopWidget::availableGeometry().
    On X11, it is also possible to draw on the desktop.

    So I thought it would be a good place for drawn something. "On X11, it is also possible to draw on the desktop."

  • Lifetime Qt Champion

    Ok so you subclassed QDesktopWidget ?

    Hmm yes, . "On X11, it is also possible to draw on the desktop." does sounds like it works on linux.

    Did you try to give it a paintevent ?

  • Okay, perhaps I was misunderstood. It's no longer an Qt3 application! So QPainter is not an option in Q5:
    QWidget::paintEngine: Should no longer be called.

    "Ok so you subclassed QDesktopWidget ?" You can see in my first code-block, that's all. I inherit from "QApplication", get the desktop and try to draw inside like in the code above. This isn't working, any idea? I need something to cover the desktop background. I thought the QDesktopWidget from QApplication was just the thing, unfortunately no.

  • Lifetime Qt Champion

    Well you may only use QPainter in the PaintEvent function unless drawing to an image.
    So thats is most likely the reason for that error.

    Im not really sure how one would draw with it. Let me test and see if i can get a result.

  • Thanks for your efforts. This post describes what I want to do but I looks outdated for me:


    Looking in https://doc.qt.io/qt-5/qx11info.html#appRootWindow it referents to https://doc.qt.io/qt-5/qapplication.html#desktop which sounds like "QApplication::desktop()" should give me the root widget (desktop).

  • I thought it should work, but it doesn't:

    auto d = QWidget::createWindowContainer(QWindow::fromWinId(QX11Info::appRootWindow())); 

    but it only creates a new Window and not draw on the X11 desktop. I found an smeller post here:


  • Lifetime Qt Champion

    I tested various things i found on google and nothing did draw on the desktop. ( linux mint, Xfce )
    I wonder if docs are wrong or it is indeed still possible.

  • I come to the same conclusion. Should I create a ticket?

  • Lifetime Qt Champion

    Yes that would be a good idea. At least we need to change the docs if its really possible anymore
    or there are requirements to the windows manager etc.
    Its best if you include a sample so its easy to test for the developers.

  • Lifetime Qt Champion

    QDesktopWidget is deprecated - don't know if it's worth the trouble.

  • Lifetime Qt Champion


    Oh Thanks, i missed "This class is obsolete." in the docs.
    I agree it's unlikely it will be looked at.

    Also, so far none of the method one can google worked so it might not be possible anymore with
    Qt alone.

Log in to reply