1

Using PyQt6 I can instantiate a QMainWindow using something like:

import sys

from PyQt6.uic import loadUi
from PyQt6.QtWidgets import QApplication, QMainWindow

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        loadUi("mainwindow.ui", self)


if __name__ == '__main__':
    app = QApplication(sys.argv)

    window = MainWindow()
    window.show()

    exit(app.exec())

The equivalent pyside6 code:

import sys

from PySide6.QtUiTools import QUiLoader
from PySide6.QtWidgets import QMainWindow, QApplication


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        QUiLoader().load("mainwindow.ui", self)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    exit(app.exec())

This does not work because second parameter of QUiLoader().load() is the parent and thus I should add the resulting widget to my MainWindow thus preventing me to design the whole Main Window in Qt-Designer.

I know I could instantiate the whole MainWindow from main, such as:

import sys
from PySide6 import QtCore, QtGui, QtWidgets
from PySide6.QtUiTools import QUiLoader

app = QtWidgets.QApplication(sys.argv)
window = QUiLoader().load("mainwindow.ui", None)
window.show()
exit(app.exec())

... but that would prevent me to subclass QMainWindow to add my own code.

What is the right way to port PyQt6 code to pyside6?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
ZioByte
  • 2,690
  • 1
  • 32
  • 68
  • 1
    AFAIK PySide6 still does not support doing the same thing loadUi does (and I often ask myself why they don't). You can only use pyside-uic classes. The only viable alternative is to just create the central widgets of those main windows as a basic form (QWidget) and then use `setCentralWidget()`. This obviously means that you won't be able to embed menus, dock bars and tool bars from Designer. – musicamante Apr 10 '22 at 10:53
  • I don't understand why this question has been marked as a duplicate, it really isn't. The other question doesn't generate menu bars and other features of a `QMainWindow`, and its answers do not do this either. @ZioByte, did you ever find a solution? – Mikkel Roald-Arbøl Feb 15 '23 at 11:25
  • @MikkelRoald-Arbøl No, in the end I ditched PySide6 and kept using PyQt6. IMHO this is a major flaw in PySide*. – ZioByte Feb 17 '23 at 09:12

0 Answers0