简体   繁体   中英

How to reset an UI in PyQt5

I'm building a layout in pyqt5 where you can dynamically add or delete widgets. Below you can see a simplified version of my problem: after the successful deleting of the nested layout "nested_hbox" and its widgets it won't build up again and just shows an empty window.

Edit: In my real application I have a grid layout where you can dynamically add diagrams in shape of a matrix to view multiple incoming values from an interface. For that you can quantify the number of rows and columns. So, the grid must be refreshed, when the user actuates a button.

import sys
from PyQt5.QtWidgets import *

class Window(QWidget):

    def __init__(self):
        super().__init__()
        self.layouts = []
        self.main_content_ui()
        self.init_ui()

    def main_content_ui(self):
        """Build up content for the main layout "vbox" """

        self.lbl_hello = QLabel()
        self.lbl_hello.setObjectName("lbl_hello")
        self.lbl_hello.setText("Hello StackOverflow")

        self.btn_reset_ui = QPushButton()
        self.btn_reset_ui = QPushButton()
        self.btn_reset_ui.setObjectName("btn_reset_ui")
        self.btn_reset_ui.clicked.connect(self.reset_ui)
        self.btn_reset_ui.setText("Reset UI")

        self.nested_hbox = QHBoxLayout()
        self.nested_hbox.setObjectName("nested_hbox")
        self.nested_hbox.addWidget(self.lbl_hello)
        self.nested_hbox.addWidget(self.btn_reset_ui)
        self.layouts.append(self.layouts)

        # main layout
        self.vbox = QVBoxLayout()
        self.vbox.setObjectName("vbox_main")
        self.layouts.append(self.vbox)
        self.vbox.addLayout(self.nested_hbox)

    def init_ui(self):
        """Set "vbox" as main layout
        """
        self.setLayout(self.vbox)
        self.show()

    def delete_layout(self, layout):
        """Delete all layouts from list "layouts"
        """
        try:
            while layout.count():
                item = layout.takeAt(0) 
                widget = item.widget() 
                if widget is not None: 
                    widget.deleteLater() 
                else: 
                    self.delete_layout(item.layout())
        except Exception as e:
            print(e)

    def reset_ui(self):
        """Clear and reinitalize main layouts content"""

        for lay in self.layouts:
            self.delete_layout(lay)
        print("Layout deleted rebuild layout")
        self.main_content_ui()
        self.vbox.update()
        QMainWindow.update(self)

app = QApplication(sys.argv)
ex = Window()
sys.exit(app.exec_())

I expect the same Window as before clicking the reset-button but the window doesn't show anything.

There's a nice way presented by Qt Wiki on how to make your application restartable https://wiki.qt.io

EXIT_CODE_REBOOT = -11231351

def main():
    exitCode = 0
    while True:
        try: app = QApplication(sys.argv)
        except RuntimeError: app = QApplication.instance()
        window = MainUI()
        window.show()
        exitCode = app.exec_()
        if exitCode != EXIT_CODE_REBOOT: break
    return exitCode

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM