PDA

View Full Version : Replece one widget for another in QGridLayout



Tomasz Losek
12th July 2015, 12:01
Hi!

I've a two buttons width display the form. Fieleds in form depend from selected button:

QWidget *base = new QWidget();
layout = new QGridLayout(base);

LeNrEwidencyjny = new QLineEdit();
LeNrRejestracyjny = new QLineEdit();
LeNrTelefonu = new QLineEdit();
GbPodstawoweInformacje = new QGroupBox();

QPushButton *BtnTypWlasny = new QPushButton(QIcon::QIcon(":/new/prefix1/user_business.png"),"pojazd własny");
BtnTypWlasny->setFixedHeight(80);
connect(BtnTypWlasny, SIGNAL(clicked()), this, SLOT(slotTypWlasny()));

QPushButton *BtnTypPrzewoznika = new QPushButton(QIcon::QIcon(":/new/prefix1/delivery.png"),"pojazd przewoźnika");
BtnTypPrzewoznika->setFixedHeight(80);
connect(BtnTypPrzewoznika, SIGNAL(clicked()), this, SLOT(slotTypPrzewoznika()));

layout->addWidget(BtnTypWlasny,0,0,1,1,Qt::AlignTop);
layout->addWidget(BtnTypPrzewoznika,0,1,1,1,Qt::AlignTop);

base->show();
this->setCentralWidget(base);

slotTypWlasny:

void FormNowaCiezarowka_add::slotTypWlasny(){
layoutTMP = new QGridLayout();
GbPodstawoweInformacje = new QGroupBox("pojazd własny");

layoutTMP->addWidget(new QLabel("nr ewidencyjny1: "),0,0,1,1,Qt::AlignTop);
layoutTMP->addWidget(LeNrEwidencyjny,0,1,1,1,Qt::AlignTop);

layoutTMP->addWidget(new QLabel("nr rejestracyjny1: "),1,0,1,1,Qt::AlignTop);
layoutTMP->addWidget(LeNrRejestracyjny,1,1,1,1,Qt::AlignTop);

layoutTMP->addWidget(new QLabel("nr telefonu1: "),2,0,1,1,Qt::AlignTop);
layoutTMP->addWidget(LeNrTelefonu,2,1,1,1,Qt::AlignTop);

GbPodstawoweInformacje->setLayout(layoutTMP);
layout->addWidget(GbPodstawoweInformacje,1,0,1,2,Qt::Align Top);
}

and the slotTypPrzewoznika:

void FormNowaCiezarowka_add::slotTypPrzewoznika(){
layoutTMP = new QGridLayout();
GbPodstawoweInformacje = new QGroupBox("pojazd przewoźnika");

layoutTMP->addWidget(new QLabel("nr ewidencyjny2: "),0,0,1,1,Qt::AlignTop);
layoutTMP->addWidget(LeNrEwidencyjny,0,1,1,1,Qt::AlignTop);

layoutTMP->addWidget(new QLabel("nr rejestracyjny2: "),1,0,1,1,Qt::AlignTop);
layoutTMP->addWidget(LeNrRejestracyjny,1,1,1,1,Qt::AlignTop);

GbPodstawoweInformacje->setLayout(layoutTMP);
layout->addWidget(GbPodstawoweInformacje,1,0,1,2,Qt::Align Top);
}
whet i've pushed BtnTypWlasny and next push the BtnTypPrzewoznika i've got:
11272


I can't replece one widget by another :

layout->addWidget(GbPodstawoweInformacje,1,0,1,2,Qt::Align Top);

I've tried for example:

layout->removeWidget(layout->itemAtPosition(1, 0)->widget());
QWidget* widget0 = layout->itemAt(2)->widget();
delete widget0;

but it doesn't work :(

anda_skoa
12th July 2015, 13:07
What if you just delete the respective other groupbox widget?

Also you should probably check if the widget you are creating already exists, e.g. if the same button is clicked twice.

But what you should really consider is to put a QStackedWidget into cell 1,0 and use those two groupboxes as it pages.

Cheers,
_

d_stranz
12th July 2015, 17:40
layout->removeWidget(layout->itemAtPosition(1, 0)->widget());

Doesn't this change the indexes of the remaining grid items?


QWidget* widget0 = layout->itemAt(2)->widget();

And doesn't this need a column index?