简体   繁体   中英

How can I set text of a qlineedit inside a mdi sub window from another class?

I am using python and pyqt6.

I have a maindwindow A, it has a mdi area with a subwindow havig a qlineedt and a button, when i press the button a pop up window appears which is another class from within the same program, the popup window has qlineedit and a button, when i enter a value in the qlineedit and press the button, the popup window closes and the value of the qlineedit is transferred to the qlineedit of the subwindow, I tried this code:

A.subwindow.settext(self.textedit.text())

but it does not work. The error is "qmainwindow A has no attribute subwindow" I also tried this:

A.mdi.subwindow.settext(self.textedit.text())

And the error is "qmainwindow A has no attribute mdi" I declared mdi as:

self.mdi = QMdiArea()
windowLayout.addWidget(self.mdi)

And subwindow as :

self.subwindow = QMdiSubWindow()
self.mdi.addSubWindow(self.subwindow)
self.subwindow.show()  

Here's the minimal code that produces the same error:

import PyQt6.QtWidgets   as qtw
import PyQt6.QtCore as qtc
import sys 
import os 

class Minwindow(qtw.QMainWindow): 
    def __init__(self):
        super().__init__()
        self.mdi = qtw.QMdiArea()
        widget = qtw.QWidget()
        self.setCentralWidget(widget)
        windowLayout = qtw.QHBoxLayout(widget)
        windowLayout.addWidget(self.mdi, )
        
        pbSub = qtw.QPushButton('Sub', self)
        pbSub.setGeometry(9, 9, 75, 20)
        pbSub.clicked.connect(self.on_click_sub)
        self.setGeometry(125,75,350,300)
    def on_click_sub(self):
        self.subw = qtw.QMdiSubWindow()
        self.subw.txtusr = qtw.QLineEdit('', self.subw)
        self.subw.txtusr.setGeometry(25,30,100,25)
        pbPop = qtw.QPushButton('Pop', self.subw)
        pbPop.setGeometry(50, 75, 75, 20)
        pbPop.clicked.connect(self.on_click_pop)
        self.subw.setGeometry(75, 75, 200, 150)
        self.subw.setWindowTitle("Create User")
        self.mdi.addSubWindow(self.subw)
        self.subw.show() 
    def on_click_pop(self):        
        self.showPopup()
    def showPopup(self):
        name = 'POPS'
        self.dpop = Popw(name)        
        self.dpop.move(self.pos().x()+75, self.pos().y()+75)
        self.dpop.setFixedSize(200, 150)
        self.dpop.show()   
class Popw(qtw.QMainWindow):
    def __init__(self, name):
        super().__init__()        
        self.name = name
        #self.setGeometry(100, 100, 300, 350)
        self.setWindowFlags(qtc.Qt.WindowType.Window | qtc.Qt.WindowType.CustomizeWindowHint | qtc.Qt.WindowType.WindowStaysOnTopHint)
        self.initUI()
    def initUI(self):
        ntxtusr = qtw.QLineEdit('', self)
        ntxtusr.setGeometry(25,30,100,25)
        pbOk = qtw.QPushButton('Ok', self)
        pbOk.setGeometry(50, 75, 75, 20)
        pbOk.clicked.connect(self.on_click_ok) 
    def on_click_ok(self):
        Minwindow.mdi.subw.txtusr.setText(ntxtusr.text())
        self.close()
    
if __name__ == "__main__":       
    app = qtw.QApplication(sys.argv)
 
    mainl = Minwindow()
    mainl.show()
    sys.exit(app.exec())        
        

I have solved this problem by introducing a global variable and pass the text value from qlineedit of popup window to this global variable and in qtimer, if the global variable is not empty, it will update the text in qlineedit of mdi subwindow.

here is the rest of the code:

import PyQt6.QtWidgets   as qtw
import PyQt6.QtCore as qtc
import sys 

ntexx = ''
class Minwindow(qtw.QMainWindow): 
    def __init__(self):
        super().__init__()
        self.mdi = qtw.QMdiArea()
        widget = qtw.QWidget()
        self.setCentralWidget(widget)
        windowLayout = qtw.QHBoxLayout(widget)
        windowLayout.addWidget(self.mdi, )
        
        pbSub = qtw.QPushButton('Sub', self)
        pbSub.setGeometry(9, 9, 75, 20)
        pbSub.clicked.connect(self.on_click_sub)
        timer = qtc.QTimer(self) 
        # adding action to timer
        timer.timeout.connect(self.showTime)  
        # update the timer every second
        timer.start(500)
        
        self.setGeometry(125,75,350,300)
    def showTime(self):
        global ntexx
        if ntexx != '':
           self.subw.txtusr.setText(ntexx)
           ntexx = ''
        
    def on_click_sub(self):
        self.subw = qtw.QMdiSubWindow()
        self.subw.txtusr = qtw.QLineEdit('', self.subw)
        self.subw.txtusr.setGeometry(25,30,100,25)
        pbPop = qtw.QPushButton('Pop', self.subw)
        pbPop.setGeometry(50, 75, 75, 20)
        pbPop.clicked.connect(self.on_click_pop)
        self.subw.setGeometry(75, 75, 200, 150)
        self.subw.setWindowTitle("Create User")
        self.mdi.addSubWindow(self.subw)
        self.subw.show() 
    def on_click_pop(self):        
        self.showPopup()
 
    def showPopup(self):
        name = 'POPS'
        self.dpop = Popw(name)        
        self.dpop.move(self.pos().x()+75, self.pos().y()+75)
        self.dpop.setFixedSize(200, 150)
        self.dpop.show()   
class Popw(qtw.QMainWindow):
    def __init__(self, name):
        super().__init__()        
        self.name = name
        #self.setGeometry(100, 100, 300, 350)
        self.setWindowFlags(qtc.Qt.WindowType.Window | qtc.Qt.WindowType.CustomizeWindowHint | qtc.Qt.WindowType.WindowStaysOnTopHint)
        self.initUI()
    def initUI(self):
        self.ntxtusr = qtw.QLineEdit('', self)
        self.ntxtusr.setGeometry(25,30,100,25)
        pbOk = qtw.QPushButton('Ok', self)
        pbOk.setGeometry(50, 75, 75, 20)
        pbOk.clicked.connect(self.on_click_ok) 
    def on_click_ok(self):
        #Minwindow.mdi.subw.txtusr.setText(ntxtusr.text())
        global ntexx
        ntexx = self.ntxtusr.text()
        self.close()
    
if __name__ == "__main__":       
    app = qtw.QApplication(sys.argv)
 
    mainl = Minwindow()
    mainl.show()
    sys.exit(app.exec())        

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM