<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div class="elementToProof"><span style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">Hi Maurizio,</span></div>
<div class="elementToProof"><span style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)"><br>
</span></div>
<div class="elementToProof"><span style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">You are correct, I apologize for the mistakes.</span></div>
<div class="elementToProof"><span style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">I managed to get a self-contained example which throws the same error as my development code:</span></div>
<blockquote style="margin-top:0; margin-bottom:0">
<div class="elementToProof"><span class="ContentPasted0" style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
<div style="color:black!important; font-size:12pt; background-color:white!important">
<span class="ContentPasted0"><br>
</span></div>
</span></div>
</blockquote>
<span style="color: rgb(0, 0, 0); font-size: 10pt;">    from PyQt5.QtCore import *</span>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">    from PyQt5.QtGui import *</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">    from PyQt5.QtWidgets import *</span></div>
<div class="ContentPasted4 elementToProof"><span style="color: rgb(0, 0, 0); font-size: 10pt;">   
</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">    class MyWindow(QMainWindow):</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">        def __init__(self):</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            super().__init__()</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            splitter = QSplitter(self)</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            self.setCentralWidget(splitter)</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">           
</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            # Frame & frame</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            frame = QFrame(self)</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            splitter.addWidget(frame)</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            layout = QVBoxLayout()</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            frame.setLayout(layout)</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">           
</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            # Button</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            button = QPushButton("Switch")</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            button.clicked.connect(self.switch)</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            layout.addWidget(button)</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">           
</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            # Tab widget</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            tab_widget = QTabWidget()</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            layout.addWidget(tab_widget)</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            self.tab_widget = tab_widget</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">           
</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            # Red</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            w = QWidget(tab_widget)</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            w.setStyleSheet("background: red;")</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            tab_widget.addTab(w, "Red")</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">           
</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            # Green</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            w = QWidget(tab_widget)</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            w.setStyleSheet("background: green;")</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            tab_widget.addTab(w, "Green")</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">           
</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            # TreeView</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            tv = QTreeView(tab_widget)</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            tv.horizontalScrollbarAction(1)</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            tv.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows)</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            tree_model = QStandardItemModel()</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            tree_model.setHorizontalHeaderLabels(["TREE EXPLORER"])</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            tv.header().hide()</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            tv.setModel(tree_model)</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            for i in range(100):</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">                item = QStandardItem("ITEM {}".format(i))</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">           
</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            tv.setUniformRowHeights(True)</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">           
</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            tab_widget.addTab(tv, "TreeView")</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">       
</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">        def switch(self, *args):</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            widgets = []</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            old_tab_widget = self.tab_widget</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            for i in reversed(range(old_tab_widget.count())):</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">                widgets.append((old_tab_widget.widget(i), old_tab_widget.tabText(i)))</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">                old_tab_widget.removeTab(i)</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">           
</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            for i in reversed(range(self.centralWidget().count())):</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">                self.centralWidget().widget(i).setParent(None)</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            old_tab_widget = None</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            self.tab_widget = None</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">           
</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            new_tab_widget = QTabWidget()</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            for widget, tab_text in widgets:</span></div>
<div class="ContentPasted4 elementToProof"><span style="color: rgb(0, 0, 0); font-size: 10pt;">                new_tab_widget.addTab(widget, tab_text) # <- ERROR THROWN HERE<br>
</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">           
</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            self.centralWidget().addWidget(new_tab_widget)</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">            self.tab_widget = new_tab_widget</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">           
</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">   
</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">    if __name__ == '__main__':</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">        import sys</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">   
</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">        app = QApplication(sys.argv)</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">        w = MyWindow()</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">   
</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">        w.resize(640, 480)</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">        w.show()</span></div>
<div class="ContentPasted4"><span style="color: rgb(0, 0, 0); font-size: 10pt;">   
</span></div>
<span style="color: rgb(0, 0, 0); font-size: 10pt;">        sys.exit(app.exec_())</span>
<blockquote style="margin-top:0; margin-bottom:0">
<div class="elementToProof"><span class="ContentPasted0" style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
<div><span class="ContentPasted0" style="color:black!important"></span></div>
</span></div>
</blockquote>
<div class="elementToProof"><span class="ContentPasted0" style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)"><br>
</span></div>
<div class="elementToProof"><span class="ContentPasted0" style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">Matic<br>
</span></div>
<div class="elementToProof"><span class="ContentPasted0" style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)"><br>
</span></div>
<div id="appendonsend"></div>
<hr tabindex="-1" style="display:inline-block; width:98%">
<div id="divRplyFwdMsg" dir="ltr"><font style="font-size:11pt" face="Calibri, sans-serif" color="#000000"><b>From:</b> Maurizio Berti <maurizio.berti@gmail.com><br>
<b>Sent:</b> Tuesday, January 17, 2023 10:25 PM<br>
<b>To:</b> Matic Kukovec <kukovecmatic@hotmail.com><br>
<b>Cc:</b> PyQt@riverbankcomputing.com <pyqt@riverbankcomputing.com><br>
<b>Subject:</b> Re: Moving child widgets between QTabWidgets?</font>
<div> </div>
</div>
<div>
<div dir="ltr">
<div>If it throws that error, it's because the widgets (or their parent) have been deleted.</div>
<div>Unfortunately, your code is insufficient to actually understand where the issue is, and, by the way, it also has important issues: first of all the second for loop is wrong (new_tab_widget is a widget, so that will throw an exception), and using removeTab
 with the number of a counter is wrong, as you'll always get even numbered items, if you want to remove all widgets, always use removeTab(0).<br>
