3

I'm trying to figure out why the window is closing after opening. I looked at examples of why this has happened for others but my application is set up a little differently (possibly a newer version of pyqt than the ones I was looking at or because I have it launching from within a tool bar menu).

Below is the main parts of the code that launch the first window of type Class1Base and which launch but lose the second window NewWindow (had to alter names for anonymity).

In ToolbarMenuSetup.py:

class ToolbarMenuSetup(object):
   # only including call to the below for brevity
   def clickOpenClass1(self):
      self.config = self.toolbarConfig()
      retval = Class1(self.class1_dlg, self.config)

In Class1.py:

class Class1(Class1Base):
   def __init__(self, dlg, config):
      self.dlg = dlg
      self.iface = self.dlg.iface
      self.config = config

      self.dlg.ui.btnOpenWindow.clicked.connect(self.openNewWindow)
      self.run()

In Class1Base.py:

class NewWindow(Qt.QDialog, UI_NewWindowBase):
   def __init__(self, ToolbarMenuSetup):
      QtGui.QDialog.__init__(self)
      self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
      self.setupUi(self)

class Class1Base():
   def __init__(self, dlg, config):
      self.dlg = dlg
      self.iface = self.dlg.iface
      self.run()

   def openNewWindow(self):
      window = NewWindow(self)
      window.show()

   def run(self):
      self.dlg.show()
      result = self.dlg.exec_()
      if(result):
         performCalculations() 
         # this function not included for brevity

Any help is greatly appreciated!

Resolved by:
- added self.window = None into run()
- added self. in front of the lines in openNewWindow()

  • From what I've seen in other similar issues it seems like I'm losing the reference to the new window and that's why it's immediately closing. I just can't seem to figure out where I should be keeping that reference so that I don't lose it since the Class1 calls directly to the run function of Class1Base – InsertDisplayName Sep 14 '17 at 17:14
  • you can try to assign it to the class `Class1Base`, with `self.window = NewWindow(self)` not sure it helps a lot – PRMoureu Sep 14 '17 at 17:17
  • @PRMoureu within one of the functions or no? I tried to put it in the run function but still had the same issue (opens but closes right away) – InsertDisplayName Sep 14 '17 at 18:22
  • in the method `openNewWindow` to replace `window = NewWindow(self)`...(need to replace `self.window.show()` too) – PRMoureu Sep 14 '17 at 18:38
  • 1
    @PRMoureu I forgot to get back to you about this. I tried a few things based on that comment and it ended up working (will edit post with what changed in case someone needs it later). Thank you! – InsertDisplayName Sep 18 '17 at 18:11

0 Answers0