Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Disappearing QQuickWidget Image -- Needs Constant Repainting

    General and Desktop
    qquickwidget qml repaint qchart.js qchart
    2
    8
    2181
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      maximo last edited by maximo

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

      ui->myQuickWidget->setSource("my.qml");
      

      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?

      1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

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

        1 Reply Last reply Reply Quote 0
        • M
          maximo last edited by

          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;
          }
          
          1 Reply Last reply Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion last edited by

            hi
            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"
                }
            }
            
            M 1 Reply Last reply Reply Quote 1
            • M
              maximo @mrjj last edited by

              @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());
                          w->setSource(sChart);
                      }
                  }
              }
              

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

              mrjj 1 Reply Last reply Reply Quote 0
              • mrjj
                mrjj Lifetime Qt Champion @maximo last edited by mrjj

                @maximo
                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.

                M 1 Reply Last reply Reply Quote 0
                • M
                  maximo @mrjj last edited by

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

                  mrjj 1 Reply Last reply Reply Quote 0
                  • mrjj
                    mrjj Lifetime Qt Champion @maximo last edited by

                    @maximo
                    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.

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post