0
votes

Could some one help me on my code issue

I use 2 Push buttons (in Qt GUi Program) ( 1st, 2nd ) in the bellow code

Initial Image

1) When I press 1st Push Button, the output in Gbox .. When 1st button pressed

1st push button clicked

2) When I Press 2nd Push Button the expected output should be like Expected output in Gbox after pressing 2nd button

Expected when 2nd Push button Clicked

But no changes after pressing 2nd button in Gbox.. No Changes after pressing 2nd Button

No_changes in Gbox_2nd Push Button Selected

#include "test.h"
#include "ui_test.h"
#include <QPushButton>
#include <QCheckBox>
#include <QVBoxLayout>
#include <QtWidgets>
#include <QLineEdit>
#include <QTextEdit>
#include <QGridLayout>
#include <QVBoxLayout>
#include <QGroupBox>

test::test(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::test)
{
    ui->setupUi(this);
    QVBoxLayout *mainLayout = new QVBoxLayout;

    ui->label_1->hide();
    ui->label_2->hide();
    ui->label_3->hide();
    ui->label_4->hide();
    ui->label_5->hide();
    ui->label_6->hide();
    ui->label_7->hide();

    ui->lineEdit_1->hide();
    ui->lineEdit_2->hide();
    ui->lineEdit_3->hide();
    ui->lineEdit_4->hide();
    ui->lineEdit_5->hide();
    ui->lineEdit_6->hide();
    ui->lineEdit_7->hide();

    ui->Optional->hide();

    Gbox->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Maximum);
    Gbox->setTitle(tr("Gbox_11"));

    mainLayout->addWidget(Gbox);
    setLayout(mainLayout);
}

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

void test::on_1st_clicked()
{
    ui->label_1->show();
    ui->lineEdit_1->show();
    ui->label_1->setText("R : ");

    ui->label_2->show();
    ui->lineEdit_2->show();
    ui->label_2->setText("ID : ");

    ui->label_3->show();
    ui->lineEdit_3->show();
    ui->label_3->setText("X : ");

    ui->label_4->show();
    ui->lineEdit_4->show();
    ui->label_4->setText("Y : ");

    ui->Optional->hide();
    ui->label_5->hide();
    ui->label_6->hide();
    ui->label_7->hide();
    ui->lineEdit_5->hide();
    ui->lineEdit_6->hide();
    ui->lineEdit_7->hide();
    ui->Gbox->show();

    QPushButton *Road = new QPushButton("R");
    QPushButton *Junction = new QPushButton("J");
    QPushButton *Lane = new QPushButton("L");
    QPushButton *Objects = new QPushButton("O");
    QPushButton *Vehical = new QPushButton("V");
    QPushButton *Fupdates = new QPushButton("F");

    glay = new QGridLayout;

    glay->addWidget(Road,0,0,1,1);
    glay->addWidget(Junction,1,1,1,1);
    glay->addWidget(Lane,2,0,1,1);
    glay->addWidget(Objects,3,1,1,1);
    glay->addWidget(Vehical,4,2,1,1);
    glay->addWidget(Fupdates,5,3,1,1);

    setLayout(glay);

    ui->Gbox->setLayout(glay);
}

void test::on_2nd_clicked()
{
    ui->label_1->show();
    ui->lineEdit_1->show();
    ui->label_1->setText("J: ");

    ui->label_2->show();
    ui->lineEdit_2->show();
    ui->label_2->setText("ID : ");

    ui->label_3->show();
    ui->lineEdit_3->show();
    ui->label_3->setText("X : ");

    ui->label_4->show();
    ui->lineEdit_4->show();
    ui->label_4->setText("Y : ");

    ui->Optional->show();
    ui->Gbox->show();

    QPushButton *A = new QPushButton("1");
    QPushButton *B = new QPushButton("2");
    QPushButton *C = new QPushButton("3");
    QPushButton *D = new QPushButton("4");
    QPushButton *E = new QPushButton("5");
    QPushButton *F = new QPushButton("6");

    vlay = new QVBoxLayout;

    vlay->addWidget(A,1);
    vlay->addWidget(B,0);
    vlay->addWidget(C,0);
    vlay->addWidget(D,1);
    vlay->addWidget(E,1);
    vlay->addWidget(F,0);
    setLayout(vlay);

    ui->Gbox->setLayout(vlay);
}

void test::on_Optional_stateChanged()
{
    if(ui->Optional->isChecked())
    {
        ui->label_5->show();
        ui->label_5->setText("P :");

        ui->label_6->show();
        ui->label_6->setText("S 1 :");

        ui->label_7->show();
        ui->label_7->setText("S 2 :");

        ui->lineEdit_5->show();
        ui->lineEdit_6->show();
        ui->lineEdit_7->show();
    }
    else
    {
        ui->label_5->hide();
        ui->label_6->hide();
        ui->label_7->hide();
        ui->lineEdit_5->hide();
        ui->lineEdit_6->hide();
        ui->lineEdit_7->hide();
    }
}
1
You're using the layout glay more than once -- you have setLayout(glay) followed immediately by ui->Gbox->setLayout(glay). Likewise with vlay in test::on_2nd_clicked. Please provide a minimal reproducible example.G.M.
I removed the reparations of glay, vlay and tried but still same issue..QWidget::setLayout: Attempting to set QLayout "" on QGroupBox "Gbox", which already has a layouthemanth
Any better example to know on how to reset the group box when called by different Push Buttons..?hemanth
Instead of replacing layouts, why do not you use QStackedWidget?eyllanesc

1 Answers

0
votes

Use QLabel::repaint() in each method, and delete previously set layout before setting the new layout for GBox, for exmaple:

void test::on_2nd_clicked(){
                    ui->label_1->repaint();
                    ui->label_1->setText("J: ");

                    ui->label_2->repaint();
                    ui->label_2->setText("ID : ");
                    ...
                    ...
 QPushButton *A = new QPushButton("1");
    QPushButton *B = new QPushButton("2");
    QPushButton *C = new QPushButton("3");
    QPushButton *D = new QPushButton("4");
    QPushButton *E = new QPushButton("5");
    QPushButton *F = new QPushButton("6");

    vlay = new QVBoxLayout;


    vlay->addWidget(A,1);
    vlay->addWidget(B,0);
    vlay->addWidget(C,0);
    vlay->addWidget(D,1);
    vlay->addWidget(E,1);
    vlay->addWidget(F,0);
    setLayout(vlay);
    delete glay;
    ui->Gbox->setLayout(vlay);

    }