2014-09-03 57 views
0

我想要生成一個GridLayout(CheckBox的)和HLayout堆疊佈局的對話框。那麼,我做到了。但我的網格變大了(我的HLayout很好),但現在我想要一個ScrollArea,以免佔用太多的房地產。我嘗試了下面的代碼....它會生成一個滾動視圖,但滾動視圖的大小是HLayout中按鈕的大小(第一個ofc)。我希望滾動視圖是對話框的寬度,名義上它應該是HLayout的最小寬度。此外,添加scroll_view和scroll_viewWidget會引發QLayout異常,QLayout :: addChildLayout:layout「」已經有父級。有任何想法嗎?Python PyQt QScrollArea和QGridLayout(或任何?)

class checkboxDialog(QtGui.QDialog) : 

def __init__(self, headers, name) : 
    super(checkboxDialog, self).__init__() 

    self.checkerLayout = QtGui.QVBoxLayout() 
    self.checkerLayout.setMargin(1) 
    self.checkerLayout.setSpacing(2) 

    self.scroll_view = QtGui.QScrollArea(self) 
    self.scroll_view.setWidgetResizable(True) 

    self.scroll_viewWidget = QtGui.QWidget() 
    self.scroll_viewWidget.setGeometry(QtCore.QRect(0, 0, 600, 400)) 
    self.scroll_view.setWidget(self.scroll_viewWidget)   

    self.checkerHlayout = QtGui.QHBoxLayout(self.scroll_viewWidget) 

    checksLayout = QtGui.QGridLayout() 
    checksLayout.setColumnStretch(90,7) 
    checksLayout.setMargin(1) 
    checksLayout.setSpacing(2)  
    self.cbList = [] 
    index = 0 
    for row_index in range(90): 
     for column_index in range(7): 
      if index > len(headers)-1 : break 
      checkbox = QtGui.QCheckBox(headers[index][0:8], self) 
      checkbox.setToolTip("Chanel = {}".format(headers[index])) 
      self.cbList.append(checkbox) 
      # Hide the Phase channels for now ... not sure I shoudl even build the CheckBoxes 
      # But if I dont then the len(cbList) < len(data[0]) 
      if headers[index][-1] == 'p' : 
       checkbox.hide() 
      checksLayout.addWidget(checkbox, row_index, column_index) 
      index += 1 

    self.checkerHlayout.addLayout(checksLayout) 

    self.buttonLayout = QtGui.QHBoxLayout() 
    self.buttonLayout.setMargin(1) 
    self.buttonLayout.setSpacing(2) 

    applyButton = QtGui.QPushButton("Apply") 
    nextButton = QtGui.QPushButton("Next") 
    nextAllButton = QtGui.QPushButton("NextAll") 
    prevButton = QtGui.QPushButton("Prev") 
    prevAllButton = QtGui.QPushButton("PrevAll") 
    clearButton = QtGui.QPushButton("Clear") 

    self.buttonLayout.addWidget(applyButton)   
    self.buttonLayout.addWidget(nextButton) 
    self.buttonLayout.addWidget(nextAllButton) 
    self.buttonLayout.addWidget(prevButton) 
    self.buttonLayout.addWidget(prevAllButton) 
    self.buttonLayout.addWidget(clearButton) 

    self.checkerLayout.addLayout(self.checkerHlayout) 
    self.checkerLayout.addLayout(self.buttonLayout) 
    self.setLayout(self.checkerLayout) 

    self.setObjectName(name) 
    self.setWindowTitle(name) 

    self.connect(applyButton, QtCore.SIGNAL('clicked()'), self.checked) 
    self.connect(nextButton, QtCore.SIGNAL('clicked()'), self.next_chn) 
    self.connect(nextAllButton, QtCore.SIGNAL('clicked()'), self.next_chnAll) 
    self.connect(prevButton, QtCore.SIGNAL('clicked()'), self.prev_chn) 
    self.connect(prevAllButton, QtCore.SIGNAL('clicked()'), self.prev_chnAll) 
    self.connect(clearButton, QtCore.SIGNAL('clicked()'), self.clear_chn) 

回答

1

我想這是你想要的東西:

self.w2 = QtGui.QWidget(self) 
    self.w2.setLayout(self.buttonLayout) 
    self.checkerLayout.addWidget(self.w2) 
    self.checkerLayout.addWidget(self.scroll_view) 
    self.setLayout(self.checkerLayout) 

(替換用addLayout語句塊)

+0

沒錯!那是做的......你說我不明白爲什麼,我很抱歉。 – 2014-09-03 22:29:19

+0

@TimCarnahan你在'checkerLayout'上兩次調用'addLayout'。 mdurant嵌套可能是你想要的佈局。 – Trilarion 2014-09-04 08:55:56

+0

順便說一句,我建議使用QtDesigner來製作複雜的GUI,即使在運行時需要動態填充某些元素,也會帶來很多痛苦。 – mdurant 2014-09-04 12:32:37