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.


Qt Code:
  1. #include <QtGui>
  2. #include "cardiary.h"
  3. #include <QStackedLayout>
  4.  
  5. CarDiary::CarDiary(QWidget *parent)
  6. : QWidget(parent)
  7. {
  8.  
  9. QStackedLayout *stackedLayout = new QStackedLayout;
  10. stackedLayout->addWidget(carSelectionPage());
  11. stackedLayout->addWidget(addCarPage());
  12.  
  13.  
  14. QVBoxLayout *mainLayout = new QVBoxLayout;
  15. mainLayout->addLayout(stackedLayout);
  16. setLayout(mainLayout);
  17.  
  18. stackedLayout->setCurrentIndex(0);
  19.  
  20. }
  21.  
  22.  
  23. QGroupBox *CarDiary::carSelectionPage()
  24. {
  25. QGroupBox *groupBox = new QGroupBox();
  26.  
  27. QVBoxLayout *carList = new QVBoxLayout;
  28.  
  29. QPushButton *addCar = new QPushButton(tr("&Add Car"));
  30. QPushButton *car1 = new QPushButton(tr("&MX 5"));
  31.  
  32.  
  33. carList->addWidget(car1);
  34. carList->addWidget(addCar);
  35. groupBox->setLayout(carList);
  36.  
  37. return groupBox;
  38.  
  39. }
  40.  
  41. QGroupBox *CarDiary::addCarPage()
  42. {
  43. QGroupBox *groupBox = new QGroupBox();
  44.  
  45. QVBoxLayout *layout = new QVBoxLayout;
  46.  
  47. QLabel *carNameLabel = new QLabel(tr("Car Name:"));
  48. QLineEdit *carNameInput = new QLineEdit();
  49.  
  50. layout->addWidget(carNameLabel);
  51. layout->addWidget(carNameInput);
  52. groupBox->setLayout(layout);
  53.  
  54. return groupBox;
  55. }
To copy to clipboard, switch view to plain text mode