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 to handle a change of the current widget in QStackedWidget?
QtWS25 Last Chance

How to handle a change of the current widget in QStackedWidget?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.9k 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.
  • M Offline
    M Offline
    mingvv
    wrote on 22 Mar 2021, 16:05 last edited by mingvv
    #1

    I found the following signal: void QStackedWidget::currentChanged(int index).
    But I don’t understand how it works or how I use it. Index holds the index of the new current widget. What it means? What am I supposed to give this signal?

    What I want is QStackedWidget, which contains 3 widgets. These 3 widgets are just initialized and added using the function:

    // Example: 
    QWidget *widget1 = new QWidget(parent);
    parent->insertWidget(0, widget1);
    // It’s the same for every widget. 
    

    I don’t want to draw a widget unless it is current. So I need to handle a event changing the current widget to render the widget contents using my function, which will create the layout and elements.
    If current widget is changed, I want to destroy the last one and render a new one.

    J 1 Reply Last reply 22 Mar 2021, 18:03
    0
    • J Offline
      J Offline
      JoeCFD
      wrote on 22 Mar 2021, 16:48 last edited by
      #2

      call setCurrentIndex(int index) to display the one you want to. I do not think you want to destroy any. Otherwise, QStackedWidget is not needed.

      1 Reply Last reply
      2
      • M mingvv
        22 Mar 2021, 16:05

        I found the following signal: void QStackedWidget::currentChanged(int index).
        But I don’t understand how it works or how I use it. Index holds the index of the new current widget. What it means? What am I supposed to give this signal?

        What I want is QStackedWidget, which contains 3 widgets. These 3 widgets are just initialized and added using the function:

        // Example: 
        QWidget *widget1 = new QWidget(parent);
        parent->insertWidget(0, widget1);
        // It’s the same for every widget. 
        

        I don’t want to draw a widget unless it is current. So I need to handle a event changing the current widget to render the widget contents using my function, which will create the layout and elements.
        If current widget is changed, I want to destroy the last one and render a new one.

        J Offline
        J Offline
        JonB
        wrote on 22 Mar 2021, 18:03 last edited by
        #3

        @mingvv
        I have done it before so that the widgets are not created initially until the first time the user visits the page. But as @JoeCFD says, if you then wish to destroy the widget page when the user visits a different page, rather than keeping it around for re-use, then there is not much point using a QStackedWidget at all.

        M 1 Reply Last reply 23 Mar 2021, 06:48
        1
        • J JonB
          22 Mar 2021, 18:03

          @mingvv
          I have done it before so that the widgets are not created initially until the first time the user visits the page. But as @JoeCFD says, if you then wish to destroy the widget page when the user visits a different page, rather than keeping it around for re-use, then there is not much point using a QStackedWidget at all.

          M Offline
          M Offline
          mingvv
          wrote on 23 Mar 2021, 06:48 last edited by mingvv
          #4

          @JonB said in How to handle a change of the current widget in QStackedWidget?:

          @mingvv
          I have done it before so that the widgets are not created initially until the first time the user visits the page.

          How you did it?

          @JonB said in How to handle a change of the current widget in QStackedWidget?:

          @mingvv
          But as @JoeCFD says, if you then wish to destroy the widget page when the user visits a different page, rather than keeping it around for re-use, then there is not much point using a QStackedWidget at all.

          Can you offer another type of widget as a replacement?
          I don’t want to create a new window and delete the old one. Actually, I came from WPF, there are pages for it. So I can switch pages in one window. As I understood QStackedWidget is analogous to pages and I decided that I would be comfortable to pass from WPF using it.
          In principle, I don’t need to save the information when I go back (1<-2, 2<-3 etc.), but I need to pass it forward (0->1, 1->2, 2->3 etc.) through the pages.

          J 1 Reply Last reply 23 Mar 2021, 07:34
          0
          • M mingvv
            23 Mar 2021, 06:48

            @JonB said in How to handle a change of the current widget in QStackedWidget?:

            @mingvv
            I have done it before so that the widgets are not created initially until the first time the user visits the page.

            How you did it?

            @JonB said in How to handle a change of the current widget in QStackedWidget?:

            @mingvv
            But as @JoeCFD says, if you then wish to destroy the widget page when the user visits a different page, rather than keeping it around for re-use, then there is not much point using a QStackedWidget at all.

            Can you offer another type of widget as a replacement?
            I don’t want to create a new window and delete the old one. Actually, I came from WPF, there are pages for it. So I can switch pages in one window. As I understood QStackedWidget is analogous to pages and I decided that I would be comfortable to pass from WPF using it.
            In principle, I don’t need to save the information when I go back (1<-2, 2<-3 etc.), but I need to pass it forward (0->1, 1->2, 2->3 etc.) through the pages.

            J Offline
            J Offline
            JonB
            wrote on 23 Mar 2021, 07:34 last edited by
            #5

            @mingvv said in How to handle a change of the current widget in QStackedWidget?:

            How you did it?

            Looking at the code I didn't actually use QStackedWidget::currentChanged(), though I probably could have done. I had 14 pages in the app, and did not want the slowed start-up time to create them all initially. I have a menu of buttons for visiting the pages. These are QActions, which has triggerred signal, which is the equivalent of QStackedWidget::currentChanged.

            The QStackedWidget is initially populated with "placeholder" (empty) widgets:

            for pageName in self.PageNames:
                page = JPlaceHolder()
                page.setObjectName(str(pageName))
                self.pageStack.addWidget(page)
            

            Connect the QActions:

            for action in self.actions:
                    action.setCheckable(True)
                    self.addAction(action)
                    action.triggered.connect(self.pageChange)
            

            And in the pageChange slot:

            def pageChange(self):
                action = self.sender()
                # find the index in self.actions of the action
                index = self.actions.index(action)
            
                widget = self.pageStack.widget(index)
                # if the widget is still a placeholder it has not been created yet
                if isinstance(widget, JPlaceHolder):
                    name = self.PageNames(index)
                    with ShowWaitCursor():
                        self.createPage(name)
            

            Finally the createPage():

            def createPage(self, name):
                if name == self.PageNames.SearchPage:
                    try:
                        return self.searchPage
                    except AttributeError:
                        pass
                    self.searchPage = page = SearchPage(self)
            
                elif name == self.PageNames....:
                    ...
            
                # Now replace the placeholder widget in the page stack with the actual page created
                # The value of the self.PageNames enumeration gives the position of the page in the page stack
                index = int(name.value)
                widget = self.pageStack.widget(index)
                if isinstance(widget, JPlaceHolder):
                    # if the widget in the page stack is a "placeholder"
                    # we create the actual page now and replace the placeholder with the page
                    self.pageStack.removeWidget(widget)
                    widget.deleteLater()
                    self.pageStack.insertWidget(index, page)
                      
                    return page                
            

            Whether it needs to be as much as this I don't recall. Perhaps you can just fill your QStackedWidget with nullptrs and create the pages in QStackedWidget::currentChanged(int index) when called, or maybe I found this didn't work if the page was nullptr....

            I don’t need to save the information when I go back (1<-2, 2<-3 etc.), but I need to pass it forward (0->1, 1->2, 2->3 etc.) through the pages.

            Have you looked at QWizard? That allows forward navigation through pages. You get backward navigation for free too, though if you don't want that you don't have to implement it/can forbid it. It's just a different kind of multi-page from QStackedWidget.

            1 Reply Last reply
            2

            1/5

            22 Mar 2021, 16:05

            • Login

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