PDA

View Full Version : connecting button to stacked layout



astiolo
28th May 2011, 08:53
I am experimenting with a program to log the work needed on a car and. I have created a stacked layout, with group boxes for each layout. I want to connect a button in a group box to make it change to another layout. I don't understand linking to children and parents properly so I can't get it to work.

I want to connect the addCar button signal 'clicked()' in carSelectionPage to the stacked layout slot 'setCurrentIndex(1)'. Where do I put the connect code and how do I refer to both items?

Alternatively if there is a better way of having separate windows which I can navigate between, that would be good.




#include <QtGui>
#include "cardiary.h"
#include <QStackedLayout>

CarDiary::CarDiary(QWidget *parent)
: QWidget(parent)
{

QStackedLayout *stackedLayout = new QStackedLayout;
stackedLayout->addWidget(carSelectionPage());
stackedLayout->addWidget(addCarPage());


QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(stackedLayout);
setLayout(mainLayout);

stackedLayout->setCurrentIndex(0);

}


QGroupBox *CarDiary::carSelectionPage()
{
QGroupBox *groupBox = new QGroupBox();

QVBoxLayout *carList = new QVBoxLayout;

QPushButton *addCar = new QPushButton(tr("&Add Car"));
QPushButton *car1 = new QPushButton(tr("&MX 5"));


carList->addWidget(car1);
carList->addWidget(addCar);
groupBox->setLayout(carList);

return groupBox;

}

QGroupBox *CarDiary::addCarPage()
{
QGroupBox *groupBox = new QGroupBox();

QVBoxLayout *layout = new QVBoxLayout;

QLabel *carNameLabel = new QLabel(tr("Car Name:"));
QLineEdit *carNameInput = new QLineEdit();

layout->addWidget(carNameLabel);
layout->addWidget(carNameInput);
groupBox->setLayout(layout);

return groupBox;
}

Santosh Reddy
28th May 2011, 09:22
I want to connect the addCar button signal 'clicked()' in carSelectionPage to the stacked layout slot 'setCurrentIndex(1)'. Where do I put the connect code and how do I refer to both items?
Modify as following



class CarDiary {
...
private slot:
void selectCar(void);
...
private:
QStackedLayout *stackedLayout;
};

CarDiary::CarDiary(QWidget *parent)
: QWidget(parent)
{
stackedLayout = new QStackedLayout;
...
}

CarDiary::selectCar(void)
{
stackedLayout->setCurrentIndex(1);
}

QGroupBox *CarDiary::carSelectionPage()
{
...
connect(addCar, SIGNAL(clicked()), this, SLOT(selectCar())); //add this after creating addCar
return groupBox;
}



Alternatively if there is a better way of having separate windows which I can navigate between, that would be good.
There is no alternative, you should first understand how signals and slots work in Qt. If someone were to say an alternative, it would be to post a question in a forum, as you did.

astiolo
28th May 2011, 09:49
That worked nicely, just needed to add 'void' infront of 'CarDiary::selectCar(void)' as well.

I was trying to do it in one line and assumed I would need to use the parent function somehow, but this is good.


There is no alternative...

I meant is there an alternative to the stacked layout with group boxes, but this is working well.

Thankyou

Santosh Reddy
28th May 2011, 09:56
Alternatively if there is a better way of having separate windows which I can navigate between, that would be good.


I meant is there an alternative to the stacked layout with group boxes, but this is working well.

Having stacked layout or separate windows is your UI design decision.

just to answer you, technically both are possible.