PDA

View Full Version : how can i change the index of qstackedwidget form a button of the contianed widget



nearrainbow
10th June 2018, 19:42
please help.

d_stranz
10th June 2018, 20:22
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.