2015-11-11 250 views
1

我正在使用QwtPlotRenderer将绘图保存到文件。我还使用QImage::grabWidget()将绘图保存到QPixmap。将Qwtplot保存为图像

但在这两种情况下产生的图像正在:

enter image description here

正如你所看到的,而在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(); 
} 

回答

0

我找到了解决方法的试验和错误method.It解决我的问题,但我不知道这是否是右端solution.If任何人都可以解释的原因,我会快乐。

将曲线附加到绘图之后,并保存到图像之前,我称为myplot->replot(),结果如我所料。

1

replot()函数是实际绘制图形的函数。

由于您要保存图形的图片,首先请致电replot()

+0

这应该是对[Muhammet Ali Asan的回答](http://stackoverflow.com/a/33654083/3982001)的评论,或者更好的说是赞扬。 –

+0

我同意,不幸的是我由于缺乏声誉而无法添加评论。 也许最好删除这个答案,并在将来添加评论 – Silyus