QLayout:无法将空小部件添加到QGridLayout /

时间:2015-04-12 11:08:27

标签: qt

我正在尝试使用Qt动态添加小部件。 我的代码看起来像这样

void MainWindow::on_actionLoad_DAS_Measurement_triggered()
{
    QWidget *MeasurementsWidget = new QWidget; //Create a new widget which will show a small icon of the loaded measurements.
    QGridLayout *MeasurementLayout = new QGridLayout; //Create a GridLayout which will contain the small icons.
    QTextBrowser *Measurements[12] = {new QTextBrowser}; //Create an array for the loaded measurements. This will contain the actual icons respectively.
    int MeasurementCount, MeasurementColumns, MeasurementLines, Number;

QStringList LoadDASMeasurement = QFileDialog::getOpenFileNames(this,"","","DAS Measurement (*.dl3; *.dl2)"); //Opening measurements in .dl2 and .dl3 format MeasurementCount = LoadDASMeasurement.count(); //Saving the number of the loaded measurement MeasurementCount++; //Increasing the number of the loaded measurement, to create enough columns after the division MeasurementLines = 2; // 2 lines are used. Can be modified in the future MeasurementColumns = MeasurementCount / MeasurementLines; Number = 0; for (int LineCount = 0; LineCount < MeasurementLines; LineCount++) { for (int ColumnCount = 0; ColumnCount < MeasurementColumns; ColumnCount++) { MeasurementLayout->addWidget(Measurements[Number],LineCount,ColumnCount); Number++; } } MeasurementsWidget->setLayout(MeasurementLayout); //Assign the widget to the GridLayout setCentralWidget(MeasurementsWidget); //Set the Widget as a centralwidget, so it will be shown in the mainwindow

}

我总是收到一条警告信息: QLayout:无法将空小部件添加到QGridLayout / 并且只显示第一个小部件。

有人能帮助解决这个警告的原因吗?

1 个答案:

答案 0 :(得分:0)

这是你的问题:

QTextBrowser *Measurements[12] = {new QTextBrowser};

创建一个包含12个指针的数组,其中第一个new QTextBrowser,其余的都是nullptr。您需要循环遍历数组并实例化12个单独的QTextBrowser