Muhammet Ali Asan Muhammet Ali Asan - 3 months ago 26
C++ Question

Saving Qwtplot to image

I am using

QwtPlotRenderer
to save plot to file. I have also used
QImage::grabWidget()
to save plot to QPixmap.

But in both cases the resulting image is being :

enter image description here

As you can see ,the curve is not visible in result image whereas in
myplot->show()
function I am using full output. How can I solve this ?

Here is my code :

#include "QApplication"
#include<qwt_plot_layout.h>
#include<qwt_plot_curve.h>
#include<qwt_scale_draw.h>
#include<qwt_scale_widget.h>
#include<qwt_legend.h>
#include<qwt_plot_renderer.h>
class TimeScaleDraw:public QwtScaleDraw
{
public:
TimeScaleDraw(const QTime & base)
:baseTime(base)
{
}
virtual QwtText label(double v)const
{
QTime upTime = baseTime.addSecs((int)v);
return upTime.toString();
}
private:
QTime baseTime;
};

int main(int argc,char * argv[])
{
QApplication a(argc,argv);

QwtPlot * myPlot = new QwtPlot(NULL);

myPlot->setAxisScaleDraw(QwtPlot::xBottom,new TimeScaleDraw(QTime::currentTime()));
myPlot->setAxisTitle(QwtPlot::xBottom,"Time");
myPlot->setAxisLabelRotation(QwtPlot::xBottom,-50.0);
myPlot->setAxisLabelAlignment(QwtPlot::xBottom,Qt::AlignLeft|Qt::AlignBottom);

myPlot->setAxisTitle(QwtPlot::yLeft,"Speed");
QwtPlotCurve * cur = new QwtPlotCurve("Speed");
QwtPointSeriesData * data = new QwtPointSeriesData;
QVector<QPointF>* samples=new QVector<QPointF>;
for ( int i=0;i<60;i++)
{
samples->push_back(QPointF(i,i*i));
}

data->setSamples(*samples);
cur->setData(data);
cur->attach(myPlot);

//myPlot->show();


QPixmap pix = QPixmap::grabWidget(myPlot);
std::cout<<pix.save("der.png","PNG")<<std::endl;
QPixmap pixmap (400,400);
QPainter * painter=new QPainter(&pixmap);

QwtPlotRenderer rend;

rend.render(myPlot,painter,myPlot->geometry());
pixmap.save("Dene.jpg");
return a.exec();
}

Answer

I found the solution by trial and error method.It solved my problem but I am not sure if it is the rightest solution.If anyone can explain reason I would be happy.

After attaching the curve to plot and before saving it to image I called myplot->replot() and the result become as I expected.

Comments