WewLad WewLad - 6 months ago 92
Python Question

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

I'm making an application in PyQt4 and this is my code so far:

import sys
from PyQt4 import QtGui, QtCore

class PasswordPrompt(QtGui.QWidget):

def __init__(self):
super(PasswordPrompt, self).__init__()
self.initUi()

def initUi(self):
self.setFixedSize(500, 75)
self.setWindowTitle('Please enter the password...')

self.prompt = QtGui.QLineEdit(self)
self.btn = QtGui.QPushButton('Enter', self)
self.btn.clicked.connect(self.btnClicked)

self.hbox = QtGui.QHBoxLayout()
self.hbox.addWidget(self.prompt)
self.hbox.addWidget(self.btn)

self.vbox = QtGui.QVBoxLayout()
self.vbox.addLayout(self.hbox)
self.vbox2 = QtGui.QVBoxLayout()

self.vbox2.addSpacing(300)
self.hbox2 = QtGui.QHBoxLayout()
self.hbox2.addSpacing(150)
self.vbox2.addLayout(self.hbox2)

self.vbox.addLayout(self.vbox2)

self.setLayout(self.vbox)
self.center()
self.show()

def btnClicked(self):
pw = self.prompt.text()


if pw == "password":
print("Permission granted!")
self.close()
global mw
mw = MainWindow()
else:
print("Permissed denied!")
self.prompt.clear()
self.warningText = QtGui.QLabel('That is the wrong password!', self)
self.hbox2.addWidget(self.warningText)


def center(self):
qr = self.frameGeometry()
cp = QtGui.QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())

class MainWindow(QtGui.QMainWindow):

def __init__(self):
super(MainWindow, self).__init__()
self.initUi()

def initUi(self):
self.setWindowTitle('Main Menu')
self.setFixedSize(1200, 625)
self.palette = QtGui.QPalette()
self.palette.setColor(QtGui.QPalette.Background, QtCore.Qt.white)
self.setPalette(self.palette)

self.navigationMenuBar()
self.firstWidgets()
self.center()
self.show()

def navigationMenuBar(self):

exitAction = QtGui.QAction('&Exit', self)
exitAction.triggered.connect(QtGui.qApp.quit)

self.options = self.menuBar()
theMenu = self.options.addMenu('&Navigation')
theMenu.addAction(exitAction)

def center(self):
qr = self.frameGeometry()
cp = QtGui.QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())

def firstWidgets(self):
self.vbox1 = QtGui.QVBoxLayout()
self.task1 = QtGui.QLabel('Check 1', self)
self.task1CB = QtGui.QCheckBox(self)
self.hbox1 = QtGui.QHBoxLayout()
self.hbox1.addWidget(self.task1)
self.hbox1.addWidget(self.task1CB)
self.vbox1.addLayout(self.hbox1)

self.setLayout(self.vbox1)


def main():
application = QtGui.QApplication(sys.argv)
p = PasswordPrompt()
sys.exit(application.exec())

if __name__=='__main__':
main()


My problem is in MainWindow.firstWidgets(). I try to set a layout however I get the error from the title even though that's my first time using .setLayout for that form which confuses me.

Answer

You can't set a QLayout directly on the QMainWindow. You need to create a QWidget and set it as the central widget on the QMainWindow and assign the QLayout to that.

wid = QtGui.QWidget(self)
self.setCentralWidget(wid)
layout = QtGui.QVBoxLayout()
wid.setLayout(layout)