Results 1 to 7 of 7

Thread: Help with QStackedWidget

  1. #1
    Join Date
    Nov 2009
    Posts
    4
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Help with QStackedWidget

    Hi everyone!!
    I'm having some problems with a QStackedWidget and I though maybe you can help me. The program is a QWidget which contains a QStackedWidget and two QPushButtons (one, to change page; and another, to quit). The problem is that the "Change" button doesn't work and I have no idea why...
    Here's the relevant code...
    Qt Code:
    1. MenuManager::MenuManager(QWidget *parent): QWidget(parent)
    2. {
    3. menus = new QStackedWidget;
    4. menus->addWidget( m1 = new MenuOne );
    5. menus->addWidget( m2 = new MenuTwo );
    6. menus->addWidget( m3 = new MenuThree);
    7. menus->addWidget( m4 = new MenuFour );
    8. menus->addWidget( m5 = new MenuFive );
    9.  
    10. /*menus->setCurrentIndex(3);*/ // Works fine, which means Menus' code is allright
    11.  
    12. QPushButton *bChange = new QPushButton("Change");
    13. QObject::connect(bChange, SIGNAL(clicked()), menus, SLOT(setCurrentIndex(3)));
    14.  
    15. QPushButton *bQuit = new QPushButton("Quit");
    16. QObject::connect(bQuit, SIGNAL(clicked()), this, SLOT(close()));
    17.  
    18. QVBoxLayout *layout = new QVBoxLayout;
    19. layout->addWidget(menus);
    20. layout->addWidget(bChange);
    21. layout->addWidget(bQuit);
    22. setLayout(layout);
    23. }
    To copy to clipboard, switch view to plain text mode 

    Any help will be apreciated. Thanks!

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Help with QStackedWidget

    this connection is wrong
    Qt Code:
    1. QObject::connect(bChange, SIGNAL(clicked()), menus, SLOT(setCurrentIndex(3)));//3 -- wrong
    To copy to clipboard, switch view to plain text mode 
    .
    read carefully about signal&slots
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. The following user says thank you to spirit for this useful post:

    onírico (12th November 2009)

  4. #3
    Join Date
    Nov 2009
    Posts
    4
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with QStackedWidget

    Thank for spending your time reading my code, spirit.
    I´ve read about signals and spots but I still haven't found the point. Could you give more clues?? I supose I have to define a slot function to change the page. I´m not sure how.

  5. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Help with QStackedWidget

    you is not careful.
    your connection is
    Qt Code:
    1. QObject::connect(bChange, SIGNAL(clicked()), menus, SLOT(setCurrentIndex(3)));
    To copy to clipboard, switch view to plain text mode 
    it's not correct, because passing real values in connection IS NOT allowed.
    so, your connection must look like
    Qt Code:
    1. QObject::connect(bChange, SIGNAL(clicked()), menus, SLOT(setCurrentIndex(int)));
    To copy to clipboard, switch view to plain text mode 
    i.e. you should specify SIGNATURE of method.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. The following user says thank you to spirit for this useful post:

    onírico (12th November 2009)

  7. #5
    Join Date
    Nov 2009
    Posts
    4
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with QStackedWidget

    Ok. But now, how do I link that button with the MenuThree?? Do I have to use QSignalMapper??
    what is exactly the signature (of a signal or slot)???

    Thank you again, spirit!! And sorry for being worse than a headache.

  8. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Help with QStackedWidget

    add a slot which changes pages:
    Qt Code:
    1. ....
    2. QObject::connect(bChange, SIGNAL(clicked()), this, SLOT(changePage()));
    3. ....
    4. void MyWidget::changePage()
    5. {
    6. int nextPage = menus->currentIndex() + 1;
    7. if (nextPage >= menus->count())
    8. nextPage = 0;
    9. menus->setCurrentIndex(nextPage);
    10. }
    11. ...
    To copy to clipboard, switch view to plain text mode 
    should be something like this.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  9. The following user says thank you to spirit for this useful post:

    onírico (12th November 2009)

  10. #7
    Join Date
    Nov 2009
    Posts
    4
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help with QStackedWidget

    That wasn´t exactly what I wanted. But it helped me a lot to solve my problem.

    Thank so much, spirit!!!

Similar Threads

  1. QStackedLayout / QStackedWidget
    By morraine in forum Newbie
    Replies: 15
    Last Post: 12th July 2013, 09:16
  2. Replies: 4
    Last Post: 13th August 2007, 15:28
  3. QStackedWidget fill Postscript image And scroll problem
    By patrik08 in forum Qt Programming
    Replies: 11
    Last Post: 22nd April 2007, 09:30
  4. QStackedLayout vs. QStackedWidget
    By bruccutler in forum Qt Programming
    Replies: 1
    Last Post: 12th March 2007, 23:43
  5. parsing QtableWidget from a QStackedWidget
    By rosmarcha in forum Qt Programming
    Replies: 10
    Last Post: 13th October 2006, 14:36

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.