1

Currently, I am writing a code that plots some 2D-Data (splines) using QChartView and QChart. I want to add some graphics objects like rectangles or even images to the graphs. Is this possible? There seem to be no methods like draw rectangle within QChart. My code looks something like this:

QChartView *mainChart; 
QChart *chart; 
chart=new QChart(); 
series_x = new QSplineSeries; {...} chart->addSeries(series_x); 
mainChart->setChart(plot); setCentralWidget(mainChart)

Best regards.

NickCoder
  • 1,504
  • 2
  • 23
  • 35
Angelina
  • 11
  • 2
  • Sorry, here is the code: `QChartView *mainChart; QChart *chart; chart=new QChart(); series_x = new QSplineSeries; {...} chart->addSeries(series_x); mainChart->setChart(plot); setCentralWidget(mainChart)` – Angelina Oct 10 '19 at 09:03

2 Answers2

0

Ok....

After a lot of research I found QGraphicsView and QGraphicsScene. This allows for adding overlaing widgets. Though, now I am struggling with the size of my QChart when the main window is resized. Furthermore, I found no way to match the size of the chart to the part of the main window I am painting on. I tried things like setMinimumSize, mainwindow->size but it never is there where it is supposed to be.

And of course: the coordinate system of the chart is not the same as the scene coordinates. The problem is, I want to relate the graphics objects to the coordinates in the chart.

Any suggestions?

Thanks and cheers Angelina

Angelina
  • 11
  • 2
0

You should try using resizeEvent() method from the QWidget class that you can use directly from your QMainWindow object.

For example if your class inheriting from QMainWindow is called PersonalWindow, you'll have:

PersonalWindow.h

void resizeEvent(QResizeEvent* event) override;

PersonalWindow.cpp

void PersonalWindow::resizeEvent(QResizeEvent* event) {
    //
    //  Your own code here.
    //

    //
    QMainWindow::resizeEvent(event);
}

In this method you can get a member variable that is the current size of your window and set it back to your chart.

Patapoom
  • 794
  • 1
  • 7
  • 16