Results 1 to 2 of 2

Thread: how can i change the index of qstackedwidget form a button of the contianed widget

  1. #1
    Join Date
    Jun 2018
    Qt products

    Default how can i change the index of qstackedwidget form a button of the contianed widget

    please help.

  2. #2
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 678 Times in 668 Posts
    Qt products
    Windows Android

    Default Re: how can i change the index of qstackedwidget form a button of the contianed widge

    You have at least two choices:

    1 - Derive from QStackedWidget and implement a slot that listens for a signal from the contained widget. When the button is clicked, the contained widget emits this custom signal.
    In other words, contained widget has a slot that listens for button clicked. That slot emits a new signal. The custom QStackedWidget has a slot that is connected to that signal. When it receives the signal, it switches pages.

    2 - Pass a pointer to the QStackedWidget into the contained widget (in the constructor or with a custom method). In the slot that handles the button click, change the page using this pointer.

    Option 2 is usually considered a bad idea, because it breaks the encapsulation of the contained widget. This widget shouldn't really know that it is contained in a stacked widget. By giving it a pointer to the stacked widget, it prevents it from being used in any other way - like in a QScrollArea, a layout, or some other arrangement. If I were doing it, I would choose option 1, where the widget listening for the signal only knows it is connected to something that can issue a signal telling it to change pages. It doesn't even need to know it is a button click that initiates the chain of actions that results in the signal.

    By the way, it would also be a mistake for the "changeThePage()" signal to specify -which- page to change to. Suppose you hard-coded "1" as the new page number. What would happen if you inserted a new page before page 1? Better to have a signal "changeToEditor()" or whatever, and let the stack widget decide which page number that corresponds to.

    Again, it is all about encapsulation. No part of your program needs to know any details about another part, except for the details that are necessary for it to work.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 6
    Last Post: 25th March 2013, 11:14
  2. Qstackedwidget form strange cut
    By rspock in forum Newbie
    Replies: 5
    Last Post: 24th March 2013, 19:43
  3. How to change QStackedWidget index from Qdialog
    By sliverTwist in forum Qt Programming
    Replies: 5
    Last Post: 20th March 2013, 20:24
  4. QStackedWidget Button menu
    By kewal.deshpande in forum Qt Programming
    Replies: 2
    Last Post: 14th June 2011, 08:13
  5. Replies: 5
    Last Post: 12th March 2010, 22:43

Tags for this Thread


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.