PyQt can't set a value for an object from a dialog in main window
I have a Main program that calls various Dialogs with their own GUIs. Basically what I want to do is to set up a value in Main for an object that is another class:
class ZoneManager(QMainWindow, mainWindow.Ui_zzzMainWindow):
def __init__(self):
QMainWindow.__init__(self)
mainWindow.Ui_zzzMainWindow.__init__(self)
.....
def cookie_find(self):
match = re.search('cookie_id=(.*?)"', page).group(1)
rga = str(match)
print (match)
dialog = QDialog()
dialog.ui = rga_session.Ui_rga_sessionDialog()
dialog.ui.setupUi(dialog)
dialog.exec_()
dialog.ui.rgaSessionText.setText(rga) # <<<<I want to set the text into a QLineEdit object
but I can't. The dialog is in a separate file and made it in QTDesigner with standard 2 methods: from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_rga_sessionDialog(object):
def setupUi(self, rga_sessionDialog):
rga_sessionDialog.setObjectName("rga_sessionDialog")
self.rgaSessionText = QtWidgets.QLineEdit(rga_sessionDialog)
self.rgaSessionText.setGeometry(QtCore.QRect(110, 30, 261, 21))
self.rgaSessionText.setFocusPolicy(QtCore.Qt.ClickFocus)
self.rgaSessionText.setLocale(QtCore.QLocale(QtCore.QLocale.English, QtCore.QLocale.UnitedStates))
self.rgaSessionText.setObjectName("rgaSessionText")
..........
def retranslateUi(self, rga_sessionDialog):
_translate = QtCore.QCoreApplication.translate
.....
Who I can I append that text that I found from Regex into "rgaSessionText" ? What I'm doing wrong? Thanks in advance
I think it's better to communicate between the mainwindow and other dialogs through the signal-slot way.
In your ZoneManager class, define:
settextsignal= pyqtSignal(str)
In your Ui_rga_sessionDialog class, define:
@pyqtSlot(str)
def textUpdate(self, rga):
self.rgaSessionText.setText(rga)
Then in your cookie_find method, after initializing the Ui_rga_sessionDialog, put:
self.settextsignal.connect(dialog.ui.textUpdate)
Then whenever you want to set the text, just call:
self.settextsignal.emit(text)
链接地址: http://www.djcxy.com/p/64494.html