2015-11-11 218 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