QLayout: Attempting to add QLayout "" to MainWindow "", which already has a layout
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
Code:
from PyQt5.
QtCore import Qt,
QSizefrom PyQt5 import QtGui
import sys
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
hbox.setSpacing(10)
vBoxMain.setSpacing(35) #space between widgets
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.setSpacing(10)
hbox.setSpacing(10)
#Add edit text
insertFile.setText("Select apk file here")
hbox.addWidget(insertFile)
#Add browse button
#apkbtn.resize(apkbtn.sizeHint())
hbox.addWidget(browsebtn)
#apkbtn.move(250, 250)
vBox.addLayout(hbox)
def loadMainMenu(self, vBox):
#Add logo UPO image
pixmap2 = pixmap.scaledToWidth(300)
labelImg.setPixmap(pixmap2)
labelImg.setAlignment(Qt.AlignCenter)
vBox.addWidget(labelImg)
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.resize(apkbtn.sizeHint())
vBox.addWidget(apkbtn)
#apkbtn.move(250, 250)
#Add Install APK button
#apkbtn.resize(apkbtn.sizeHint())
vBox.addWidget(rcdbtn)
#apkbtn.move(250, 250)
#Add Install APK button
#apkbtn.resize(apkbtn.sizeHint())
vBox.addWidget(forbtn)
#apkbtn.move(250, 250)
#Add quit button
#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__':
main = MainWindow()
sys.exit(app.exec_())
Re: QLayout: Attempting to add QLayout "" to MainWindow "", which already has a layou
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
Code:
hbox.setSpacing(10)
you do this
Code:
self.setCentralWidget(widget)
hbox.setSpacing(10)
Cheers,
_
Re: QLayout: Attempting to add QLayout "" to MainWindow "", which already has a layou
Thanks for your prompt reply.
The suggestion you provide me rises up a error:
Code:
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
Re: QLayout: Attempting to add QLayout "" to MainWindow "", which already has a layou
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,
_
Re: QLayout: Attempting to add QLayout "" to MainWindow "", which already has a layou
Quote:
Originally Posted by
anda_skoa
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:
Code:
hbox.setSpacing(10)
but if I remove the "self" here, I get all widgets overlapped in a microscopic window.
Thanks again,
M
Re: QLayout: Attempting to add QLayout "" to MainWindow "", which already has a layou
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,
_