Display own widget in new window
Hi.
I want to do, when I click pushButton it should show new window with my widget (pie chart)
I have something like this
Code:
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?
Re: Display own widget in new window
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.
Re: Display own widget in new window
Quote:
Originally Posted by
d_stranz
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?
Re: Display own widget in new window
Quote:
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.
Re: Display own widget in new window
Quote:
Originally Posted by
d_stranz
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?
Re: Display own widget in new window
Quote:
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".