2013-07-30 53 views
2

這可能是一個簡單的問題,但我想我把它扔給SO :-) 如何將幾個佈局結合在一起(QDialog派生)課?PySide如何結合QDialog類對象中的多個佈局

這是我最初的嘗試 - 但行self.setLayout(mainLayout)不起作用,我收到NameError ...任何人都可以幫助(我是PySide編程的新手)?

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) 

編輯

完整的錯誤輸出是:

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道歉放射性我可憐註釋格式...

+0

我沒有得到任何NameError - 發佈錯誤消息的全文,如果你仍然得到它。 –

+0

可能是一個縮進錯誤,請確保該行與'__init__'中的所有其他縮進級別相同 –

回答

2

這些線是衝突:

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

錯誤/警告消息表明佈局互相重疊,然後setLayout最後嘗試設置一些已設置的屬性。

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

這種速戰速決的工作原理:

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

感謝您的建議的電臺 - 我已嘗試進行您所建議的更改,但我仍然遇到可怕的NameError:' '
'類FindDialog的(QDialog的)'
'文件 「.. \ FindDialog.py」 36行,在:回溯(最近通話最後一個)'
'文件 「.. \ FindDialog.py」 ,第109行,在FindDialog'
'self.setLayout(mainLayout)'
NameError:name'self'is not defined' –