2017-03-27 5 views
1

を表示されませんでしたチャートを保存:QtChart - C++ - 私は、この例ではQTextDocumentでは、ファイルにグラフを保存しようとしている

QTextDocument doc("Frame rate test\n"); 
QTextCursor cursor(&doc); 
cursor.movePosition(QTextCursor::End); 

if (getTestFinishedStatus()) 
{ 
    QPixmap pix = _pFrameRateChart->grab(); //_pFrameRateChart is QChartView 
    cursor.insertImage(pix.toImage()); 
} 

QTextDocumentWriter docWriter; 
docWriter.setFileName("framerate.odf"); 
docWriter.setFormat("ODF"); 
docWriter.write(&doc); 

問題は、結果は同じではありませんです私がuiでチャートを表示している場合。明らかに

enter image description here

私は追加しない場合でも、2番目の結果を持っているしたいと思います:

ここenter image description here

が表示された結果である: はここに表示されていない結果であり、チャートビューをウィジェットに表示してUIビューに表示します。 QChartのサイズ変更、QChartのサイズ変更、一時的なウィジェットへのチャートの追加、QVBoxLayoutの保存、一時的にQChartViewの保存などを表示しましたが、良い結果が得られませんでした。

答えて

0

私はので、ここではよりしかし、回避策のようなものです私の解決策、だ、これに任意の簡単な方法を見つけることができませんでした:それは働いていますが、小さなウィンドウがで開かれています

QPixmap ChartView::getChartPixmap() 
{ 
    QWidget* w = new QWidget; //creating a temporary widget, which will enable to display the chart 

    w->resize(REPORT_IMAGE_WIDTH, REPORT_IMAGE_HEIGHT); 
    QVBoxLayout *vl; 
    vl = new QVBoxLayout(w); 
    vl->addWidget(this); //'this' being the QChartView 

    w->show(); //showing the widget so it is resized and can be grabbed with the correct dimensions 

    QTest::qWait(500); //we need to wait for a little for the graph to be drawn otherwise you'll still have the same size problem 

    QPixmap pixmap = w->grab(); //retrieve the pixmap 

    w->hide(); //hiding the widget 

    return pixmap; 
} 

このグラフは500msです。

0

QtChartsがQGraphivsViewに基づいているので、次のコードを使用してPixmap上にQGraphivsViewをレンダリングすると、これも機能すると思います。

を試してください。 pixmapを取得しようとする代わりに画像を表示してください。

void Printer::putProfileImage(QRect profilePlaceholder, QRect viewPort, QPainter *painter, QGraphivsView* profile) 
{ 
    int x = profilePlaceholder.x() - viewPort.x(); 
    int y = profilePlaceholder.y() - viewPort.y(); 
    QRect pos(x, y, profilePlaceholder.width(), profilePlaceholder.height()); 
    profile->render(painter, pos); 

} 
+0

残念ながら、私は大きな画像でも小さなグラフで同じ結果を得ます。 – Dinendal

+0

は、私が実際のプロジェクトで使用するいくつかのコードで答えを更新しました。 –

関連する問題

 関連する問題