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.
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.
– Cleber Nandi
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.
– Cleber Nandi
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
– Cleber Nandi
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?
– Cleber Nandi
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.
– Réulison Silva