Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can I make my window(Widget) restart after switching to another window(Widget) in a QStackedWidget?

How can I make my window(Widget) restart after switching to another window(Widget) in a QStackedWidget?

Scheduled Pinned Locked Moved Solved General and Desktop
cppqt6qstackedwidgetqobjectdelete
8 Posts 3 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BogdanR
    wrote on 25 May 2022, 20:01 last edited by
    #1

    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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 25 May 2022, 20:03 last edited by
      #2

      Hi and welcome to devnet,

      Add a reset function to your widget that you will call when clicking the back button.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • B Offline
        B Offline
        BogdanR
        wrote on 26 May 2022, 04:36 last edited by
        #3

        Can you give me a basic code example please?

        J 1 Reply Last reply 26 May 2022, 07:50
        0
        • B BogdanR
          26 May 2022, 04:36

          Can you give me a basic code example please?

          J Offline
          J Offline
          JonB
          wrote on 26 May 2022, 07:50 last edited by
          #4

          @BogdanR

          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);
          
          
          1 Reply Last reply
          1
          • B Offline
            B Offline
            BogdanR
            wrote on 26 May 2022, 09:07 last edited by BogdanR
            #5

            @JonB
            So I have to manually reset the values, containers and classes that are in that window?

            J 1 Reply Last reply 26 May 2022, 09:18
            0
            • B BogdanR
              26 May 2022, 09:07

              @JonB
              So I have to manually reset the values, containers and classes that are in that window?

              J Offline
              J Offline
              JonB
              wrote on 26 May 2022, 09:18 last edited by JonB
              #6

              @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 might delete the old one and new 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. all QLineEdits go to "", all QSpinBoxes go to 0 etc. --- you could write that in a function and use QObject::findChildren() to walk down every QWidget 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 a QStackedWidget 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.

              1 Reply Last reply
              3
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 26 May 2022, 12:52 last edited by
                #7

                To add to @JonB, I was rather thinking of having the reset method implemented directly by each of your custom widget so that they are responsible for getting their internal state back to defaults.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2
                • B Offline
                  B Offline
                  BogdanR
                  wrote on 27 May 2022, 09:31 last edited by
                  #8

                  Ok thanks! I think I understand what I have to do

                  1 Reply Last reply
                  0

                  4/8

                  26 May 2022, 07:50

                  • Login

                  • Login or register to search.
                  4 out of 8
                  • First post
                    4/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved