Python - Pyqt5 - how to insert a Qgridlayout into a Qmainwindow?

Asked

Viewed 159 times

1

Good afternoon, everyone.

I got a problem.

I’m using Qmainwindow to create a screen where I insert a Qmenubar successfully, but then I’ll try to insert a Qgridlayout and it doesn’t work. No error appears but also does not show the components theoretically linked to it.

The Code is as follows::

# coding: utf-8

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QGridLayout, QLabel, QLineEdit, QAction, QWidget

__version__ = "1.0.0"

class App(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = 'System Converter Database - Version: '
        self.left = 10
        self.top = 10
        self.width = 800
        self.height = 400
        self.init_ui()

    def init_ui(self):
        self.setWindowTitle(self.title + __version__)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.move(app.desktop().screen().rect().center() - self.rect().center())

        # Adicionando os componentes
        # Menu principal
        main_menu = self.menuBar()
        menu_scd = main_menu.addMenu("SCD")
        menu_scd.addAction("Criar SCD")
        menu_scd.addAction("Localizar SCD")
        main_menu.addSeparator()

        # Menu Conversor
        menu_converter = main_menu.addMenu("Conversor")
        menu_converter_passo = menu_converter.addMenu("Passo-a-passo")
        menu_converter_passo.addAction("Copiar")
        menu_converter_passo.addAction("Extrair")
        menu_converter_passo.addAction("Preparar")
        menu_converter_passo.addAction("Converter")
        menu_converter_automatico = menu_converter.addMenu("Automático")
        menu_converter_automatico.addAction("Iniciar")

        # Menu Verificador
        menu_verificador = main_menu.addMenu("Verificador")
        menu_verificador.addAction("Iniciar")

        # Menu Sair
        menu_quit = QAction("Sair", self)
        menu_quit.triggered.connect(self.sair)
        main_menu.addAction(menu_quit)

        # Grade principal

        title = QLabel('Title')
        titleEdit = QLineEdit()

        grid_main = QGridLayout()
        grid_main.setSpacing(5)

        grid_main = QGridLayout()
        grid_main.addWidget(title, 1, 0)
        grid_main.addWidget(titleEdit, 1, 1)

        self.setLayout(grid_main)


        self.show()

    def sair(self):
        self.close()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

How can I fix this?

Thanks in advance.

1 answer

2


Set the layout in a simple Qwidget and set it as a central widget of the main window.

QWidget* widget = new QWidget(mainWindow);
widget->setLayout(gridLayout);
mainWindow->setCentralWidget(widget);

If the following message appears on the console:

QLayout: Attempting to add QLayout "" to QMainWindow "", which already has a layout

It is because you passed the main window as parent to Qgridlayout. This means the same as setting the layout in the main window. So don’t pass any parent or pass the central widget as parent to the grid layout.

  • Good morning Réulison Silva. This code used is new to me. The way the variables were specified (you can call it). Why do I normally know it like this: name_variable = method() Would you be so kind as to enlighten me? Grateful.

  • I really could not implement what you proposed. I even researched and shows the same suggestion, as I know how to implement it in my code. I need help, please.

  • If I put exactly what was mentioned (this is because I don’t know how this applies to python, since I’m still starting) the following error happens: Qwidget* widget = new Qwidget() Syntaxerror: invalid syntax

  • 1

    Consegui resolver assim:

 # Grade principal
 main_grid = QGridLayout(self)
 qwidget = QWidget()
 qwidget.setLayout(main_grid)
 self.setCentralWidget(qwidget)



 # Adicionando widgets
 title = QLabel('Title')
 titleEdit = QLineEdit()

 main_grid.addWidget(title, 1, 0)
 main_grid.addWidget(titleEdit, 1, 1)

Claro que isso é o que foi dito acima. But how to put another Qgrid inside the one I just created?

  • 1

    What you can do is create a Qwidget configured as the central widget (mainwindow) and assign it a layout. Qmainwindow has its own layout to put statusbar and mainmenu, so you can’t set another directly.

Browser other questions tagged

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