I have a QSplitter
with two widgets in it and want to replace one of them:
import sys
from PySide2.QtWidgets import (QMainWindow, QApplication,
QVBoxLayout, QWidget, QSplitter, QTextEdit, QLabel)
class Example(QWidget):
def __init__(self):
super().__init__()
splitter = QSplitter()
splitter.insertWidget(0, QLabel('test'))
splitter.insertWidget(1, QTextEdit())
print(splitter.count()) # prints 2
splitter.replaceWidget(1, QLabel('something else'))
print(splitter.count()) # prints 1
self.layout = QVBoxLayout(self)
self.layout.addWidget(splitter)
if __name__ == "__main__":
app = QApplication()
main = Example()
main.show()
sys.exit(app.exec_())
My understanding of replaceWidget
is that it will replace an existing widget on index X in the splitter by the provided widget. In the above example on index 0 there's a QLabel('test')
and on index 1 QTextEdit()
.
After trying to replace the widget on index 1, I would expect it to be QLabel('something else')
. But it seems that the widget on index 1 just gets deleted from looking at the output of the count()
before and after the replace operation.
Can anyone see the issue?
question from:
https://stackoverflow.com/questions/66062531/pyside2-qsplitter-replacewidget-not-working-as-expected 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…