Replace widget in Qt

I have a base class that has some gui elements that I set to use the constructor in the Qt creator. These elements are:

QWidget* w1;
QWidget* w2;
QWidget* w3;

Now in the class that inherits this base class, I would like to "convert" these widgets to lineEdit elements that will contain all the geometric parameters of these widgets. So I am doing something like this:

QLineEdit* leAmplitude;
leAmplitude = new QLineEdit(ui->w1);
leAmplitude->setGeometry(ui->w1->geometry());
ui->glControls->addWidget(leAmplitude);

But the added item QLineEditdoes not appear in the same place as the w1 item. It is simply added at the bottom of other controls in QGridLayoutglControls. How to make lineEdit accept all geometric parameters from w1?

+4
2

, , , , setGeometry, addLayout . , , - ui->w1 .

, QLayout:: replaceWidget! . :

QLineEdit* leAmplitude;
leAmplitude = new QLineEdit;
QLayoutItem *previous = ui->glControls->replaceWidget(ui->w1, leAmplitude);
// possibly assert that previous is ui->w1, or just delete it, or whatever

Qt 5.2, , , , , () . , , QGridLayout:: addWidget , ( sizeHint sizePolicy).

+6

, :

QLineEdit* leAmplitude;
leAmplitude = new QLineEdit(ui->w1->parentWidget());
ui->w1->parentWidget()->layout()->replaceWidget(ui->w1, leAmplitude);
ui->w1 = leAmplitude;
0

Source: https://habr.com/ru/post/1628655/


All Articles