Reputation: 23
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
QLineEdit *newEdit = new QLineEdit(" ");
newEdit->setGeometry(y * 50, x * 25, 50, 25);
newEdit->setText("0");
layout()->addWidget(newEdit);
objMatrix[y + x * size] = newEdit;
}
}
I am using that code to add widgets dynamicly. Then i get this error:
QMainWindowLayout::addItem: Please use the public QMainWindow API instead
As many times, as code layout()->addWidget(newEdit);
has worked.
What should i do to prevent it?
Sorry for my english.
Upvotes: 2
Views: 2638
Reputation: 18524
You should work with layouts in another way because in your code widget has not aby layout, so your pointer to layout is bad, so you programm crashes. Try In constructor for example:
QWidget *centralWidget = new QWidget(this);
QGridLayout *layout = new QGridLayout();
centralWidget->setLayout(layout);
layout->addWidget(new QPushButton("Button 1"),0,0);
layout->addWidget(new QPushButton("Button 2"),0,1);
layout->addWidget(new QPushButton("Button 3"),0,2);
setCentralWidget(centralWidget);
If you want set position of widgets yourself then you don't need layout at all. Just set centralWidget as parent to all your another widgets and call setGeometry without any issue. Note that in this case top left corner of centralWidget will have 0;0 coordinates for child widgets.
Upvotes: 4
Reputation: 51
You must first add central widget in window. And insert widgets in him.
Upvotes: 1