1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Cannot close a dialog PyQt5

Discussão em 'Python' iniciado por Stack, Setembro 12, 2024.

  1. Stack

    Stack Membro Participativo

    I have the following 2 classes. From UI_mainwindow I open a dialog as seen below. I want that dialog to close when I click on one of those 2 buttons (createProject_btn, openProject_btn). Though nothing seems to be working.

    I know this kind of questions have been asked million times, and I'm sure there are duplicates, but my eyes are so new to the python, I cannot follow the patterns. Hence, any help is appreciated.

    Note: the ui files are pretty simple, one button on UI_mainwindow to open the dialog, 2 buttons on the dialog to set a property inside and close the dialog.

    Ui_mainWindow

    import os

    from .createOrOpenProjectDialog import CreateOrOpenProjectDialog
    from qgis.PyQt import uic
    from PyQt5 import QtWidgets
    from PyQt5.QtWidgets import (QDialog)

    FORM_CLASS, _ = uic.loadUiType(os.path.join(os.path.dirname(__file__), 'ui', 'window.ui'))

    class Ui_mainWindow(QtWidgets.QDialog, FORM_CLASS):
    def __init__(self, parent=None):
    """Constructor."""
    super(Ui_mainWindow, self).__init__(parent)
    # Set up the user interface from Designer through FORM_CLASS.
    self.setupUi(self)
    self.connectSignalsSlots()

    def connectSignalsSlots(self):
    print("main.connectSignalsSlots 1")
    self.pushButton1.clicked.connect(self.createProject)
    print("main.connectSignalsSlots 2")

    def createProject(self):
    dialog = MyDialog(self)
    print(dialog.exec())
    dialog.showOutput()

    class MyDialog(QDialog):
    def __init__(self, parent=None):
    super().__init__(parent)
    # .dialog is just a handle - class property that we created here
    self.dialog = CreateOrOpenProjectDialog(self)

    def showOutput(self):
    print("showOutput:")
    print(self.dialog.clickedButton)


    CreateOrOpenProjectDialog

    import os

    from qgis.PyQt import uic
    from PyQt5.QtWidgets import (QDialog)

    # This loads your .ui file so that PyQt can populate your plugin with the elements from Qt Designer
    FORM_CLASS, _ = uic.loadUiType(os.path.join(os.path.dirname(__file__), 'ui', 'dialog.ui'))

    class CreateOrOpenProjectDialog(QDialog, FORM_CLASS):
    def __init__(self, parent=None):
    """Constructor."""
    super(CreateOrOpenProjectDialog, self).__init__(parent)
    self.setupUi(parent)
    self.connectSignalsSlots()

    def connectSignalsSlots(self):
    # todo: figure out if actions are better to use then direct events
    # self.action_create_project.triggered.connect(self.createProject)
    self.createProject_btn.clicked.connect(self.createProject)
    self.openProject_btn.clicked.connect(self.openProject)

    def createProject(self):
    print("dialog.createProject")
    self.clickedButton = "createProj"
    self.accept()

    def openProject(self):
    print("dialog.openProject")
    self.clickedButton = "openProj"
    self.accept()

    Continue reading...

Compartilhe esta Página