Important: Please read the Qt Code of Conduct -

QChartView position

  • Hello everyone,

    I'm trying to create a screen to show a chart, but I don't know how set the position of the QChartView. In the picture bellow, the chart suppose to be between the line gray circuled in red, and the down of the screen.


    The code is below, you can see I tried some properties that are commented out, but not worked.

    // Assign names to the set of bars used
                QBarSet *set0 = new QBarSet("Aprovados");
                QBarSet *set1 = new QBarSet("Reprovados");
                /*QBarSet *set2 = new QBarSet("Segura");
                QBarSet *set3 = new QBarSet("Simmons");
                QBarSet *set4 = new QBarSet("Trout");*/
                // Assign values for each bar
                *set0 << 10;
                *set1 << 7;
                // Add all sets of data to the chart as a whole
                // 1. Bar Chart
                QBarSeries *series = new QBarSeries();
                // 2. Stacked bar chart
                // QHorizontalStackedBarSeries *series = new QHorizontalStackedBarSeries();
                // Used to define the bar chart to display, title
                // legend,
                QChart *chart = new QChart();
                // Add the chart
                // Set title
                chart->setTitle("Quantidade de Acessos Aprovados/Reprovados");
                // Define starting animation
                // NoAnimation, GridAxisAnimations, SeriesAnimations
                // Holds the category titles
                QStringList categories;
                categories << "Aprovados/Reprovados"; //<< "Reprovados";
                // Adds categories to the axes
                QBarCategoryAxis *axis = new QBarCategoryAxis();
                // 1. Bar chart
                chart->setAxisX(axis, series);
                // 2. Stacked Bar chart
                // chart->setAxisY(axis, series);
                // Define where the legend is displayed
                // Used to display the chart
                QChartView *chartView = new QChartView(chart);
                // Used to change the palette
                QPalette pal = qApp->palette();
                // Change the color around the chart widget and text
                pal.setColor(QPalette::Window, QRgb(0xffffff));
                pal.setColor(QPalette::WindowText, QRgb(0x404044));
                // Apply palette changes to the application
        /*QMainWindow window;
                // Set the main window widget
                window.resize(1020, 600);
                QGridLayout *mainLayout = new QGridLayout;
                mainLayout->addWidget(chartView, 200, 600);

    Any help is usefull. Thanks a lot!

  • @mateuscrv
    The chartview has been added in a QGridLayout, so the QGridlayout have the control of chartview Size and Position attributes.
    You could change the position of this view by change the content margins of this QGridLayout.
    Or adding spacers with fixed sizes.

  • In the meantime I thought the dynamic-spline-example would guide me, but it failed. I also found, that there is as well a scroll() for the QChart itself. But I really have no clue about what it does, since i can not, or at least I didn't find out how, zoom only in x-direction an then scroll along UPSers

Log in to reply