Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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.

    0_1556228531025_c4cdbe94-6654-4ef5-8128-b99aaebfc788-image.png

    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();
                series->append(set0);
                series->append(set1);
                
    
                // Used to define the bar chart to display, title
                // legend,
                QChart *chart = new QChart();
    
                // Add the chart
                chart->addSeries(series);
    
                // Set title
                chart->setTitle("Quantidade de Acessos Aprovados/Reprovados");
    
                // Define starting animation
                // NoAnimation, GridAxisAnimations, SeriesAnimations
                chart->setAnimationOptions(QChart::AllAnimations);
    
                // Holds the category titles
                QStringList categories;
                categories << "Aprovados/Reprovados"; //<< "Reprovados";
    
                // Adds categories to the axes
                QBarCategoryAxis *axis = new QBarCategoryAxis();
                axis->append(categories);
                chart->createDefaultAxes();
    
                // 1. Bar chart
                chart->setAxisX(axis, series);
    
                // 2. Stacked Bar chart
                // chart->setAxisY(axis, series);
    
                // Define where the legend is displayed
                chart->legend()->setVisible(true);
                chart->legend()->setAlignment(Qt::AlignBottom);
    
                // Used to display the chart
                QChartView *chartView = new QChartView(chart);
                chartView->setRenderHint(QPainter::Antialiasing);
                //chartView->setFixedSize(720,400);
                //chartView->setGeometry(0,400,720,400);
    
                // 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
                qApp->setPalette(pal);
    
        /*QMainWindow window;
    
                // Set the main window widget
    
    
                window.setCentralWidget(chartView);
                window.resize(1020, 600);
                window.show();*/
    
                QGridLayout *mainLayout = new QGridLayout;
                mainLayout->addWidget(chartView, 200, 600);
                setLayout(mainLayout);
    

    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.
    https://doc.qt.io/qt-5/qlayout.html#setContentsMargins
    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