Results 1 to 7 of 7

Thread: how to change the index of QstackedWidget from a contained widget

  1. #1
    Join Date
    Feb 2013
    Location
    Banzart
    Posts
    54
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default how to change the index of QstackedWidget from a contained widget

    I have a QStackedWidget that contains 2 layers of widgets(widget inside another widget). The final widget contains a multiplicity of buttons. How can i change the index of that QstackedWidget when i press one of those buttons ?? i should mention that the contained widget in the QstackedWidget is a separated widget so it does have it's own .cpp .h and .ui files
    Last edited by sliverTwist; 23rd March 2013 at 21:57. Reason: updated contents
    Sliver_Twist

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to change the index of QstackedWidget from a contained widget

    setCurrentIndex() is a slot, so make the contained widget emit a signal that carries the desired index and connect it to the slot of the stacked widget instance.

    Simply ways to map multiple buttons to different integer values are QButtonGroup and QSignalMapper

    Cheers,
    _

  3. #3
    Join Date
    Feb 2013
    Location
    Banzart
    Posts
    54
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to change the index of QstackedWidget from a contained widget

    Quote Originally Posted by anda_skoa View Post
    setCurrentIndex() is a slot, so make the contained widget emit a signal that carries the desired index and connect it to the slot of the stacked widget instance.

    Simply ways to map multiple buttons to different integer values are QButtonGroup and QSignalMapper

    Cheers,
    _
    Well sir i didn't understand what you said ?? Did you understand my question ?? And could you give me an example of what you just mentioned ??
    Sliver_Twist

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,243
    Thanks
    303
    Thanked 866 Times in 853 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to change the index of QstackedWidget from a contained widget

    What he is saying is this:

    - Your button has a signal, clicked().
    - In the widget class that contains the button, create a slot and name it anything you want. I will name it onSwitchPageClicked().
    - In the constructor for the widget class, connect the button's clicked() signal to the onSwitchPageClicked() slot.
    - In this slot, call setCurrentIndex() for the stacked widget with the index of the page you want to change to.


    Please note - it does not matter at all what the layout of code in your project's .cpp and .h files is, so it is unimportant to mention that as part of your post.

  5. #5
    Join Date
    Feb 2013
    Location
    Banzart
    Posts
    54
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to change the index of QstackedWidget from a contained widget

    Sorry still not clear enough

    This is the code of my widget

    Qt Code:
    1. #include "loginwidget.h"
    2. #include "ui_loginwidget.h"
    3.  
    4. #include <QDesktopServices>
    5. #include <QUrl>
    6. LoginWidget::LoginWidget(QWidget *parent) :
    7. QWidget(parent),
    8. ui(new Ui::LoginWidget)
    9. {
    10. ui->setupUi(this);
    11. }
    12.  
    13. LoginWidget::~LoginWidget()
    14. {
    15. delete ui;
    16. }
    17.  
    18.  
    19. void LoginWidget::on_NewpushButton_clicked()
    20. {
    21. QDesktopServices::openUrl(QUrl("http://www.google.com", QUrl::TolerantMode));
    22. }
    23.  
    24. void LoginWidget::on_ConfigurepushButton_clicked()
    25. {
    26. // Action an button clicked
    27. }
    To copy to clipboard, switch view to plain text mode 

    I should mention that to show this widget in the stacked Widget i have implemented inside the stacked widget a widget that I've promoted to LoginWidget
    could you help me modifying this code i am still a beginner in Qt
    Sliver_Twist

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to change the index of QstackedWidget from a contained widget

    1) add a signal to LoginWidget in loginwidget.h, something like void changeStackedWidgetIndex(int index);
    2) in the appropriate slot emit it with the desired index
    3) in your stacked widget class, connect the new signal to the setCurrentIndex slot, something like this
    Qt Code:
    1. connect( ui->loginWidget, SIGNAL(changeStackedWidgetIndex(int)), this, SLOT(setCurrentIndex(int)) );
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    sliverTwist (25th March 2013)

  8. #7
    Join Date
    Feb 2013
    Location
    Banzart
    Posts
    54
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to change the index of QstackedWidget from a contained widget

    thank you my problem was solved
    Last edited by sliverTwist; 25th March 2013 at 12:14.
    Sliver_Twist

Similar Threads

  1. How to change QStackedWidget index from Qdialog
    By sliverTwist in forum Qt Programming
    Replies: 5
    Last Post: 20th March 2013, 20:24
  2. Change QVBoxLayout index?
    By davidlamhauge in forum Qt Programming
    Replies: 2
    Last Post: 27th April 2012, 01:51
  3. Replies: 3
    Last Post: 3rd March 2011, 01:22
  4. Replies: 2
    Last Post: 8th February 2011, 19:07
  5. Replies: 3
    Last Post: 30th December 2010, 16:34

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.