Multiple ui’s on the same Qmainwindow

Asked

Viewed 87 times

0

I am trying to put two Qwidgets imported from ui files, however none appears, problem that does not occur when I enter only one.

    # -*- coding: utf-8 -*-
import sys
from PySide2.QtUiTools import *
from PySide2.QtWidgets import *
from PySide2.QtCore import *


def tela(ui):
    arquivo = QFile(ui)
    arquivo.open(QFile.ReadOnly)
    loader = QUiLoader()
    janela = loader.load(arquivo)
    arquivo.close()
    return janela


class Janela(QMainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__()
        menu = tela('menu.ui')
        conteudo = tela('adiciona.ui')
        janela = QHBoxLayout()
        janela.addWidget(menu)
        janela.addWidget(conteudo)
        self.setLayout(janela)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Janela()
    window.show()

    app.exec_()

I am not given any error message.

1 answer

1


In Pyqt5, this can be:

import sys

#from PySide2.QtUiTools import *
from PyQt5 import uic

from PyQt5.QtWidgets import *
from PyQt5.QtCore    import *


def tela(ui):
#    arquivo = QFile(ui)
#    arquivo.open(QFile.ReadOnly)
#    loader = QUiLoader()
#    janela = loader.load(arquivo)

    janela = uic.loadUi(ui)                     # PyQt5

#    arquivo.close()
    return janela


class Janela(QMainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__()

        central_widget = QWidget()              # + <---
        self.setCentralWidget(central_widget)   # + <---

        menu = tela('gui.ui')
        conteudo = tela('we.ui')

        janela = QHBoxLayout(central_widget)    # + <--- central_widget
        janela.addWidget(menu)
        janela.addWidget(conteudo)
#        self.setLayout(janela)                 # - <---


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Janela()
    window.show()

    app.exec_()

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.