【问题标题】:Saving Qwtplot to image将 Qwtplot 保存到图像
【发布时间】: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


    【解决方案1】:

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

    既然要保存图表的图片,首先调用replot() 是有意义的。

    【讨论】:

    • 这应该是对Muhammet Ali Asan's answer 的评论,或者,更好的是,一个upvote。
    • 我同意,很遗憾,由于我缺乏声誉,我无法添加评论。也许最好删除这个答案并在将来添加评论
    【解决方案2】:

    我通过试错法找到了解决方案。它解决了我的问题,但我不确定它是否是最正确的解决方案。如果有人能解释原因,我会很高兴。

    在将曲线附加到绘图后并将其保存到图像之前,我调用了myplot-&gt;replot(),结果如我所料。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-25
      相关资源
      最近更新 更多