Issues with textedit not working when creating a class for subwindows
-
Hi I hope I have this right, I'm having a problem where I am using the 'Class Document' to setup subwindows which on selecting 'Add Window' subwindows are added to MainWindow's MdiArea. All good, but having used setWidget as a textedit when I run the code I cannot carry out any editing in the subwindow as it appears the textedit is not there. Can anyone assist me please?
import sys from PyQt6.QtWidgets import QMainWindow, QApplication, QMdiArea, QMdiSubWindow, QTextEdit, QWidget from PyQt6.QtGui import QAction class MainWindow(QMainWindow): count = 0 def __init__(self): super(MainWindow, self).__init__() self.mdi = QMdiArea() self.setCentralWidget(self.mdi) bar = self.menuBar() file = bar.addMenu("Add Window") file.addAction("window") file.triggered[QAction].connect(self.click) self.setWindowTitle("Multiple window using MDI") def click(self, event): print("New sub window") if event.text() == "window": test = Document() f = self.mdi.addSubWindow(test) f.show() class Document(QWidget): count = 0 def __init__(self): super().__init__() self.filenew = QMdiSubWindow() self.textEdit = QTextEdit() self.filenew.setWidget(self.textEdit) self.setFixedSize(400, 400) MainWindow.count += 1 self.setWindowTitle(f"Untitled {MainWindow.count}") def main(): app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec()) if __name__ == '__main__': main()
Thanks Pixelhead120
-
Hi I hope I have this right, I'm having a problem where I am using the 'Class Document' to setup subwindows which on selecting 'Add Window' subwindows are added to MainWindow's MdiArea. All good, but having used setWidget as a textedit when I run the code I cannot carry out any editing in the subwindow as it appears the textedit is not there. Can anyone assist me please?
import sys from PyQt6.QtWidgets import QMainWindow, QApplication, QMdiArea, QMdiSubWindow, QTextEdit, QWidget from PyQt6.QtGui import QAction class MainWindow(QMainWindow): count = 0 def __init__(self): super(MainWindow, self).__init__() self.mdi = QMdiArea() self.setCentralWidget(self.mdi) bar = self.menuBar() file = bar.addMenu("Add Window") file.addAction("window") file.triggered[QAction].connect(self.click) self.setWindowTitle("Multiple window using MDI") def click(self, event): print("New sub window") if event.text() == "window": test = Document() f = self.mdi.addSubWindow(test) f.show() class Document(QWidget): count = 0 def __init__(self): super().__init__() self.filenew = QMdiSubWindow() self.textEdit = QTextEdit() self.filenew.setWidget(self.textEdit) self.setFixedSize(400, 400) MainWindow.count += 1 self.setWindowTitle(f"Untitled {MainWindow.count}") def main(): app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec()) if __name__ == '__main__': main()
Thanks Pixelhead120
@Pixelhead120
Lost in yourDocument
class. It's aQWidget
, which then has aQMdiSubWindow
in it plus aQTextEdit
?When you use
self.mdi.addSubWindow()
look at examples at https://doc.qt.io/qt-5/qmdiarea.html#addSubWindow. You just pass it the widget you want added. That might be yourQTextEdit
if that is all you want on your subwindow, or it might be, say, aQWidget
onto which you add layouts and other widgets. You certainly won't want anyQMdiSubWindow()
,QMdiArea::addSubWindow()
creates that around the widget you pass it and returns thatQMdiSubWindow
it created, if you want it.