[SOLVED] SetLayout of qt c++

Issue

please, help me to solve this probleme of qt;
QWidget::setLayout: Attempting to set QLayout "" on Login_1 "Login_1", which already has a layout ?

Login_1::Login_1(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::Login_1)
{
    ui->setupUi(this);

    //The main windows
    QGridLayout* MainLayout = new QGridLayout();
    //The first ligne (username, line 0)
    QLabel* LbNom = new QLabel("User name");
    QLineEdit* LeNom = new QLineEdit();
    MainLayout->addWidget(LbNom,0,0);
    MainLayout->addWidget(LeNom,0,1);

    //The second line (password, line 1)
    QLabel* LbPassword = new QLabel("Password");
    QLineEdit* LePassword = new QLineEdit(this);
    MainLayout->addWidget(LbPassword,1,0);
    MainLayout->addWidget(LePassword,1,1);

    //Login Button(line 2)
    QPushButton* PbLogin = new QPushButton(this);
    PbLogin->setText("Login");
    MainLayout->addWidget(PbLogin,2,0);
    //setLayout(MainLayout);

}

Login_1::~Login_1()
{
    delete ui;
}

Solution

Thank you for your helping…

I create at first time a new widget:

QWidget* Mywidget = new QWidget();

After tha, I make for this widget (Mywidget) parent of my odd layout:

    QGridLayout* MainLayout = new QGridLayout(Mywidget);

And it work

Answered By – parfait tolefo

Answer Checked By – Senaida (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *