Qwidget class inside Qmainwindow class

Asked

Viewed 183 times

0

I have the following code:

# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QPushButton
from PyQt5.QtWidgets import QSpacerItem, QLabel, QComboBox
from PyQt5.QtWidgets import QSizePolicy, QMainWindow, QApplication
from PyQt5.QtCore import QMetaObject
from PyQt5.QtGui import QIcon, QPixmap


class Janela(QMainWindow):
    def __init__(self):
        super().__init__()
        self.contentor = QWidget(self)
        self.verticalLayout = QVBoxLayout(self.contentor)
        self.menu = QHBoxLayout()
        self.adicionar = QPushButton(self.contentor)
        icone_adicionar = QIcon()
        icone_adicionar.addPixmap(QPixmap("adicionar.png"))
        self.adicionar.setIcon(icone_adicionar)
        self.adicionar.setFlat(True)
        self.menu.addWidget(self.adicionar)
        self.listar = QPushButton(self.contentor)
        icone_listar = QIcon()
        icone_listar.addPixmap(QPixmap("listar.png"))
        self.listar.setIcon(icone_listar)
        self.listar.setFlat(True)
        self.menu.addWidget(self.listar)
        self.configurar = QPushButton(self.contentor)
        icone_configurar = QIcon()
        icone_configurar.addPixmap(QPixmap("configurar.png"))
        self.configurar.setIcon(icone_configurar)
        self.configurar.setFlat(True)
        self.menu.addWidget(self.configurar)
        self.verticalLayout.addLayout(self.menu)
        # ---------------------------------------#
        self.conteudo = QWidget(self.contentor)
        # ---------------------------------------#
        self.verticalLayout.addWidget(self.conteudo)
        self.setCentralWidget(self.contentor)
        QMetaObject.connectSlotsByName(self)


class Conteudo(QWidget):
    def __init__(self):
        super().__init__()
        self.verticalLayout = QVBoxLayout(self)
        spacer = QSpacerItem(20, 87,
                             QSizePolicy.Minimum,
                             QSizePolicy.Expanding)
        self.verticalLayout.addItem(spacer)
        self.login = QPushButton(self)
        icon = QIcon()
        icon.addPixmap(QPixmap("google.png"),
                       QIcon.Normal, QIcon.Off)
        self.login.setIcon(icon)
        self.login.setText("Entrar com Google")
        self.verticalLayout.addWidget(self.login)
        self.horizontalLayout = QHBoxLayout()
        self.label_tema = QLabel(self)
        self.label_tema.setText("Tema")
        self.horizontalLayout.addWidget(self.label_tema)
        self.tema = QComboBox(self)
        self.horizontalLayout.addWidget(self.tema)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.verticalLayout.addItem(spacer)
        self.faz_bkp = QPushButton(self)
        self.faz_bkp.setText("Fazer backup")
        self.verticalLayout.addWidget(self.faz_bkp)
        self.imp_bkp = QPushButton(self)
        self.imp_bkp.setText("Importar backup")
        self.verticalLayout.addWidget(self.imp_bkp)


if __name__ == "__main__":
    aplicativo = QApplication(sys.argv)
    janela = Janela()
    janela.show()

    aplicativo.exec_()

The Window class presents only one Qhboxlayout - Window()['menu'] - with buttons that would change the contents of a Qwidget - Window()['content']. When the variable 'window' is set with both the Window class and the Content, it works. However when trying Window()['content'] = Content gives error. How should I proceed?

1 answer

1


Try this:

import sys
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QPushButton
from PyQt5.QtWidgets import QSpacerItem, QLabel, QComboBox
from PyQt5.QtWidgets import QSizePolicy, QMainWindow, QApplication
from PyQt5.QtCore    import QMetaObject
from PyQt5.QtGui     import QIcon, QPixmap


class Janela(QMainWindow):
    def __init__(self):
        super().__init__()

        self.contentor = QWidget() 

        self.setCentralWidget(self.contentor)             # +

        self.verticalLayout = QVBoxLayout(self.contentor)

        self.menu = QHBoxLayout()
        self.adicionar  = QPushButton(self.contentor)
        icone_adicionar = QIcon()
        icone_adicionar.addPixmap(QPixmap("D:/_Qt/img/py-qt.png"))
        self.adicionar.setIcon(icone_adicionar)
        self.adicionar.setFlat(True)
        self.menu.addWidget(self.adicionar)
        self.listar = QPushButton(self.contentor)
        icone_listar = QIcon()
        icone_listar.addPixmap(QPixmap("D:/_Qt/img/1.png"))
        self.listar.setIcon(icone_listar)
        self.listar.setFlat(True)
        self.menu.addWidget(self.listar)
        self.configurar = QPushButton(self.contentor)
        icone_configurar = QIcon()
        icone_configurar.addPixmap(QPixmap("D:/_Qt/img/2.png"))
        self.configurar.setIcon(icone_configurar)
        self.configurar.setFlat(True)
        self.menu.addWidget(self.configurar)
        self.verticalLayout.addLayout(self.menu)

        # ---------------------------------------#
#        self.conteudo = QWidget(self.contentor)
        self.conteudo = Conteudo(self.contentor)         # +
        # ---------------------------------------#

        self.verticalLayout.addWidget(self.conteudo)
        self.setCentralWidget(self.contentor)
        QMetaObject.connectSlotsByName(self)


class Conteudo(QWidget):
    def __init__(self, parent=None):                      # + parent=None
        super().__init__(parent)                          # + parent

        self.verticalLayout = QVBoxLayout(self)
        spacer = QSpacerItem(20, 87,
                             QSizePolicy.Minimum,
                             QSizePolicy.Expanding)
        self.verticalLayout.addItem(spacer)
        self.login = QPushButton(self)
        icon = QIcon()
        icon.addPixmap(QPixmap("D:/_Qt/img/cat.png"),
                       QIcon.Normal, QIcon.Off)
        self.login.setIcon(icon)
        self.login.setText("Entrar com Google")
        self.verticalLayout.addWidget(self.login)
        self.horizontalLayout = QHBoxLayout()
        self.label_tema = QLabel(self)
        self.label_tema.setText("Tema")
        self.horizontalLayout.addWidget(self.label_tema)
        self.tema = QComboBox(self)
        self.horizontalLayout.addWidget(self.tema)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.verticalLayout.addItem(spacer)
        self.faz_bkp = QPushButton(self)
        self.faz_bkp.setText("Fazer backup")
        self.verticalLayout.addWidget(self.faz_bkp)
        self.imp_bkp = QPushButton(self)
        self.imp_bkp.setText("Importar backup")
        self.verticalLayout.addWidget(self.imp_bkp)


if __name__ == "__main__":
    aplicativo = QApplication(sys.argv)
    janela = Janela()
    janela.show()
    aplicativo.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.