簡體   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