Moving child widgets between QTabWidgets?

David Boddie david at boddie.org.uk
Wed Jan 18 16:23:13 GMT 2023


On Wed Jan 18 13:48:58 GMT 2023, Matic Kukovec wrote:

> I managed to get a self-contained example which throws the same error as my
> development code:

I think you should avoid reparenting the old QTabWidget to None before you
move the pages into the new QTabWidget.

Try this version of the switch method:

    def switch(self, *args):
        widgets = []
        old_tab_widget = self.tab_widget
        for i in reversed(range(old_tab_widget.count())):
            widgets.append((old_tab_widget.widget(i), 
old_tab_widget.tabText(i)))
            old_tab_widget.removeTab(i)

        new_tab_widget = QTabWidget()
        for widget, tab_text in widgets:
            new_tab_widget.addTab(widget, tab_text) # <- ERROR THROWN HERE

        for i in reversed(range(self.centralWidget().count())):
            self.centralWidget().widget(i).setParent(None)
        #old_tab_widget = None
        #self.tab_widget = None

        self.centralWidget().addWidget(new_tab_widget)
        self.tab_widget = new_tab_widget

You also shouldn't need to set the old and new tab widgets to None.

David




More information about the PyQt mailing list