Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QT insert widget inside an other widget

I've created an UI with qt Creator,in this UI there is just a button and a widget (let's call it respectively button and char_container); I need to add a chartview programmatically inside the chart_container. I didn't change the default layout.

I've tried the following code,but it does not work:

void MainWindow::button_slot(){
    QtCharts::QChart *chart = new QtCharts::QChart();
    QtCharts::QChartView *chartView = new QtCharts::QChartView(chart);
    chartView->setParent(ui->chart_container);
    this.repaint();
}
like image 768
Antonio Del Sannio Avatar asked Oct 15 '25 14:10

Antonio Del Sannio


1 Answers

The best way to add a widget inside another is to use a layout, as I show below:

//on constructor
ui->chart_container->setLayout(new QVBoxLayout);


void MainWindow::button_slot()
{
    QtCharts::QChart *chart = new QtCharts::QChart();
    QtCharts::QChartView *chartView = new QtCharts::QChartView(chart, ui->chart_container);
    ui->chart_container->layout()->addWidget(chartView);
}
like image 182
eyllanesc Avatar answered Oct 18 '25 06:10

eyllanesc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!