22

All Qt Charts seem to have a margin and rounded corners.

Example image with red marked border

How to remove both?

I want the white area to fill the entire dialog. I cannot find any informations in the Qt documentations. I found a screenshot of one Example that does not have this spacing. But I cannot find the code that disables it.

My initialization code looks like this:

QPieSeries *series = new QPieSeries();
series->append("Jane", 1);
series->append("Joe", 2);
series->append("Andy", 3);
series->append("Barbara", 4);
series->append("Axel", 5);

QChart *chart = new QChart();
chart->addSeries(series);

QChartView *chartView = new QChartView(chart);
chartView->setBackgroundBrush(Qt::red);
chartView->setRenderHint(QPainter::Antialiasing);

QMainWindow window;
window.setCentralWidget(chartView);
window.resize(400, 300);
window.show();
feedc0de
  • 3,646
  • 8
  • 30
  • 55
  • Is the red part of the `QChartView` or part of the `QChart`? You could try either `ui->pieChartView->setBackgroundBrush(Qt::white)` or `m_pieChart->setMargins(QMargins())`. – G.M. Aug 25 '16 at 14:02
  • 6
    You should do something like this: `m_pieChart->layout()->setContentsMargins(0, 0, 0, 0);` – Devopia Aug 26 '16 at 08:35
  • @Devopia: `m_pieChart` is now `chart` in the example. Your code works! But i still have rounded borders. If you also know how to remove them, I would accept your answer (if you write one). – feedc0de Aug 26 '16 at 08:51
  • 5
    Sure :) something like: `chart->setBackgroundRoundness(0);` – Devopia Aug 26 '16 at 08:56
  • @Devopia you really should write an anser to this question. You solved my problem perfectly and I want to close this question (with an answer)! – feedc0de Aug 29 '16 at 06:16
  • @Devopia Thanks; those margins were driving me nuts. I tried like 100 other things but never thought to go through `layout()`. – Jason C Feb 21 '22 at 02:31

2 Answers2

34

Devopia answerd the question in the comments!

In my example above I needed the following 2 lines of code to remove the red part completely:

chart->layout()->setContentsMargins(0, 0, 0, 0);
chart->setBackgroundRoundness(0);
feedc0de
  • 3,646
  • 8
  • 30
  • 55
4

At Qt 5.11 this does not work because the layout() method const (inherited from QGraphicsWidget):

QGraphicsLayout *QGraphicsWidget::layout() const

Furthermore it is not possible to take a copy of this because QGraphicsLayout is a base class.

For me this works:

chart->setMargins(QMargins(0,0,0,0));
Alex44
  • 3,597
  • 7
  • 39
  • 56
  • 1
    Why would the layout() method being const be a problem, given that it returns a pointer to a non-const QGraphicsLayout? Also, `chart->setMargins()` is not quite the same as `chart->layout()->setContentsMargins()`. Speaking in CSS terms, the former sets the chart's padding, while the latter sets its margins. – Joseph Artsimovich Jul 24 '18 at 18:43
  • The line `chart->layout()->setContentsMargins(0, 0, 0, 0);` works well with Qt 5. Furthermore Joseph Artsimovich describes perfectly the problem you certainly have. – Patapoom May 22 '20 at 08:42
  • This is not right. 1) That method being `const` means it won't modify the `QGraphicsWidget`; but the returned layout itself is not `const` and you can do whatever you want with it, and 2) `setMargins` sets the margins *inside* the rounded border; `layout()->setContentsMargins` is the margins outside that border to the edge of the widget; they're not quite the same. – Jason C Feb 21 '22 at 02:28