Results 1 to 6 of 6

Thread: QLayout: Attempting to add QLayout "" to MainWindow "", which already has a layout

  1. #1
    Join Date
    Apr 2019
    Posts
    13
    Qt products
    Qt5
    Platforms
    Unix/X11

    Question 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
    Qt Code:
    1. from PyQt5.QtCore import Qt, QSize
    2. from PyQt5.QtWidgets import (QWidget, QHBoxLayout, QVBoxLayout,
    3. from PyQt5 import QtGui
    4. from PyQt5.QtGui import QPixmap, QIcon, QFont
    5.  
    6. import sys
    7.  
    8. class MainWindow(QWidget):
    9. def __init__(self):
    10. super().__init__()
    11.  
    12. self.initUI()
    13.  
    14. def initUI(self):
    15.  
    16. hbox = QHBoxLayout(self)
    17. hbox.setSpacing(10)
    18.  
    19. vBoxMain = QVBoxLayout(self) #Vertical box
    20. vBoxMain.setSpacing(35) #space between widgets
    21.  
    22. vBoxContext = QVBoxLayout(self) #Vertical box
    23. vBoxContext.setSpacing(35) #space between widgets
    24.  
    25. self.loadMainMenu(vBoxMain)
    26. self.loadContextMenu(vBoxContext)
    27.  
    28. hbox.addLayout(vBoxMain)
    29. hbox.addLayout(vBoxContext)
    30.  
    31. #self.setLayout(hbox)
    32. #self.move(300,200)
    33. self.setWindowTitle('AnForA')
    34. self.setWindowIcon(QIcon('upoSmallLogo.png'))
    35.  
    36. self.show()
    37.  
    38. def loadContextMenu(self, vBox):
    39.  
    40. vbox = QVBoxLayout(self)
    41. vbox.setSpacing(10)
    42.  
    43.  
    44. hbox = QHBoxLayout(self)
    45. hbox.setSpacing(10)
    46.  
    47. #Add edit text
    48. insertFile = QLineEdit(self)
    49. insertFile.setText("Select apk file here")
    50. hbox.addWidget(insertFile)
    51.  
    52. #Add browse button
    53. browsebtn = QPushButton('Browse', self)
    54. browsebtn.clicked.connect(QApplication.instance().quit)
    55. #apkbtn.resize(apkbtn.sizeHint())
    56. hbox.addWidget(browsebtn)
    57. #apkbtn.move(250, 250)
    58.  
    59. vBox.addLayout(hbox)
    60.  
    61. def loadMainMenu(self, vBox):
    62. #Add logo UPO image
    63. pixmap = QPixmap("logoUPO.png")
    64. labelImg = QLabel(self)
    65. pixmap2 = pixmap.scaledToWidth(300)
    66. labelImg.setPixmap(pixmap2)
    67. labelImg.setAlignment(Qt.AlignCenter)
    68. vBox.addWidget(labelImg)
    69.  
    70. labelDesc = QLabel(self)
    71. labelDesc.setText('AnForA\nAndroid Forensic Automator')
    72. labelDesc.setAlignment(Qt.AlignCenter)
    73. labelDesc.setFont(QtGui.QFont("Times", 12, QtGui.QFont.Bold))
    74. vBox.addWidget(labelDesc)
    75.  
    76. #Add Install APK button
    77. apkbtn = QPushButton('Install APK into Android Device', self)
    78. apkbtn.clicked.connect(QApplication.instance().quit)
    79. #apkbtn.resize(apkbtn.sizeHint())
    80. vBox.addWidget(apkbtn)
    81. #apkbtn.move(250, 250)
    82.  
    83. #Add Install APK button
    84. rcdbtn = QPushButton('Record user session', self)
    85. rcdbtn.clicked.connect(QApplication.instance().quit)
    86. #apkbtn.resize(apkbtn.sizeHint())
    87. vBox.addWidget(rcdbtn)
    88. #apkbtn.move(250, 250)
    89.  
    90. #Add Install APK button
    91. forbtn = QPushButton('Start forensic analysis', self)
    92. forbtn.clicked.connect(QApplication.instance().quit)
    93. #apkbtn.resize(apkbtn.sizeHint())
    94. vBox.addWidget(forbtn)
    95. #apkbtn.move(250, 250)
    96.  
    97.  
    98. #Add quit button
    99. qbtn = QPushButton('Quit', self)
    100. qbtn.clicked.connect(QApplication.instance().quit)
    101. #qbtn.resize(qbtn.sizeHint())
    102. #qbtn.move(250, 300)
    103. vBox.addWidget(qbtn)
    104.  
    105. """
    106. #Add Label main menu
    107. labelMainMenu = QtGui.QLabel('AnForA\nAndroid Forensic Automator',cWidget)
    108. labelMainMenu.setFont(QtGui.QFont("Times", 16, QtGui.QFont.Bold))
    109. labelMainMenu.setFixedWidth(400)
    110. vBox.addWidget(labelMainMenu)
    111.  
    112. cWidget.setLayout(vBox)
    113. self.setCentralWidget(cWidget)
    114. """
    115.  
    116. if __name__ == '__main__':
    117. app = QApplication(sys.argv)
    118. main = MainWindow()
    119. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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
    Qt Code:
    1. hbox = QHBoxLayout(self)
    2. hbox.setSpacing(10)
    To copy to clipboard, switch view to plain text mode 
    you do this
    Qt Code:
    1. widget = QWidget(self)
    2. self.setCentralWidget(widget)
    3.  
    4. hbox = QHBoxLayout(widget)
    5. hbox.setSpacing(10)
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. #3
    Join Date
    Apr 2019
    Posts
    13
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default 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:
    Qt Code:
    1. Traceback (most recent call last):
    2. File "anforaGui.py", line 133, in <module>
    3. main = MainWindow()
    4. File "anforaGui.py", line 24, in __init__
    5. self.initUI()
    6. File "anforaGui.py", line 29, in initUI
    7. self.setCentralWidget(widget)
    8. AttributeError: 'MainWindow' object has no attribute 'setCentralWidget'
    To copy to clipboard, switch view to plain text mode 

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  5. #5
    Join Date
    Apr 2019
    Posts
    13
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QLayout: Attempting to add QLayout "" to MainWindow "", which already has a layou

    Quote Originally Posted by anda_skoa View Post
    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:
    Qt Code:
    1. hbox = QHBoxLayout(self)
    2. hbox.setSpacing(10)
    To copy to clipboard, switch view to plain text mode 

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

    Thanks again,
    M

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

Similar Threads

  1. Replies: 2
    Last Post: 11th July 2017, 21:06
  2. Replies: 1
    Last Post: 20th November 2015, 11:02
  3. Replies: 3
    Last Post: 16th March 2015, 08:31
  4. Replies: 0
    Last Post: 20th September 2010, 10:58
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 20:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.