C++ Static function and dynamic memory



  • Hi,

    I am building a QTableView with a list of <Workout>
    Here an example :
    https://www.dropbox.com/s/fr9pfy4a8mgbuqt/workoutList1.png

    To draw the graph, I had to reimplement paint in a custom Delegate.
    #1 down here is the current code that does that (working fine)

    What I want to do, is do the graphing stuff in another class with a static method so I can use it from other places.
    So I made the class "util.h" with this method :
    static std::shared_ptr<QwtPlot> plotWorkout(std::shared_ptr<Workout> workout);
    see #2 for code, it is the exact same code as #1, but it seems my histogram data is not transfering. I suspect it has to do with QVector object been deleted as soon as I exit my static function? How can I create a QVector dynamicly so that it doesn't get deleted? I'm new with C++ and the syntax is a bit daunting for me. Usually what I do is to keep a pointer of a "new" object but QVector doesn't support "new"

    Thanks in advance !!

    #1
    @void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    std::shared_ptr<QwtPlot> plot(new QwtPlot());

    // ---- GRAPH -------------------------
    plot->setCanvasBackground(QColor(Qt::black));
    plot->setAxisScale( QwtPlot::yLeft, 0.0, 100);
    plot->setAxisScale( QwtPlot::xBottom, 0.0, workout->getTotalLength());
    plot->enableAxis(0, false);
    plot->enableAxis(2, false);
    
    
    // ---- HISTOGRAM (POWER INTERVAL) -----
    std::shared_ptr<QwtPlotHistogram> histo(new QwtPlotHistogram());
    histo->setZ(0);
    histo->setStyle(QwtPlotHistogram::Columns);
    histo->setPen( QPen( Qt::darkGreen, 1 ) );
    histo->setBrush( QBrush(Qt::darkGreen) );
    
    // ----- INTERVAL dans HISTOGRAM --------
    QVector<QwtIntervalSample> samples(workout->getNbInterval());
    double time = 0;
    int i = 0;
    foreach (std::shared_ptr<Interval> val, workout->getLstInterval()) {
        QwtInterval interval(time, time + val->getLength());
        qDebug() << "FTP T: " << val->getTargentFTP();
        samples[i]= QwtIntervalSample(val->getTargentFTP(), interval);
        time += val->getLength();
        i++;
    }
    
    histo->setData(new QwtIntervalSeriesData(samples));
    histo->attach(plot.get());
    plot->replot();
    
    
    
    
    if (option.state & QStyle::State_Selected)
        painter->fillRect(option.rect, option.palette.highlight());
    
    
    QwtPlotRenderer* plotRender = new QwtPlotRenderer();
    plotRender->setDiscardFlag(QwtPlotRenderer::DiscardBackground, true);
    plotRender->setDiscardFlag(QwtPlotRenderer::DiscardCanvasBackground, true);
    plotRender->setDiscardFlag(QwtPlotRenderer::DiscardLegend,true);
    plotRender->setDiscardFlag(QwtPlotRenderer::DiscardTitle,true);
    plotRender->setDiscardFlag(QwtPlotRenderer::DiscardFooter,true);
    plotRender->setLayoutFlag(QwtPlotRenderer::FrameWithScales,true);
    
    
    
    plotRender->render(plot.get(), painter, option.rect);
    

    }@

    #2
    @//--------------------------------------------------------------------------
    std::shared_ptr<QwtPlot> Util::plotWorkout(std::shared_ptr<Workout> workout) {

    std::shared_ptr<QwtPlot> plot(new QwtPlot());
    
    
    // ---- GRAPH -------------------------
    plot->setCanvasBackground(QColor(Qt::black));
    plot->setAxisScale( QwtPlot::yLeft, 0.0, 100);
    plot->setAxisScale( QwtPlot::xBottom, 0.0, workout->getTotalLength());
    

    // plot->enableAxis(0, false);
    // plot->enableAxis(2, false);

    // ---- HISTOGRAM (POWER INTERVAL) -----
    std::shared_ptr<QwtPlotHistogram> histo(new QwtPlotHistogram());
    
    histo->setZ(0);
    histo->setStyle(QwtPlotHistogram::Columns);
    histo->setPen( QPen( Qt::darkGreen, 1 ) );
    histo->setBrush( QBrush(Qt::darkGreen) );
    
    // ----- INTERVAL dans HISTOGRAM --------
    QVector<QwtIntervalSample> samples(workout->getNbInterval());
    double time = 0;
    int i = 0;
    foreach (std::shared_ptr<Interval> val, workout->getLstInterval()) {
        QwtInterval interval(time, time + val->getLength());
        samples[i]= QwtIntervalSample(val->getTargentFTP(), interval);
        time += val->getLength();
        i++;
    }
    
    
    histo->setData(new QwtIntervalSeriesData(samples));
    histo->attach(plot.get());
    plot->replot();
    return plot;
    

    }@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.