How to remove margin from QChartView or QChart

我的未来我决定 提交于 2019-11-30 06:47:13

问题


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

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();

回答1:


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);



回答2:


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));


来源:https://stackoverflow.com/questions/39146502/how-to-remove-margin-from-qchartview-or-qchart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!