Important: Please read the Qt Code of Conduct -

Disappearing QQuickWidget Image -- Needs Constant Repainting

  • I am using the following source code in my MainWindow::MainWindow() function to load some QML on a QQuickWidget:


    The trouble I'm having is that this widget is on a tab, and when I click to view the other tab and then click back, the data is gone. The solution I came up with was to throw this in MainWindow::paintEvent(). However, that seems messy because it will keep repainting that constantly with every mouse move.

    How do I keep from having to repaint this widget all the time?

  • Lifetime Qt Champion

    @maximo said:
    Hi, what is inside
    my.qml ?

  • import QtQuick 2.0
    import "."
    import "QChart.js"        as Charts
    import "QChartGallery.js" as ChartsData
    Chart {
      id: chart_bar;
      width: 300;
      height: 300;
      chartAnimated: false;
      chartData: ChartsData.ChartBarData;
      chartType: Charts.ChartType.BAR;

  • Lifetime Qt Champion

    tried to put it in "Qt Quick Widgets Example"
    but it just crashes - so cant say if I can reproduce it.

    However, could you try the sample and see if its only drawn once?
    In the sample it keeps spinning.

    import QtQuick 2.0
    Rectangle {
        id: root
        Rectangle {
            property int d: 100
            id: square
            width: d
            height: d
            anchors.centerIn: parent
            color: "red"
            NumberAnimation on rotation { from: 0; to: 360; duration: 2000; loops: Animation.Infinite; }
        Text {
            anchors.centerIn: parent
            text: "Qt Quick running in a widget"

  • @mrjj I tried your example. Indeed, it stayed on the widget and kept spinning, even when I changed tabs. So, it appears the issue is in how this third-party library I'm using works. I'm using Julien Wintz's QChart.js API.

    Here's how I did a workaround for now:

    1. When I do a w.setSource(sURL), I also do a w.setProperty("chart",sURL) to the same value.
    2. I do w.installEventFilter(this) on the widget.
    3. I then have a function like so:
    bool MainWindow::eventFilter(QObject *obj, QEvent *event)
        QString sChart = obj->property("chart").toString();
        if (sChart.length() > 0) {
            if (event->type() == QPaintEvent::Paint) {
                QString s = obj->property("chart").toString();
                QQuickWidget *w = this->findChild<QQuickWidget *>(obj->objectName());

    I assume that this is a little lighter on the CPU than intercepting the entire MainWindow's paintEvent().

  • Lifetime Qt Champion

    Hi. ok. so for some reason the chart only paint once.
    You could ask in Quick forum if they have an idea as they are very into Quick. Im pretty sure
    its easy to fix in the QML.

    But nice workaround :)

    Might also have used
    QTabWidget::currentChanged(int index) (its a signal)
    And then setSource(sChart); when switching to that tab.

  • @mrjj I liked your workaround better, so I implemented that instead. It was even less CPU intensive than the last workaround I was using.

  • Lifetime Qt Champion

    Ok :)
    I think to get it to repaint itself is easy for Quick experts
    but if this works for you, then its ok, i guess.