0

Reopen notice

I don't know where else to put this... I will remove it after the question has been reopened. It has been closed (single-handedly) obviously only because I posted a link to another SO thread in the comments. This link however was not targeted to answer this question, but to target an additonal question in the comments. This question has not been answered by the other question but rather by the comment. It is unfair against @icwebndev who answered the question in the comment and should receive the chance to actually really answer the question.


I do have a PySide2/PyQT5 based application that works fine so far. On the main window, when I press a button, a widget (QDialog) is opened which does some initialization steps in its __init__() method.

When this window is closed (using its close()) method, and reopened later, the __init__() is not called anymore, only theshow()` method. Is there a way to reinitialize the Dialog every time its opened?

Edit: When writing the question it occurred to me that I can simply move the relevant code to the show() function. On the other hand, I somehow have the feeling that this it not the intended way to do so.

Below there is the minimized code. Console output when opening the window for the first time is:

Hello_show
Hello_init

The second time its only

Hello_show

mainWindow.py

def startWeighInButtonPressed(self):
    weighInWindow = WeighInWidget(parent=self, app=self.app)
    weighInWindow.show()

weighInWidget.py

class WeighInWidget(QtWidgets.QDialog):

    app = None  # type: QApplication

    def __init__(self, parent=None, app=None):

        # Get application instance
        self.app = app

        # Call parent
        super(WeighInWidget, self).__init__(parent)

        # Do some other init steps
        if initFailed
            error_msg = QtWidgets.QErrorMessage()
            error_msg.showMessage('Some Error message')
            error_msg.exec_()
            self.close()
            return

        # Setup UI
        self.ui = Ui_weighInWidget()
        self.ui.setupUi(self)
        print('Hello_init')

    def show(self):
        print('Hello_show')
        super().show()

    def close(self):
        self.deleteLater()
        super().close()
Daniel
  • 1,398
  • 4
  • 20
  • 47
  • Try with this: use `exec()` (or `exec_()` in PyQt5) instead of `show()` for QDialog subclasses, also, don't override `show()` and `close()` events unles you need to implement something additional happening then. – icwebndev Apr 28 '19 at 11:23
  • @icwebndev thanks, that works so far. I replaced `show()` with `exec_()` and the `__init__()` method is now called every time. However: during the `__init__()` function I do some setup which might or might not work. If it does not work, I am opening the `QErrorMessage` as described above and try to close the Dialog before it actually shows up. I am calling `self.close()` for this, but the Dialog still opens. Do you have an idea why that happens? Please post your response as an answer. – Daniel Apr 28 '19 at 21:57
  • You're calling `close()` method of an object which isn't initiated fully at that point. Try moving that block of code after the setupUi() part. Also, that call should probably be `self.ui.close()`, once you put it after `self.ui.setupUi()` – icwebndev Apr 28 '19 at 22:16
  • I have tried both now - moving it after `self.ui.setupUi()` does not change behavior. And `self.ui()` as no `close()` method. This class `Ui_weighInWidget` is auto-generated by compiling the QTDesigner `*.ui` files to Python and inherits just from object: `class Ui_weighInWidget(object)` – Daniel Apr 29 '19 at 06:29
  • Looks like this part of the question already has an answer here: https://stackoverflow.com/questions/31351116/pyqt-self-close-in-init. Please post your answer to the original question as answer so I can accept it. Thanks for your help! – Daniel Apr 29 '19 at 06:33
  • @eyllanesc I would like to request you to open this question again. The answer I referenced here was only related to an additional question in the comment (I know that should have been a separate one, I'll do it so next time) but does not answer the original question. icwebndev should receive the well-deserved chance to actually answer my question and receive credits for it. – Daniel May 06 '19 at 14:09

0 Answers0