简体   繁体   English

如何在另一个类的 mdi 子窗口中设置 qlineedit 的文本?

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

I am using python and pyqt6.我正在使用 python 和 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:我有一个 maindwindow A,它有一个 mdi 区域,它有一个子窗口,有一个 qlineedt 和一个按钮,当我按下按钮时,会出现一个弹出窗口,这是同一程序中的另一个类,弹出窗口有 qlineedit 和一个按钮,当我在 qlineedit 中输入一个值并按下按钮时,弹出窗口关闭并且 qlineedit 的值被传输到子窗口的 qlineedit,我尝试了以下代码:

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

but it does not work.但它不起作用。 The error is "qmainwindow A has no attribute subwindow" I also tried this:错误是“qmainwindow A 没有属性子窗口”我也试过这个:

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

And the error is "qmainwindow A has no attribute mdi" I declared mdi as:错误是“qmainwindow A has no attribute mdi” 我将 mdi 声明为:

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.我已经通过引入一个全局变量来解决这个问题,并将弹出窗口的 qlineedit 中的文本值传递给这个全局变量,在 qtimer 中,如果全局变量不为空,它将更新 mdi 子窗口的 qlineedit 中的文本。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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