0

I'm writing some scripts on Python console in QGIS. I have been stuck since a lot of time because I'm not able to recall a MainWindow class object from an other script. I've created a main menu with inside a combobox. The idea is that when the user selects an option from this dropdown list, an other window opens. The problem is that this window opens for just a moment and then closes itself. Here the code.

this is the main menu's script (proofRequest.py) (not whole)

import sys 
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import proofRequest as pr

def startButton(self):
       
        
        x = self.comboBox.currentIndex()
        
        if x == 3:
           win = pr.MainWindow()
           win.show()

While this is the script of the window which I would open (always not whole)

import sys
from PyQt5.QtWidgets import *
from qgis.utils import iface 
from PyQt5.QtCore import *
from PyQt5.QtGui import *


class MainWindow(QMainWindow):
    
    def __init__(self, parent=None, *args):
        
        
        super(MainWindow, self).__init__(parent = parent)
        self.setWindowTitle("QDialog Example")
        self.resize(350, 300)
        #layout = QVBoxLayout()
        print("sono qui")
        self.button1Ok = QPushButton("Ok", self)
        self.button2Annulla = QPushButton("Annulla", self)
        self.button1Ok.setGeometry(30,180,130,40)
        self.button2Annulla.setGeometry(180,180,130,40)
        label = QLabel("Seleziona un comune con edifici, poi clicca su ok", self)
        label.setAlignment(Qt.AlignHCenter)
        label.setGeometry(10, 50, 330, 30)
        self.button1Ok.clicked.connect(self.avvia)
        self.button2Annulla.clicked.connect(self.cancella)

I tried also to use:

qgs = QgsApplication([], True)
qgs.initQgis()
exitcode = qgs.exec()
QgsApplication.exitQgis()
sys.exit(exitcode)

But QGIS crashes...

3
  • 2
    This is a basic problem of scope. You are assigning your QmainWindow instance to a variable with function level scope so it goes out of scope as soon as the startButton() method returns. You need to store a reference to the main window object with class level scope. In the constructor of the class to which the startButton() method belongs, define self.win = None, then in startButton() do: self.win = pr.MainWindow() self.win.show().
    – Ben W
    Commented Aug 11 at 7:04
  • 2
    Side Note: you should fix your imports- you are importing 3 entire libraries then doing wildcard imports from those same libraries. P.s. if you start a QgsApplication inside an already running QgsApplication then yes, it will crash! That code is for creating a standalone app e.g. from command line or an IDE and should NOT be run from the Python console.
    – Ben W
    Commented Aug 11 at 7:05
  • You saved my life!!! :-) Commented Aug 11 at 14:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.