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 crashes when adding series to plot

Qt crashes when adding series to plot

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 2.6k 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.
  • V Offline
    V Offline
    Vera
    wrote on last edited by
    #1

    Hello,

    I have a chart where I want to put in a QLineSeries, but every time it wants to add them the app crashes. My code looks like this:
    this is the creation of the plot, the I send it to another function where i will fill it.

        QChart *Dopplerplot = new QChart();
        QLineSeries *DSeries = new QLineSeries;
        Dopplerplot->addSeries(DSeries);
        QValueAxis *axisX = new QValueAxis;
        axisX->setRange(0, 2000);
        axisX->setLabelFormat("%g");
        axisX->setTitleText("Sample");
        QValueAxis *axisY = new QValueAxis;
        axisY->setRange(-1, 1);
        axisY->setTitleText("Speed");
        Dopplerplot->setAxisX(axisX, DSeries);
        Dopplerplot->setAxisY(axisY, DSeries);
        Dopplerplot->legend()->hide();
        Dopplerplot->setTitle("Doppler (m/s)");
    

    here I fill the lineseries with data I received:

        QScatterSeries *series = new QScatterSeries();
        doppler = 0;
        Dopplerplot->removeAllSeries();
        Dplot_y += 5;
        for(uint i = 0; i < ob.size(); ++i)
        {
            series->append(QPointF(ob.at(i).x, ob.at(i).y));
            doppler += ob.at(i).doppler;
        }
        if(doppler == 0)
        {
            doppler = 0.1;
        }
        else
        {
            doppler = doppler/ob.size();
        }
        DSeries->append(QPointF(Dplot_y, doppler));
        XZplot->removeAllSeries();
        XZplot->addSeries(series);
    

    And I have a timer running every second to draw the plot, this is to try when I slowed it down it would draw it but didn't help.

    void Radar::updateData()
    {
        Dopplerplot->addSeries(DSeries);
    }
    

    when I remove the addSeries line the app doesn't crash, I haven't found out where it crashes so I hope someone here could help me out.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      Vera
      wrote on last edited by
      #10

      Hello,

      I got it working finally!
      I had to send the series to my radar class instead of the charts.
      So I had it like this before, where I send the charts over:

      Radar *radar = new Radar(XZplot, Dopplerplot);
      

      and now I did it like this, where I send the series over:

      Radar *radar = new Radar(series, DSeries);
      

      And now it plots perfectly, I just have to append new points to the series and it plots.

      Thanks all for your help!

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        You should run your application through the debugger and get the stack trace to see where it fails exactly.

        By the way, which version of Qt are you using ? On which OS ? With which compiler ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • V Offline
          V Offline
          Vera
          wrote on last edited by
          #3

          Hello,

          I am using Qt version 5.10 on a windows 10 pc with the mingw compiler.
          I did some debugging and it gives a segmentation fault with a qvector in this code it ends in the qvector.h file.

          template <typename T>
          void QVector<T>::append(const T &t)
          {
              const bool isTooSmall = uint(d->size + 1) > d->alloc;
              if (!isDetached() || isTooSmall) {
                  T copy(t);
                  QArrayData::AllocationOptions opt(isTooSmall ? QArrayData::Grow : QArrayData::Default);
                  reallocData(d->size, isTooSmall ? d->size + 1 : d->alloc, opt);
          
                  if (QTypeInfo<T>::isComplex)
                      new (d->end()) T(qMove(copy));
                  else
                      *d->end() = qMove(copy);
          
              } else {
                  if (QTypeInfo<T>::isComplex)
                      new (d->end()) T(t);
                  else
                      *d->end() = t;
              }
              ++d->size;
          }
          

          does this mean that my vector is to small or that the values inside my vector are to small? the data in the point in the vector where it crashes it says 30.0, 0.1

          1 Reply Last reply
          0
          • aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Hi @Vera,

            more interesting would be, how this append is called from your code. Are you using the vector with a pointer for example? Is the pointer valid?

            Qt has to stay free or it will die.

            1 Reply Last reply
            2
            • V Offline
              V Offline
              Vera
              wrote on last edited by
              #5

              hello @aha_1980,

              I found what was causing the crash it was the Dopplerplot->removeAllSeries(). That kinda makes sense but also not, the first time it does this there are no series on the plot but that shouldn't make it crash as it doesn't for my other plot.
              Now that it doesn't crash anymore it still doesn't plot any points to the plot. So maybe it is a bad pointer. I send the dopplerplot which I created in my main to my program like this:

              Radar *radar = new Radar(XZplot, Dopplerplot);
              

              And then in the radar program I do it like this:

              Radar::Radar(QChart *XZ, QChart *Dplot)
                  :XZplot(XZ), Dopplerplot(Dplot)
              {
                  dataport = new QSerialPort();
                  cfgport = new QSerialPort();
                  DSeries = new QLineSeries();
                  MagicWord.append("0201040306050807");
                  Dplot_y = 0;
                  timer = new QTimer(this);
                  connect(timer, SIGNAL(timeout()), this, SLOT(updateData()));
                  connect(dataport, &QSerialPort::readyRead, this, &Radar::readData);
                  Set_serial();
              }
              

              and XZplot and Dopplerplot are made in the hpp file like this:

                  QChart *XZplot;
                  QChart *Dopplerplot;
              

              Then I use them both in the draw_objects() function, do you see anything wrong in my code? Or do you need some more info?

              jsulmJ 1 Reply Last reply
              0
              • V Vera

                hello @aha_1980,

                I found what was causing the crash it was the Dopplerplot->removeAllSeries(). That kinda makes sense but also not, the first time it does this there are no series on the plot but that shouldn't make it crash as it doesn't for my other plot.
                Now that it doesn't crash anymore it still doesn't plot any points to the plot. So maybe it is a bad pointer. I send the dopplerplot which I created in my main to my program like this:

                Radar *radar = new Radar(XZplot, Dopplerplot);
                

                And then in the radar program I do it like this:

                Radar::Radar(QChart *XZ, QChart *Dplot)
                    :XZplot(XZ), Dopplerplot(Dplot)
                {
                    dataport = new QSerialPort();
                    cfgport = new QSerialPort();
                    DSeries = new QLineSeries();
                    MagicWord.append("0201040306050807");
                    Dplot_y = 0;
                    timer = new QTimer(this);
                    connect(timer, SIGNAL(timeout()), this, SLOT(updateData()));
                    connect(dataport, &QSerialPort::readyRead, this, &Radar::readData);
                    Set_serial();
                }
                

                and XZplot and Dopplerplot are made in the hpp file like this:

                    QChart *XZplot;
                    QChart *Dopplerplot;
                

                Then I use them both in the draw_objects() function, do you see anything wrong in my code? Or do you need some more info?

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @Vera said in Qt crashes when adding series to plot:

                Radar *radar = new Radar(XZplot, Dopplerplot);

                is Dopplerplot a valid pointer at that time?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                V 1 Reply Last reply
                2
                • jsulmJ jsulm

                  @Vera said in Qt crashes when adding series to plot:

                  Radar *radar = new Radar(XZplot, Dopplerplot);

                  is Dopplerplot a valid pointer at that time?

                  V Offline
                  V Offline
                  Vera
                  wrote on last edited by
                  #7

                  @jsulm
                  How can I check if it is a valid pointer?
                  In my first post you can see how I created Dopplerplot, don't know if that helps.

                  jsulmJ 1 Reply Last reply
                  0
                  • V Vera

                    @jsulm
                    How can I check if it is a valid pointer?
                    In my first post you can see how I created Dopplerplot, don't know if that helps.

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @Vera said in Qt crashes when adding series to plot:

                    How can I check if it is a valid pointer?

                    You should know that. Do you create this

                    QChart *Dopplerplot = new QChart();
                    

                    before radar?
                    Also it looks to me like you are creating a local variable here:

                    QChart *Dopplerplot = new QChart(); // This Dopplerplot is not the same as in header file!
                    

                    Remove QChart * there

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    V 1 Reply Last reply
                    4
                    • jsulmJ jsulm

                      @Vera said in Qt crashes when adding series to plot:

                      How can I check if it is a valid pointer?

                      You should know that. Do you create this

                      QChart *Dopplerplot = new QChart();
                      

                      before radar?
                      Also it looks to me like you are creating a local variable here:

                      QChart *Dopplerplot = new QChart(); // This Dopplerplot is not the same as in header file!
                      

                      Remove QChart * there

                      V Offline
                      V Offline
                      Vera
                      wrote on last edited by
                      #9

                      @jsulm
                      Well I am still learning C++ and pointers are always difficult.

                      I do create the Dopplerplot before radar.
                      I am creating a local variable as the one in the header file is for the radar class which is a private variable. and the creation of the Dopplerplot is in the main file where I create my GUI. But for my other plot which is also a QChart plot I did the same thing and that one works.

                      1 Reply Last reply
                      0
                      • V Offline
                        V Offline
                        Vera
                        wrote on last edited by
                        #10

                        Hello,

                        I got it working finally!
                        I had to send the series to my radar class instead of the charts.
                        So I had it like this before, where I send the charts over:

                        Radar *radar = new Radar(XZplot, Dopplerplot);
                        

                        and now I did it like this, where I send the series over:

                        Radar *radar = new Radar(series, DSeries);
                        

                        And now it plots perfectly, I just have to append new points to the series and it plots.

                        Thanks all for your help!

                        1 Reply Last reply
                        1

                        • Login

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