PDA

View Full Version : Display own widget in new window



time
21st November 2016, 20:32
Hi.
I want to do, when I click pushButton it should show new window with my widget (pie chart)

I have something like this



MainWindow::on_PieChart1_pushButton_clicked()
{
QPieSeries *series = new QPieSeries();
series->append("Jane", 1);
series->append("Joe", 2);
series->append("Andy", 3);
series->append("Barbara", 4);
series->append("Axel", 5);

QPieSlice *slice = series->slices().at(1);
slice->setExploded();
slice->setLabelVisible();
slice->setPen(QPen(Qt::darkGreen, 2));
slice->setBrush(Qt::green);

QChart *chart = new QChart();
chart->addSeries(series);
chart->setTitle("Simple piechart example");
chart->legend()->hide();

QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);

//here I need to make new window and put chartView inside
}


So how to write it, to show new window with my pie chart when I click the push button?

I know you can use setCentralWidget(QWidget *widget) but it works only with mainWindow. I need it in new window. Any ideas?

d_stranz
22nd November 2016, 01:19
chartView->show();

Of course, what you are doing leaks memory for the QChartView. And since you create your QChartView without a parent, it is a top-level window and probably won't appear where you want it to.

If that isn't the behavior you want, then you'll have to explain what should happen when the new chart is displayed.

time
22nd November 2016, 13:10
Of course, what you are doing leaks memory for the QChartView.

So, of course I do not want memory leaks. I should use the delete chartView somewhere near end of function?

d_stranz
22nd November 2016, 22:24
I should use the delete chartView somewhere near end of function?

No. If you don't want a memory leak, then create the QChartView with the MainWindow as a parent. The chart view will then be deleted when the main window is deleted, whether the chart view is visible or not.

time
23rd November 2016, 13:24
No. If you don't want a memory leak, then create the QChartView with the MainWindow as a parent. The chart view will then be deleted when the main window is deleted, whether the chart view is visible or not.

Oh, ok. Can I ask you for simple example how to do that? How to create QChartView as a parent of MainWindow?

d_stranz
23rd November 2016, 16:11
How to create QChartView as a parent of MainWindow?

No, no. Your QChartView should be a child of MainWindow. See that there are two QChartView constructors; in Line 21 of your code above use the one that takes a QWidget * parent argument and use "this" (your MainWindow instance) as the value for "parent".