How can I make my window(Widget) restart after switching to another window(Widget) in a QStackedWidget?
-
I have a QStackedWidget with the 0 index being my mainWindow and I have other two windows in it (let's call them window2 and window3). So window2 demands some data from the user and creates a txt file with that data and window3 dose some operations on that data and displays the results. I worked a lot at these two windows so they can work properly(it's my first time trying qt) but I can't find nowhere how to make them refresh/restart.
For example if someone fills half the data in window2 and than clicks on the button to go back to the main window I want everything in window2 to reset(and the same thing with window3 as well). So how do I do this?
P.S. First question on forums so sorry if it's too much or too little info -
Hi and welcome to devnet,
Add a reset function to your widget that you will call when clicking the back button.
-
void SomeClass::resetWindow2() { window2.lineEdit.setText(""); window2.comboBox1.setCurrentIndex(0); ... } void SomeClass::resetWindow3() { window3.spinBox.setValue(0); ... } void SomeClass::stackedWidgetCurrentChanged(int index) { if (index == 0) // changing to index 0 { resetWindow2(); resetWindow3(); } } connect(stackedWidget, &QStackedWidget::currentChanged, this, &SomeClass::stackedWidgetCurrentChanged);
-
@BogdanR
That depends. What else do you have in mind? It's not going to happen "magically". You have about 3 possibilities:-
If you
new
the container window/widget and create its contents yourself then you mightdelete
the old one andnew
it again to get whatever "default" state, e.g. from Designer-generated code. -
You can do explicit resets on each child widget as shown.
-
If you are happy to do a "generic" reset for each
QWidget
type --- e.g. allQLineEdit
s go to""
, allQSpinBox
es go to0
etc. --- you could write that in a function and use QObject::findChildren() to walk down everyQWidget
type calling ti, so you don't have to write individual-widget code.
It may be that instead of your
QStackedWidget
your application would actually better suit a QWizard Class. That is similar to aQStackedWidget
but oriented towards "going through the pages as steps in a procedure". However, I don't think that offers to "reset" all widgets when you back to a previous page, but you could read what it has to say anyway. -