[英]How can I set text of a qlineedit inside a mdi sub window from another class?
我正在使用 python 和 pyqt6。
我有一個 maindwindow A,它有一個 mdi 區域,它有一個子窗口,有一個 qlineedt 和一個按鈕,當我按下按鈕時,會出現一個彈出窗口,這是同一程序中的另一個類,彈出窗口有 qlineedit 和一個按鈕,當我在 qlineedit 中輸入一個值並按下按鈕時,彈出窗口關閉並且 qlineedit 的值被傳輸到子窗口的 qlineedit,我嘗試了以下代碼:
A.subwindow.settext(self.textedit.text())
但它不起作用。 錯誤是“qmainwindow A 沒有屬性子窗口”我也試過這個:
A.mdi.subwindow.settext(self.textedit.text())
錯誤是“qmainwindow A has no attribute mdi” 我將 mdi 聲明為:
self.mdi = QMdiArea()
windowLayout.addWidget(self.mdi)
子窗口為:
self.subwindow = QMdiSubWindow()
self.mdi.addSubWindow(self.subwindow)
self.subwindow.show()
這是產生相同錯誤的最小代碼:
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())
我已經通過引入一個全局變量來解決這個問題,並將彈出窗口的 qlineedit 中的文本值傳遞給這個全局變量,在 qtimer 中,如果全局變量不為空,它將更新 mdi 子窗口的 qlineedit 中的文本。
這是其余的代碼:
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.