Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QChartView margins



  • Hi,

    I have a tabWidget with 2 tabs. In each tab I have added a vertical layout.
    I add programatically a QChartView in each layout.
    Here is the code for one tab

    m_cartView = new (QChartView);
    m_cartGraph = new (QChart);
    ui->layoutCart->addWidget(m_cartView);
    m_cartView->setChart(m_cartGraph);

    For testing I have added the following lines to show what is related to the QChartView and what is related to the QChart.

    m_cartGraph->setBackgroundBrush(Qt::yellow);
    m_cartView->setBackgroundBrush(Qt::red);

    I get the following result

    565dc1fe-0452-4e90-ac8f-c72fbb465f4e-image.png

    My question is, can I make the QChart occupy the complete QChartView? I don't need these margings.

    Thanks


  • Lifetime Qt Champion

    Hi
    Make sure
    layoutCart
    has setContentsMargins(0,0,0,0);



  • Hi,
    Thank you for your suggestion. Unfortunately it has no effect.
    I also tried
    m_cartGraph->setContentsMargins(0, 0, 0, 0);
    and
    m_cartView->setContentsMargins(0, 0, 0, 0);

    Then I tried the opposite to see if I can increase the margins.

    With ui->layoutCart->setContentsMargins(20, 20, 20, 20); Iget

    5f5d5fc9-e181-4899-b063-6cadee3e8960-image.png

    With m_cartView->setContentsMargins(20, 20, 20, 20);
    01ae49f6-d8c4-4d89-ad02-2b75662212d9-image.png

    With m_cartGraph->setContentsMargins(20, 20, 20, 20);
    c4934caa-9f23-4c2d-a773-2b68bfd823ca-image.png

    I also tried
    m_cartGraph->setContentsMargins(-11, -11, -11, -11); which gives the following
    81dbf0a4-75e2-4e48-b0ea-a94e445ad48e-image.png

    I think this is not the correct way to do it.


  • Lifetime Qt Champion

    Ok so the margins is really from m_cartView.

    Try do
    m_cartView->dumpObjectTree();

    and see if it has an internal layout we could try fiddling with.



  • Here is what I get

    QtCharts::QChartView::
    QWidget::qt_scrollarea_hcontainer
    QScrollBar::
    QBoxLayout::
    QWidget::qt_scrollarea_vcontainer
    QScrollBar::
    QBoxLayout::
    QWidget::
    QGraphicsScene::
    QGraphicsSceneBspTreeIndex::


  • Lifetime Qt Champion

    Hi
    Super.
    So its a QScrollArea.
    I tried to do

    auto list = chartView->findChildren<QBoxLayout *>();
        for (QBoxLayout *lay : list) {
            lay->setContentsMargins(0, 0, 0, 0);
        }
    

    but it had no effect.
    So I don't know how to get rid of that border.



  • Thank for your time looking into this issue.

    For the time being to get rid of this border I have set Bakcground color of the QChartView to the same color as the background of the QChart.


Log in to reply