0

The following code:

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QtCharts>
#include <QChartView>
#include <QBarSet>
#include <QBarSeries>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QBarSet *set0 = new QBarSet("People");

    *set0 << 30 << 40 << 10 << 20 << 60 << 10;

    QBarSeries *series = new QBarSeries();
    series->append(set0);

    QChart *chart = new QChart();
    chart->addSeries(series);
    chart->setTitle("Chart Example");
    chart->setAnimationOptions(QChart::SeriesAnimations);

    QStringList categories;
    categories << "2023" << "2024" << "2025";
    categories << "2026";

    QBarCategoryAxis *axis = new QBarCategoryAxis();
    axis->append(categories);
    chart->createDefaultAxes();
    chart->setAxisX(axis, series);

    QChartView *chartView = new QChartView(chart);
    chartView->setParent(ui->horizontalFrame);
}

makes the bar chart visible:

Visible bar chart

But when I move the bar chart code inside a pushbutton slot:

void MainWindow::on_pushButton_clicked()
{
    QBarSet *set0 = new QBarSet("People");

    *set0 << 30 << 40 << 10 << 20 << 60 << 10;

    QBarSeries *series = new QBarSeries();
    series->append(set0);

    QChart *chart = new QChart();
    chart->addSeries(series);
    chart->setTitle("Chart Example");
    chart->setAnimationOptions(QChart::SeriesAnimations);

    QStringList categories;
    categories << "2023" << "2024" << "2025";
    categories << "2026";

    QBarCategoryAxis *axis = new QBarCategoryAxis();
    axis->append(categories);
    chart->createDefaultAxes();
    chart->setAxisX(axis, series);

    QChartView *chartView = new QChartView(chart);
    chartView->setParent(ui->horizontalFrame);
}

it does not become visible:

Invisible bar chart

How do I make the bar chart visible when adding it in a slot?

0

2 Answers 2

0

The first code snippet does make the bar chart appear because it sets its parent before the MainWindow instance (the parent widget) is made visible because it's done in its constructor.

The opposite is true for the second code snippet. The slot is called (when the button is clicked) after show is called on the MainWindow instance.

Calling the parent's show will handle showing its child widgets, so any children added after that will not be made visible.

Take a look at QWidget::setVisble source code.


The solution to your problem is mentioned in QWidget::setParent:

Note: The widget becomes invisible as part of changing its parent, even if it was previously visible. You must call show() to make the widget visible again.


Here is a very basic example using QPushButtons, since the problem is not about bar charts:

#include <QApplication>
#include <QWidget>
#include <QPushButton>

int main(int argc,char*argv[])
{
    QApplication a(argc, argv);
    
    QWidget parentWidget;
    QPushButton beforeBtn("Before Button");

    //this button is added before that parent is made visible
    beforeBtn.setParent(&parentWidget);

    //this call will handle showing the parent and the already added children
    parentWidget.show();

    QPushButton afterBtn("After Button");
    //this is added after calling show on the parent widget
    afterBtn.setParent(&parentWidget);
    //if you uncomment the following, the newly added widget will appear
    //afterBtn.show();

    return a.exec();
}
0

eyllanesc said:

try with chartView->show().

Example:

QChartView *chartView = new QChartView(chart);

chartView->setParent(ui->horizontalFrame);
chartView->setFixedSize(ui->horizontalFrame->size());   
chartView->show();

Not the answer you're looking for? Browse other questions tagged or ask your own question.