Liam Deacon
Liam Deacon

Reputation: 904

PySide how to combine multiple layouts in QDialog class object

This may be a simple question, but I thought I throw it out to the experts at SO :-) How do you combine several layouts a in a single (QDialog-derived) class?

Here is my initial attempt - however the line self.setLayout(mainLayout) does not work and I receive a NameError... Can anyone help (I am a newbie to PySide programming)?

from PySide.QtCore import *
from PySide.QtGui import *

class FindDialog(QDialog):
def __init__(self, parent=None):
    super(FindDialog, self).__init__(parent)

    self.setWindowTitle (self.tr("pi Plot") + " - " + self.tr("Find"))
    self.setSizeGripEnabled(True)
    self.setAttribute(Qt.WA_DeleteOnClose)

    topLayout = QGridLayout(self)
    bottomLayout = QGridLayout(self)

    topLayout.addWidget(QLabel(self.tr("Start From")), 0, 0 )
    self.labelStart = QLabel()
    self.labelStart.setFrameStyle(QFrame.Panel | QFrame.Sunken)
    self.labelStart.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed))
    topLayout.addWidget(self.labelStart, 0, 1, 1, 4)

    topLayout.addWidget(QLabel(self.tr( "Find" )), 1, 0)
    self.boxFind = QComboBox(self)
    self.boxFind.setEditable(True)
    self.boxFind.setDuplicatesEnabled(False)
    self.boxFind.setInsertPolicy(QComboBox.InsertAtTop)
    #boxFind.setAutoCompletion(True)
    self.boxFind.setMaxCount (10)
    self.boxFind.setMaxVisibleItems (10)
    self.boxFind.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed))
    topLayout.addWidget(self.boxFind, 1, 1, 1, 4)


    self.groupBox = QGroupBox(self.tr("Search in"))
    self.groupBoxLayout = QVBoxLayout(self.groupBox)

    self.boxWindowNames = QCheckBox(self.tr("&Window Names"))
    self.boxWindowNames.setChecked(True)
    self.groupBoxLayout.addWidget(self.boxWindowNames)

    self.boxWindowLabels = QCheckBox(self.tr("Window &Labels"))
    self.boxWindowLabels.setChecked( False )
    self.groupBoxLayout.addWidget(self.boxWindowLabels)

    self.boxFolderNames = QCheckBox(self.tr("Folder &Names"))
    self.boxFolderNames.setChecked( False )
    self.groupBoxLayout.addWidget(self.boxFolderNames)

    bottomLayout.addWidget(self.groupBox, 0, 0, 3, 1)

    self.boxCaseSensitive = QCheckBox(self.tr("Case &Sensitive"))
    self.boxCaseSensitive.setChecked(False)
    bottomLayout.addWidget(self.boxCaseSensitive, 0, 1)

    self.boxPartialMatch = QCheckBox(self.tr("&Partial Match Allowed"))
    self.boxPartialMatch.setChecked(True)
    bottomLayout.addWidget(self.boxPartialMatch, 1, 1)

    self.boxSubfolders = QCheckBox(self.tr("&Include Subfolders"))
    self.boxSubfolders.setChecked(True)
    bottomLayout.addWidget(self.boxSubfolders, 2, 1)

    self.buttonFind = QPushButton(self.tr("&Find"))
    self.buttonFind.setDefault( True )
    bottomLayout.addWidget(self.buttonFind, 0, 2)

    self.buttonReset = QPushButton(self.tr("&Update Start Path"), self)
    bottomLayout.addWidget(self.buttonReset, 1, 2)
    self.buttonCancel = QPushButton(self.tr("&Close"), self)
    bottomLayout.addWidget(self.buttonCancel, 2, 2)
    bottomLayout.setColumnStretch(4, 1)

    mainLayout = QVBoxLayout()
    mainLayout.addLayout(topLayout)
    mainLayout.addLayout(bottomLayout)
    mainLayout.addStretch(1)

    self.setLayout(mainLayout)

EDIT

The full error output is:

Traceback (most recent call last):
  File "..\FindDialog.py", line 36, in <module>
    class FindDialog(QDialog):
  File "..\FindDialog.py", line 109, in FindDialog
    self.setLayout(mainLayout)
NameError: name 'self' is not defined

PS Apologies to Radio- for my poor comment formatting...

Upvotes: 2

Views: 2680

Answers (1)

Radio-
Radio-

Reputation: 3171

These lines are in conflict:

topLayout = QGridLayout(self)
bottomLayout = QGridLayout(self)
...
self.setLayout(mainLayout)

The error/warning messages indicate that the layouts are over-writing each other, and then finally setLayout is trying to set some properties which have already been set.

QLayout: Attempting to add QLayout "" to FindDialog "", which already has a layout
QLayout::addChildLayout: layout "" already has a parent

This quick fix works:

topLayout = QGridLayout()
bottomLayout = QGridLayout()
...
self.setLayout(mainLayout)

Upvotes: 2

Related Questions