</div>
<div>I suggest you to provide a valid minimal reproducible example.</div>
<div><br>
</div>
<div>Maurizio<br>
</div>
</div>
<br>
<div class="x_gmail_quote">
<div dir="ltr" class="x_gmail_attr">Il giorno mar 17 gen 2023 alle ore 14:11 Matic Kukovec <<a href="mailto:kukovecmatic@hotmail.com" data-auth="NotApplicable">kukovecmatic@hotmail.com</a>> ha scritto:<br>
</div>
<blockquote class="x_gmail_quote" style="margin:0px 0px 0px 0.8ex; border-left:1px solid rgb(204,204,204); padding-left:1ex">
<div class="x_msg3076459795999726911">
<div dir="ltr">
<div><span style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">HI,</span></div>
<div><br>
</div>
<div>What is the PyQt idiomatic way of moving a widget from one QTabWidget to another?</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
I have tried this:</div>
<blockquote style="margin-top:0px; margin-bottom:0px">
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
<span style="font-family:Calibri,Helvetica,sans-serif; color:rgb(0,0,0)"><br>
</span></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
<span style="font-family:Calibri,Helvetica,sans-serif; color:rgb(0,0,0)">widgets = []</span><br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
<span style="font-family:Calibri,Helvetica,sans-serif; color:rgb(0,0,0)">for i in range(old_tab_widget.count()):</span></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
<div><span style="font-family:Calibri,Helvetica,sans-serif; color:rgb(0,0,0)">    widgets.append((old_tab_widget.widget(i), old_tab_widget.tabText(i)))</span></div>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
<span style="font-family:Calibri,Helvetica,sans-serif; color:rgb(0,0,0)">    old_tab_widget.removeTab(i)</span></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
<br>
</div>
<span style="font-family:Calibri,Helvetica,sans-serif; color:rgb(0,0,0)">for widget, tab_text in new</span><span style="font-family:Calibri,Helvetica,sans-serif; color:rgb(0,0,0)">_tab_widget</span><span style="font-family:Calibri,Helvetica,sans-serif; color:rgb(0,0,0)">:</span>
<div><span style="font-family:Calibri,Helvetica,sans-serif; color:rgb(0,0,0)">   
</span><span style="font-family:Calibri,Helvetica,sans-serif; color:rgb(0,0,0)">new_tab_widget</span><span style="font-family:Calibri,Helvetica,sans-serif; color:rgb(0,0,0)">.addTab(widget, tab_text)</span></div>
</blockquote>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
but this throws errors for example for QTreeView widgets like this:</div>
<blockquote style="margin-top:0px; margin-bottom:0px">
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
<span></span><span>...<br>
</span></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
<span>    new_tab_widget</span><span></span>.addTab(widget, tab_text)<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
RuntimeError: wrapped C/C++ object of type TreeExplorer has been deleted</div>
</blockquote>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
(TreeExplorer is my subclassed QTreeView)<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
Thanks</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">
Matic<br>
</div>
</div>
</div>
</blockquote>
</div>
<br clear="all">
<br>
-- <br>
<div dir="ltr" class="x_gmail_signature">È difficile avere una convinzione precisa quando si parla delle ragioni del cuore. - "Sostiene Pereira", Antonio Tabucchi<br>
<a href="http://www.jidesk.net" data-auth="NotApplicable">http://www.jidesk.net</a></div>
</div>
</body>
</html>