程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

无法关闭对话框 PyQt5

发布于2024-11-06 19:35     阅读(282)     评论(0)     点赞(5)     收藏(2)


我有以下 2 个类。从中我打开一个对话框,如下所示。我希望当我单击这两个按钮之一( , )UI_mainwindow时关闭该对话框。虽然似乎什么都没起作用。createProject_btnopenProject_btn

我知道这类问题已经被问过无数次了,而且我确信肯定有重复的问题,但我对 Python 还很陌生,无法理解其中的规律。因此,任何帮助我都感激不尽。

注意:ui 文件非常简单,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)

创建或打开项目对话框

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()


解决方案


暂无回答



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:https://www.pythonheidong.com/blog/article/2043858/07c4a805f2bf07f010c3/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

5 0
收藏该文
已收藏

评论内容:(最多支持255个字符)