Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QT QChart can't show,refresh doesn't work
Forum Updated to NodeBB v4.3 + New Features

QT QChart can't show,refresh doesn't work

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 1.5k Views 1 Watching
  • 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.
  • JonBJ JonB

    @printfne
    I suggest you look at my code at https://forum.qt.io/topic/145986/qchart-line-series-not-shown/3 and read through that thread where much the same about "not being to able to see updates" was asked. I have not needed any repaint()s or update()s. As per there, you do have to sort out the axes yourself, I suspect that might be your issue here?

    P Offline
    P Offline
    printfne
    wrote on last edited by
    #3

    @JonB I think, I found a minimal demo:

        auto chart = new QChart();
        auto series = new QBarSeries();
        auto view = new QChartView();
        setCentralWidget(view);
    
        auto bar = new QBarSet("");
        *bar << 1 << 2;
        series->append(bar);
        chart->addSeries(series);
        view->setChart(chart);
    

    it's perfectly normal.But when I move *bar << 1 << 2; to any position after chart->addSeries(series);, the bars of the histogram are no longer displayed, like the screenshot of the main window given in the original question.

    Pl45m4P JonBJ 2 Replies Last reply
    0
    • P printfne

      @JonB I think, I found a minimal demo:

          auto chart = new QChart();
          auto series = new QBarSeries();
          auto view = new QChartView();
          setCentralWidget(view);
      
          auto bar = new QBarSet("");
          *bar << 1 << 2;
          series->append(bar);
          chart->addSeries(series);
          view->setChart(chart);
      

      it's perfectly normal.But when I move *bar << 1 << 2; to any position after chart->addSeries(series);, the bars of the histogram are no longer displayed, like the screenshot of the main window given in the original question.

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #4

      @printfne

      I haven't used QChart that much, but I think you need to set the series after you add the points. Therefore you cant modify just the series when it has been drawn already.
      [ok, judging from the example, you dont need to, but you need to set the axis to match your data]
      You could search for "dynamic qchart series" or something

      Edit:

      There is even a Qt Example

      • https://doc.qt.io/qt-6/qtcharts-dynamicspline-example.html

      wth... this example has no code available in 6.5?!
      There is a page in the example section but it seems that the code got removed?!

      The code is available in Qt5.15

      • https://code.qt.io/cgit/qt/qtcharts.git/tree/examples/charts/dynamicspline?h=5.15

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      P 1 Reply Last reply
      1
      • P printfne

        @JonB I think, I found a minimal demo:

            auto chart = new QChart();
            auto series = new QBarSeries();
            auto view = new QChartView();
            setCentralWidget(view);
        
            auto bar = new QBarSet("");
            *bar << 1 << 2;
            series->append(bar);
            chart->addSeries(series);
            view->setChart(chart);
        

        it's perfectly normal.But when I move *bar << 1 << 2; to any position after chart->addSeries(series);, the bars of the histogram are no longer displayed, like the screenshot of the main window given in the original question.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #5

        @printfne
        Yes, I believe I answered that in the other question. You need to adjust axes when you add points after the series is on the chart.

        1 Reply Last reply
        1
        • Pl45m4P Pl45m4

          @printfne

          I haven't used QChart that much, but I think you need to set the series after you add the points. Therefore you cant modify just the series when it has been drawn already.
          [ok, judging from the example, you dont need to, but you need to set the axis to match your data]
          You could search for "dynamic qchart series" or something

          Edit:

          There is even a Qt Example

          • https://doc.qt.io/qt-6/qtcharts-dynamicspline-example.html

          wth... this example has no code available in 6.5?!
          There is a page in the example section but it seems that the code got removed?!

          The code is available in Qt5.15

          • https://code.qt.io/cgit/qt/qtcharts.git/tree/examples/charts/dynamicspline?h=5.15
          P Offline
          P Offline
          printfne
          wrote on last edited by printfne
          #6

          @Pl45m4 @JonB
          I modified the axis to follow Chart::handleTimeout in example https://code.qt.io/cgit/qt/qtcharts.git/tree/examples/charts/dynamicspline/chart.cpp?h=5.15 , but it seems to have no effect on the display of the histogram. Also I noticed that in the example the scroll is called after adding a point, but I only added the barset once at the beginning, I don't know if that would have an effect.
          Here is the modified code:

              auto view = new QChartView();
              auto chart = new QChart();
              auto series = new QBarSeries();
              auto bar = new QBarSet("");
              setCentralWidget(view);
          
              view->setChart(chart);
              chart->addSeries(series);
              series->append(bar);
          
              QValueAxis *m_axisX = new QValueAxis();
              QValueAxis *m_axisY = new QValueAxis();
              chart->addAxis(m_axisX,Qt::AlignBottom);
              chart->addAxis(m_axisY,Qt::AlignLeft);
          
              *bar << 1 << 2 << 3;
          
              m_axisX->setTickCount(5);
              m_axisX->setRange(0, 100);
              m_axisY->setRange(0, 100);
          
              qreal x = chart->plotArea().width() / m_axisX->tickCount();
              qreal y = (m_axisX->max() - m_axisX->min()) / m_axisX->tickCount();
              chart->scroll(x, 0);
          

          This is the effect after running:
          7c7526af-fab3-47a7-a0e2-2ddb551886ab-图片.png
          It is worth mentioning that I need to frequently modify the content displayed by the bar later, so in this demo I write the output of the bar later.

          JonBJ 1 Reply Last reply
          0
          • P printfne

            @Pl45m4 @JonB
            I modified the axis to follow Chart::handleTimeout in example https://code.qt.io/cgit/qt/qtcharts.git/tree/examples/charts/dynamicspline/chart.cpp?h=5.15 , but it seems to have no effect on the display of the histogram. Also I noticed that in the example the scroll is called after adding a point, but I only added the barset once at the beginning, I don't know if that would have an effect.
            Here is the modified code:

                auto view = new QChartView();
                auto chart = new QChart();
                auto series = new QBarSeries();
                auto bar = new QBarSet("");
                setCentralWidget(view);
            
                view->setChart(chart);
                chart->addSeries(series);
                series->append(bar);
            
                QValueAxis *m_axisX = new QValueAxis();
                QValueAxis *m_axisY = new QValueAxis();
                chart->addAxis(m_axisX,Qt::AlignBottom);
                chart->addAxis(m_axisY,Qt::AlignLeft);
            
                *bar << 1 << 2 << 3;
            
                m_axisX->setTickCount(5);
                m_axisX->setRange(0, 100);
                m_axisY->setRange(0, 100);
            
                qreal x = chart->plotArea().width() / m_axisX->tickCount();
                qreal y = (m_axisX->max() - m_axisX->min()) / m_axisX->tickCount();
                chart->scroll(x, 0);
            

            This is the effect after running:
            7c7526af-fab3-47a7-a0e2-2ddb551886ab-图片.png
            It is worth mentioning that I need to frequently modify the content displayed by the bar later, so in this demo I write the output of the bar later.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #7

            @printfne
            Start by altering order to:

            *bar << 10 << 20 << 30;
            series->append(bar);
            chart->addSeries(series);
            

            Do you see anything now?

            P 1 Reply Last reply
            0
            • JonBJ JonB

              @printfne
              Start by altering order to:

              *bar << 10 << 20 << 30;
              series->append(bar);
              chart->addSeries(series);
              

              Do you see anything now?

              P Offline
              P Offline
              printfne
              wrote on last edited by
              #8

              @JonB Oh! I saw the form, but it's so weird, first of all, the code in form series->append(bar);chart->addSeries(series);*bar << 1 << 2 << 3;*bar << 20 << 5 << 30; doesn't work properly, but the form *bar << 1 << 2 << 3; series->append(bar); chart->addSeries(series); *bar << 20 << 5 << 30; does, I think it's weird. Secondly, the display of this chart looks so strange, I found that the displayed content is related to m_axisX->setTickCount(1); and qreal x = chart->plotArea().width() / m_axisX->tickCount();chart->scroll(x, 0);, but what confuses me is that it seems that I should not manually assign a value to chart->scroll(x, 0), Because I can't seem to calculate how much I should scroll.
              The complete code is as follows:

                  auto view = new QChartView();
                  auto chart = new QChart();
                  auto series = new QBarSeries();
                  auto bar = new QBarSet("");
                  setCentralWidget(view);
              
                  view->setChart(chart);
              
                  QValueAxis *m_axisX = new QValueAxis();
                  QValueAxis *m_axisY = new QValueAxis();
                  chart->addAxis(m_axisX,Qt::AlignBottom);
                  chart->addAxis(m_axisY,Qt::AlignLeft);
              
                  *bar << 1 << 2 << 3;
                  series->append(bar);
                  chart->addSeries(series);
                  *bar << 20 << 5 << 30;
              
                  m_axisX->setTickCount(1);
                  m_axisX->setRange(-500, 1000);
                  m_axisY->setRange(0, 10000);
              
                  qreal x = chart->plotArea().width() / m_axisX->tickCount();
                  chart->scroll(x, 0);
              

              The running effect is as follows:
              f914b063-5fde-4ed5-bbdf-1fc1320ebbfc-图片.png

              JonBJ 1 Reply Last reply
              0
              • P printfne

                @JonB Oh! I saw the form, but it's so weird, first of all, the code in form series->append(bar);chart->addSeries(series);*bar << 1 << 2 << 3;*bar << 20 << 5 << 30; doesn't work properly, but the form *bar << 1 << 2 << 3; series->append(bar); chart->addSeries(series); *bar << 20 << 5 << 30; does, I think it's weird. Secondly, the display of this chart looks so strange, I found that the displayed content is related to m_axisX->setTickCount(1); and qreal x = chart->plotArea().width() / m_axisX->tickCount();chart->scroll(x, 0);, but what confuses me is that it seems that I should not manually assign a value to chart->scroll(x, 0), Because I can't seem to calculate how much I should scroll.
                The complete code is as follows:

                    auto view = new QChartView();
                    auto chart = new QChart();
                    auto series = new QBarSeries();
                    auto bar = new QBarSet("");
                    setCentralWidget(view);
                
                    view->setChart(chart);
                
                    QValueAxis *m_axisX = new QValueAxis();
                    QValueAxis *m_axisY = new QValueAxis();
                    chart->addAxis(m_axisX,Qt::AlignBottom);
                    chart->addAxis(m_axisY,Qt::AlignLeft);
                
                    *bar << 1 << 2 << 3;
                    series->append(bar);
                    chart->addSeries(series);
                    *bar << 20 << 5 << 30;
                
                    m_axisX->setTickCount(1);
                    m_axisX->setRange(-500, 1000);
                    m_axisY->setRange(0, 10000);
                
                    qreal x = chart->plotArea().width() / m_axisX->tickCount();
                    chart->scroll(x, 0);
                

                The running effect is as follows:
                f914b063-5fde-4ed5-bbdf-1fc1320ebbfc-图片.png

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #9

                @printfne
                My code does not use any of tick counts, plat area sizes or scrolling. So that is specific to the example you copied from. If you stick with what you have you will have to play with the values if you want all 4 bars to have the same visible width as each other.

                P 1 Reply Last reply
                0
                • JonBJ JonB

                  @printfne
                  My code does not use any of tick counts, plat area sizes or scrolling. So that is specific to the example you copied from. If you stick with what you have you will have to play with the values if you want all 4 bars to have the same visible width as each other.

                  P Offline
                  P Offline
                  printfne
                  wrote on last edited by
                  #10

                  @JonB That is to say, I have to manually specify the range of chart->scroll?

                  Also I have another question, in the above code, I used the code *bar << 1 << 2 << 3;series->append(bar);chart->addSeries(series);*bar< < 20 << 5 << 30;, he can work normally, let’s call it code A, and use the code series->append(bar);chart->addSeries(series);*bar << 1 << 2 << 3;*bar << 20 << 5 << 30;, it cannot be displayed normally, let’s call it code B for now. Here code A can run normally, that is to say, after series-append and chart->addSeries The code *bar << num1 << num2 can be displayed, but in code B, this is not displayed, what is the reason What caused it?

                  JonBJ 1 Reply Last reply
                  0
                  • P printfne

                    @JonB That is to say, I have to manually specify the range of chart->scroll?

                    Also I have another question, in the above code, I used the code *bar << 1 << 2 << 3;series->append(bar);chart->addSeries(series);*bar< < 20 << 5 << 30;, he can work normally, let’s call it code A, and use the code series->append(bar);chart->addSeries(series);*bar << 1 << 2 << 3;*bar << 20 << 5 << 30;, it cannot be displayed normally, let’s call it code B for now. Here code A can run normally, that is to say, after series-append and chart->addSeries The code *bar << num1 << num2 can be displayed, but in code B, this is not displayed, what is the reason What caused it?

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #11

                    @printfne
                    My understanding is the same as I keep saying. When you add new points to a chart after it has been shown, it's not that QChartView doesn't update, it's that its current axes --- which were set up when the initial points were added before being added to the chart --- are out of range for the new point. In some shape or form, you need to rescale the axes horizontally and/or vertically so that they include the new point(s) within their range.

                    Keep your horizontal & vertical axes visible at least while you develop. If you do not see a new point that you added, look to see whether it is outside the current horizontal/vertical ranges. That is what my code was doing and it displays the new points in real time without my having to write any extra "updating/repainting" calls.

                    P 1 Reply Last reply
                    1
                    • JonBJ JonB

                      @printfne
                      My understanding is the same as I keep saying. When you add new points to a chart after it has been shown, it's not that QChartView doesn't update, it's that its current axes --- which were set up when the initial points were added before being added to the chart --- are out of range for the new point. In some shape or form, you need to rescale the axes horizontally and/or vertically so that they include the new point(s) within their range.

                      Keep your horizontal & vertical axes visible at least while you develop. If you do not see a new point that you added, look to see whether it is outside the current horizontal/vertical ranges. That is what my code was doing and it displays the new points in real time without my having to write any extra "updating/repainting" calls.

                      P Offline
                      P Offline
                      printfne
                      wrote on last edited by
                      #12

                      @JonB yes you are correct i solved the problem thanks a lot.

                      1 Reply Last reply
                      0
                      • P printfne has marked this topic as solved on
                      • P printfne has marked this topic as unsolved on
                      • P printfne has marked this topic as solved on

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved