1
votes
import os
import sys

from PyQt5 import QtGui
from PyQt5 import QtWidgets

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

def main():
    app = QtWidgets.QApplication(sys.argv)
    programWindow = ProgramWindow()

    programWindow.show()
    sys.exit(app.exec_())


class ProgramWindow(QtWidgets.QMainWindow):

    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)

        self.setup_main_window()
        self.set_window_layout()

    def setup_main_window(self):
        self.resize( 800, 600  )
        self.setWindowTitle( "Test" )

    def set_window_layout(self):
        self.startSimulationButton = QPushButton( 'Start Simulation' )

        horizontalLayout = QHBoxLayout()
        horizontalLayout.addWidget( self.startSimulationButton )

        horizontalGroupBox = QGroupBox( "My Group" )
        horizontalGroupBox.setLayout( horizontalLayout )

        main_vertical_layout = QVBoxLayout()
        main_vertical_layout.addWidget( horizontalGroupBox )
        self.setLayout( main_vertical_layout )


if __name__ == "__main__":
    main()

When I run it, it shows a empty window and throws this error on the console:

QWidget::setLayout: Attempting to set QLayout "" on ProgramWindow "", which already has a layout

I already looked on QLayout: Attempting to add QLayout "" to QWidget "", which already has a layout but I do not think I am doing any of that, what wrong with this code?

Commenting out the last line self.setLayout( main_vertical_layout ) fixes it, but of course, does not show anything on the screen.

This code should Create a QGroupBox with the layout QHBoxLayout and put the QGroupBox inside the layout QVBoxLayout. And the QVBoxLayout should be attached on my main window:

  1. QHBoxLayout
  2. QGroupBox
  3. QVBoxLayout
  4. ProgramWindow
2

2 Answers

3
votes

With that said @Aviad your code might look like this:

import sys

from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QPushButton,
                             QHBoxLayout, QGroupBox, QVBoxLayout)

class ProgramWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setup_main_window()
        self.set_window_layout()

    def setup_main_window(self):
        self.centralwidget = QWidget()
        self.setCentralWidget(self.centralwidget)
        self.resize( 800, 600  )
        self.setWindowTitle( "Test" )

    def set_window_layout(self):
        self.startSimulationButton = QPushButton( 'Start Simulation' )

        self.horizontalLayout = QHBoxLayout(self.centralwidget) 
        self.horizontalLayout.addWidget( self.startSimulationButton )

        self.horizontalGroupBox = QGroupBox( "My Group" )
        self.horizontalLayout.addWidget( self.horizontalGroupBox )

        self.main_vertical_layout = QVBoxLayout()
        self.horizontalGroupBox.setLayout( self.main_vertical_layout )
        button1 = QPushButton( 'Button1' )
        button2 = QPushButton( 'Button2' )
        self.main_vertical_layout.addStretch(1)
        self.main_vertical_layout.addWidget( button1 )
        self.main_vertical_layout.addWidget( button2 )

def main():
    app = QApplication(sys.argv)
    programWindow = ProgramWindow()

    programWindow.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

enter image description here

0
votes

The proper way to use QMainWindow is having a "centralWidget", set this central widget with your layout , and you got it. See setCentralWiget