1
votes

I am trying to add widgets dynamically with Qt. My code looks like this

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

}

I always get a warning message: QLayout: Cannot add a null widget to QGridLayout/ And only the first widget is shown.

Can anybody help why this warning occurs?

1

1 Answers

1
votes

Here's your problem:

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

That creates an array of 12 pointers where the first is a new QTextBrowser and the rest are all nullptr. You'll need to loop over the array and instantiate 12 individual QTextBrowsers.