QT insert widget inside an other widget

被刻印的时光 ゝ 提交于 2020-01-11 10:47:20

问题


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();
}

回答1:


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);
}


来源:https://stackoverflow.com/questions/46445790/qt-insert-widget-inside-an-other-widget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!