【发布时间】:2015-11-11 15:12:34
【问题描述】:
我正在使用QwtPlotRenderer 将绘图保存到文件。我还使用QImage::grabWidget() 将绘图保存到 QPixmap。
但在这两种情况下,生成的图像都是:
如您所见,曲线在结果图像中不可见,而在myplot->show() 函数中我使用的是完整输出。我该如何解决这个问题?
这是我的代码:
#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();
}
【问题讨论】:
标签: c++ qt qwidget qwt qpixmap