PyQt5 信号与槽 多窗口传递中遇到的一点问题

关键词:python,PyQt5,信号与槽,多窗口

楼主编程业余萌新一枚,非软件相关行业从事人员,最近自学中遇到一点问题,请c站的各位大神点播一二,叩谢TAT。

这个程序想要实现的功能很简单:在主窗口内输入内容,点击主窗口内的按钮后,弹出对话窗口,并在对话窗口内显示主窗口内输入的内容

现在遇到的问题是主要有两个:

  • 点击主窗口的按钮后,对话窗口只闪现,不能维持;
  • 保持主窗口不关闭,多次点击按钮时,每点击一次按钮,对话窗口闪现的次数等于第几次点击按钮,比如说,第五次点击按钮,对话窗口就会闪现五次。

关于代码实现方式的一点说明:我把主窗口的信号与对话窗口的类直接连接起来,问题可能是出在这里,但是搞不懂为什么不行

代码如下:

  1. 主窗口控制:test_mainwindow_ctrl.py
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from test_mainwindow import *
from test_dialog_ctrl import Test_dialog_ctrl

class Test_mainwindow_ctrl(QMainWindow, Ui_MainWindow_test):

    signal_sth = pyqtSignal(str)

    def __init__(self, parent=None):
        super(Test_mainwindow_ctrl, self).__init__(parent)
        self.setupUi(self)

        self.pushButton.clicked.connect(self.show_dialog)

    # @pyqtSlot()
    def show_dialog(self):
        signal_sth = self.textEdit.toPlainText()
        self.signal_sth.emit(signal_sth)
        self.signal_sth.connect(Test_dialog_ctrl)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainwindow = Test_mainwindow_ctrl()
    mainwindow.show()
    sys.exit(app.exec_())
  1. 对话窗口控制:test_dialog_ctrl.py
from PyQt5.QtWidgets import *

from test_dialog import Ui_Dialog_test

class Test_dialog_ctrl(QDialog,Ui_Dialog_test):
    # @pyqtSlot()
    def __init__(self, signal_sth, parent=None):
        super(Test_dialog_ctrl, self).__init__(parent)
        self.setupUi(self)
        print(signal_sth)
        self.textEdit.setPlainText(signal_sth)
        self.show()
  1. 主窗口结构(Qt Designer转化):test_mainwindow.py
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow_test(object):
    def setupUi(self, MainWindow_test):
        MainWindow_test.setObjectName("MainWindow_test")
        MainWindow_test.resize(542, 174)
        self.centralwidget = QtWidgets.QWidget(MainWindow_test)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setObjectName("pushButton")
        self.gridLayout.addWidget(self.pushButton, 1, 0, 1, 1)
        self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit.setObjectName("textEdit")
        self.gridLayout.addWidget(self.textEdit, 0, 0, 1, 1)
        MainWindow_test.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow_test)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 542, 21))
        self.menubar.setObjectName("menubar")
        MainWindow_test.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow_test)
        self.statusbar.setObjectName("statusbar")
        MainWindow_test.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow_test)
        QtCore.QMetaObject.connectSlotsByName(MainWindow_test)

    def retranslateUi(self, MainWindow_test):
        _translate = QtCore.QCoreApplication.translate
        MainWindow_test.setWindowTitle(_translate("MainWindow_test", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow_test", "PushButton"))
        self.textEdit.setHtml(_translate("MainWindow_test", "\n"
"\n"
"

test message:hello world!

"
))
  1. 对话窗口结构(Qt Designer转化):test_dialog.py
from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Dialog_test(object):
    def setupUi(self, Dialog_test):
        Dialog_test.setObjectName("Dialog_test")
        Dialog_test.resize(415, 173)
        self.gridLayout = QtWidgets.QGridLayout(Dialog_test)
        self.gridLayout.setObjectName("gridLayout")
        self.buttonBox = QtWidgets.QDialogButtonBox(Dialog_test)
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName("buttonBox")
        self.gridLayout.addWidget(self.buttonBox, 1, 0, 1, 1)
        self.textEdit = QtWidgets.QTextEdit(Dialog_test)
        self.textEdit.setObjectName("textEdit")
        self.gridLayout.addWidget(self.textEdit, 0, 0, 1, 1)

        self.retranslateUi(Dialog_test)
        self.buttonBox.accepted.connect(Dialog_test.accept)
        self.buttonBox.rejected.connect(Dialog_test.reject)
        self.buttonBox.accepted.connect(Dialog_test.close)
        self.buttonBox.rejected.connect(Dialog_test.close)
        QtCore.QMetaObject.connectSlotsByName(Dialog_test)

    def retranslateUi(self, Dialog_test):
        _translate = QtCore.QCoreApplication.translate
        Dialog_test.setWindowTitle(_translate("Dialog_test", "Dialog"))
        self.textEdit.setHtml(_translate("Dialog_test", "\n"
"\n"
"


"
))

你可能感兴趣的:(python,pyqt,qt)