Results 1 to 4 of 4

Thread: connecting button to stacked layout

  1. #1
    Join Date
    May 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default connecting button to stacked layout

    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 

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: connecting button to stacked layout

    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

    Qt Code:
    1. class CarDiary {
    2. ...
    3. private slot:
    4. void selectCar(void);
    5. ...
    6. private:
    7. QStackedLayout *stackedLayout;
    8. };
    9.  
    10. CarDiary::CarDiary(QWidget *parent)
    11. : QWidget(parent)
    12. {
    13. stackedLayout = new QStackedLayout;
    14. ...
    15. }
    16.  
    17. CarDiary::selectCar(void)
    18. {
    19. stackedLayout->setCurrentIndex(1);
    20. }
    21.  
    22. QGroupBox *CarDiary::carSelectionPage()
    23. {
    24. ...
    25. connect(addCar, SIGNAL(clicked()), this, SLOT(selectCar())); //add this after creating addCar
    26. return groupBox;
    27. }
    To copy to clipboard, switch view to plain text mode 

    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.

  3. #3
    Join Date
    May 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: connecting button to stacked layout

    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.

    Quote Originally Posted by Santosh Reddy View Post
    There is no alternative...
    I meant is there an alternative to the stacked layout with group boxes, but this is working well.

    Thankyou

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: connecting button to stacked layout

    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.

Similar Threads

  1. Connecting Button with Quit-Signal
    By moatilliatta in forum Qt Programming
    Replies: 1
    Last Post: 11th October 2010, 16:19
  2. Replies: 4
    Last Post: 27th November 2009, 13:00
  3. Stacked Layout. & Widget.
    By csvivek in forum Qt Programming
    Replies: 1
    Last Post: 13th May 2008, 08:56
  4. Connecting signal to button
    By steg90 in forum Qt Programming
    Replies: 8
    Last Post: 13th December 2007, 10:37
  5. Button in Layout help
    By IsleWitch in forum Newbie
    Replies: 4
    Last Post: 9th October 2007, 16:59

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.