Reputation: 452
I am trying to do some modifications to a GUI. My GUI is built on a QWidget. Initially I had just a QPushButton in the QWidget which then got deleted and was replaced by a QGridlayout housing a bunch of other stuff. Now I want to initially have two buttons held in a QVBoxLayout which get deleted and the QVBoxLayout gets deleted and/or replaced with the QGridLayout which then houses the next items.
The problem: I can't delete and/or replace the QVBosLayout with the QGridLayout.
Minimal reproducible example below. You need PyQt5 to run
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form():
def __init__(self):
self.nCode_analysis_set_up = QtWidgets.QWidget()
self.nCode_analysis_set_up.resize(300, 100)
self.nCode_analysis_set_up.setWindowFlags(self.nCode_analysis_set_up.windowFlags() | QtCore.Qt.MSWindowsFixedSizeDialogHint)
self.nCode_analysis_set_up.setWindowTitle("nCode analysis set-up")
self.Box = QtWidgets.QVBoxLayout(self.nCode_analysis_set_up)
self.importButton = QtWidgets.QPushButton(self.nCode_analysis_set_up)
self.importButton.setText("Open import model")
self.importButton.clicked.connect(self.input_model)
self.Box.addWidget(self.importButton)
def input_model(self):
self.importButton.deleteLater()
self.Box.deleteLater()
self.Box = QtWidgets.QGridLayout(self.nCode_analysis_set_up)
self.analysis_type_label = QtWidgets.QLabel(self.nCode_analysis_set_up)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
ui = Ui_Form()
ui.nCode_analysis_set_up.show()
sys.exit(app.exec_())
Upvotes: 1
Views: 81
Reputation: 243897
Do not delete widgets since it usually brings more problems than benefits, instead use a QStackWidget or QStackedLayout:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form:
def __init__(self):
self.nCode_analysis_set_up = QtWidgets.QWidget()
self.nCode_analysis_set_up.resize(300, 100)
self.nCode_analysis_set_up.setWindowFlags(
self.nCode_analysis_set_up.windowFlags()
| QtCore.Qt.MSWindowsFixedSizeDialogHint
)
self.nCode_analysis_set_up.setWindowTitle("nCode analysis set-up")
self.stacked = QtWidgets.QStackedLayout(self.nCode_analysis_set_up)
widget1 = QtWidgets.QWidget()
box_1 = QtWidgets.QVBoxLayout(widget1)
self.importButton = QtWidgets.QPushButton()
self.importButton.setText("Open import model")
self.importButton.clicked.connect(self.input_model)
box_1.addWidget(self.importButton)
widget2 = QtWidgets.QWidget()
box_2 = QtWidgets.QGridLayout(widget2)
self.analysis_type_label = QtWidgets.QLabel()
self.analysis_type_label.setText("Label")
box_2.addWidget(self.analysis_type_label)
self.stacked.addWidget(widget1)
self.stacked.addWidget(widget2)
def input_model(self):
self.stacked.setCurrentIndex(1)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
ui = Ui_Form()
ui.nCode_analysis_set_up.show()
sys.exit(app.exec_())
Upvotes: 1