QT在另一个小部件内插入小部件 [英] QT insert widget inside an other widget
本文介绍了QT在另一个小部件内插入小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用qt Creator创建了一个UI,在这个UI中只有一个按钮和一个小部件(我们分别将其称为button和char_container); 我需要以编程方式在chart_container内部添加一个chartview. 我没有更改默认布局.
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();
}
推荐答案
将控件添加到另一个控件中的最佳方法是使用布局,如下所示:
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);
}
这篇关于QT在另一个小部件内插入小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文