PDA

View Full Version : QLayout: Attempting to add QLayout "" to MainWindow "", which already has a layout



mcanonic
19th April 2019, 17:45
Hi,
I'm new and probably this is a dumb question.
I've got the error that you can see as subject. Actually, it seems a warning since the code provide a output that I like (many improvings are needed here), but I would like to give ride to these warning.

The answers provided in internet does not help me too much, so I asked to this forum.

The code that I have written is after my signature.
Thanks,
M


from PyQt5.QtCore import Qt, QSize
from PyQt5.QtWidgets import (QWidget, QHBoxLayout, QVBoxLayout,
QLabel, QApplication, QPushButton, QLineEdit)
from PyQt5 import QtGui
from PyQt5.QtGui import QPixmap, QIcon, QFont

import sys

class MainWindow(QWidget):
def __init__(self):
super().__init__()

self.initUI()

def initUI(self):

hbox = QHBoxLayout(self)
hbox.setSpacing(10)

vBoxMain = QVBoxLayout(self) #Vertical box
vBoxMain.setSpacing(35) #space between widgets

vBoxContext = QVBoxLayout(self) #Vertical box
vBoxContext.setSpacing(35) #space between widgets

self.loadMainMenu(vBoxMain)
self.loadContextMenu(vBoxContext)

hbox.addLayout(vBoxMain)
hbox.addLayout(vBoxContext)

#self.setLayout(hbox)
#self.move(300,200)
self.setWindowTitle('AnForA')
self.setWindowIcon(QIcon('upoSmallLogo.png'))

self.show()

def loadContextMenu(self, vBox):

vbox = QVBoxLayout(self)
vbox.setSpacing(10)


hbox = QHBoxLayout(self)
hbox.setSpacing(10)

#Add edit text
insertFile = QLineEdit(self)
insertFile.setText("Select apk file here")
hbox.addWidget(insertFile)

#Add browse button
browsebtn = QPushButton('Browse', self)
browsebtn.clicked.connect(QApplication.instance(). quit)
#apkbtn.resize(apkbtn.sizeHint())
hbox.addWidget(browsebtn)
#apkbtn.move(250, 250)

vBox.addLayout(hbox)

def loadMainMenu(self, vBox):
#Add logo UPO image
pixmap = QPixmap("logoUPO.png")
labelImg = QLabel(self)
pixmap2 = pixmap.scaledToWidth(300)
labelImg.setPixmap(pixmap2)
labelImg.setAlignment(Qt.AlignCenter)
vBox.addWidget(labelImg)

labelDesc = QLabel(self)
labelDesc.setText('AnForA\nAndroid Forensic Automator')
labelDesc.setAlignment(Qt.AlignCenter)
labelDesc.setFont(QtGui.QFont("Times", 12, QtGui.QFont.Bold))
vBox.addWidget(labelDesc)

#Add Install APK button
apkbtn = QPushButton('Install APK into Android Device', self)
apkbtn.clicked.connect(QApplication.instance().qui t)
#apkbtn.resize(apkbtn.sizeHint())
vBox.addWidget(apkbtn)
#apkbtn.move(250, 250)

#Add Install APK button
rcdbtn = QPushButton('Record user session', self)
rcdbtn.clicked.connect(QApplication.instance().qui t)
#apkbtn.resize(apkbtn.sizeHint())
vBox.addWidget(rcdbtn)
#apkbtn.move(250, 250)

#Add Install APK button
forbtn = QPushButton('Start forensic analysis', self)
forbtn.clicked.connect(QApplication.instance().qui t)
#apkbtn.resize(apkbtn.sizeHint())
vBox.addWidget(forbtn)
#apkbtn.move(250, 250)


#Add quit button
qbtn = QPushButton('Quit', self)
qbtn.clicked.connect(QApplication.instance().quit)
#qbtn.resize(qbtn.sizeHint())
#qbtn.move(250, 300)
vBox.addWidget(qbtn)

"""
#Add Label main menu
labelMainMenu = QtGui.QLabel('AnForA\nAndroid Forensic Automator',cWidget)
labelMainMenu.setFont(QtGui.QFont("Times", 16, QtGui.QFont.Bold))
labelMainMenu.setFixedWidth(400)
vBox.addWidget(labelMainMenu)

cWidget.setLayout(vBox)
self.setCentralWidget(cWidget)
"""

if __name__ == '__main__':
app = QApplication(sys.argv)
main = MainWindow()
sys.exit(app.exec_())

anda_skoa
19th April 2019, 18:14
QMainWindow already has a layout, i.e. for placing toolbars, statusbar and menubar correctly around the "center widget".

You are creating a QHBoxLayout and passing "self" to its constructor, which means you are trying to set this layout as the main window's layout instead.

Instead of


hbox = QHBoxLayout(self)
hbox.setSpacing(10)

you do this


widget = QWidget(self)
self.setCentralWidget(widget)

hbox = QHBoxLayout(widget)
hbox.setSpacing(10)


Cheers,
_

mcanonic
23rd April 2019, 12:26
Thanks for your prompt reply.
The suggestion you provide me rises up a error:

Traceback (most recent call last):
File "anforaGui.py", line 133, in <module>
main = MainWindow()
File "anforaGui.py", line 24, in __init__
self.initUI()
File "anforaGui.py", line 29, in initUI
self.setCentralWidget(widget)
AttributeError: 'MainWindow' object has no attribute 'setCentralWidget'


I have a couple of questions:
- where should I note that I'm using QMainWindow (I did not find it in the code, I know, dumb question)
- which is the best website to start using qt? This concept of central widget is not defined in many guide that I read

Thanks,
Massimo

anda_skoa
23rd April 2019, 14:05
Yes, sorry, my bad.

For some reason I thought that your MainWindow as derived from QMainWindow.

So the problem is really the two QVBoxLayout constructions (vBoxMain and vBoxContext).
Simply create these two without passing "self" to the constructor.

Cheers,
_

mcanonic
24th April 2019, 12:31
Yes, sorry, my bad.

For some reason I thought that your MainWindow as derived from QMainWindow.

So the problem is really the two QVBoxLayout constructions (vBoxMain and vBoxContext).
Simply create these two without passing "self" to the constructor.

Cheers,
_

Thanks, I removed it and now I get just one warning. I think it is related to the first hbox:


hbox = QHBoxLayout(self)
hbox.setSpacing(10)


but if I remove the "self" here, I get all widgets overlapped in a microscopic window.

Thanks again,
M

anda_skoa
24th April 2019, 12:41
One layout needs to be responsible for the widget "self".

There are two ways to do that
1) passing "self" to that layout's constructor
2) calling self.setLayout() with that layout as an argument.

Cheers,
_