如何将信号从QDialog发送到QMainWindow类

时间:2017-10-07 16:52:44

标签: multithreading python-3.x signals pyqt5 signals-slots

我创建了一个UI,其中包含一个主窗口和一个打开对话窗口的按钮。我想将当前文本更改的信号从对话框窗口中的行编辑发送到Mainwindow类作为变量。我正在制作的代码示例如下:

from PyQt5 import QtCore, QtGui, QtWidgets
from selenium import webdriver
import time
import threading
from bs4 import BeautifulSoup as soup
import requests

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(388, 179)
        self.lineEdit_2 = QtWidgets.QLineEdit(Dialog)
        self.lineEdit_2.setGeometry(QtCore.QRect(100, 100, 271, 21))
        font = QtGui.QFont()
        font.setFamily("Yu Gothic")
        self.lineEdit_2.setFont(font)
        self.lineEdit_2.setStyleSheet("background-color: transparent;\n"
"color: rgb(255, 255, 255);")
        self.lineEdit_2.setObjectName("lineEdit_2")

class Dialog(QtWidgets.QDialog, Ui_Dialog):
    def __init__(self, parent=None):
        QtWidgets.QDialog.__init__(self, parent)
        self.setupUi(self)

       # self.pushButton.pressed.connect(self.textEdit.clear)
       # self.pushButton.pressed.connect(self.sejd)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        self.keyworddict = {}
        self.count = {}
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(803, 538)
        MainWindow.setMinimumSize(QtCore.QSize(0, 0))
        MainWindow.setMaximumSize(QtCore.QSize(10000, 10000))
        self.centralWidget = QtWidgets.QWidget(MainWindow)
        self.centralWidget.setObjectName("centralWidget")
        self.pushButton = QtWidgets.QPushButton(self.centralWidget)
        self.pushButton.setGeometry(QtCore.QRect(180, 210, 75, 23))
        font = QtGui.QFont()
        font.setFamily("Yu Gothic")
        font.setBold(True)
        font.setWeight(75)
        self.pushButton.setFont(font)
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralWidget)

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        self.setupUi(self)

        self.pushButton.pressed.connect(self.on_Button_clicked)

    def on_Button_clicked(self):
        dialog = QtWidgets.QDialog()
        dialog.ui = Ui_Dialog()
        dialog.ui.setupUi(dialog)
        dialog.setWindowTitle("Login")
        dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        dialog.exec_()

if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.setWindowTitle("ui")
    w.show()
    sys.exit(app.exec_())

所有帮助表示赞赏。我最好喜欢使用上面代码的例子。

1 个答案:

答案 0 :(得分:1)

至少有两种简单的方法可以做到这一点。您可以在创建对话框时连接信号,也可以在对话框关闭后检索文本:

Function getBlanksInListCount(ws As Worksheet, Optional FirstRow = 2, Optional TestColumn = 2)
    With ws
        getBlanksInListCount = WorksheetFunction.countblank(.Range(.Cells(FirstRow, TestColumn), .Cells(.Rows.Count, TestColumn).End(xlUp)))
    End With
End Function

如果这是一个登录对话框,您可能应该有一个连接到对话框alertDialog.setMessage(Html.fromHtml("Are you sure"+"\n"+"\n"+"<font color='#00bfff'><br><br><b>Est. Total Cost : $ <var>"+ estCost+" </var></b></font>",Build.VERSION.SDK_INT)); 插槽的接受按钮。然后你可以像这样检查对话框的退出状态:

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    ...

    def dialogTextChanged(self, text):
        print(text)

    def on_Button_clicked(self):
        dialog = QtWidgets.QDialog()
        dialog.ui = Ui_Dialog()
        dialog.ui.setupUi(dialog)
        # connect signal to slot
        dialog.ui.lineEdit_2.textChanged.connect(self.dialogTextChanged)
        dialog.setWindowTitle("Login")
        # this is not needed
        # dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        dialog.exec_()
        # or retrieve text after dialog closes
        print(dialog.ui.lineEdit_2.text())
        dialog.deleteLater()