繁体   English   中英

QT 在其他小部件中插入小部件

[英]QT insert widget inside an other widget

我用 qt Creator 创建了一个 UI,在这个 UI 中只有一个按钮和一个小部件(我们分别称它为 button 和 char_container); 我需要在chart_container 中以编程方式添加图表视图。 我没有更改默认布局。

我试过下面的代码,但它不起作用:

void MainWindow::button_slot(){
    QtCharts::QChart *chart = new QtCharts::QChart();
    QtCharts::QChartView *chartView = new QtCharts::QChartView(chart);
    chartView->setParent(ui->chart_container);
    this.repaint();
}

在另一个小部件中添加小部件的最佳方法是使用布局,如下所示:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM