2010-09-05 69 views
2

我並不習慣在代碼中編寫UI,所以我需要一些指針。 我想在我的N900上創建一個簡單的水平滾動對話框,但我無法弄清楚如何做到這一點。在Maemo5上使用PyQT滾動區域

這是我到目前爲止有:


    def __init__(self,parent = None): 

     QDialog.__init__(self,parent) 
     #if name == None: 
     self.setWindowTitle('Testing scrolling') 
     self.scrollArea = QScrollArea(self) 
     self.scrollArea.setWidgetResizable(True) 
     self.scrollArea.setMinimumSize(100,150) 
     self.aWidget = QWidget(self.scrollArea) 
     self.aWidget.setMinimumSize(20,200) 
     self.aWidget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) 
     self.scrollArea.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) 
     self.scrollArea.setWidget(self.aWidget) 
     scroller = self.scrollArea.property("kineticScroller").toPyObject() 
     scroller.setEnabled(True) 

     _layout = QGridLayout(self.aWidget) 
     _layout.setSpacing(60) 
     _layout.setMargin(11) 

     _layout.addWidget(QPushButton('Test0'),0,0) 
     _layout.addWidget(QPushButton('Test1'),0,1) 
     _layout.addWidget(QPushButton('Test2'),0,2) 
     _layout.addWidget(QPushButton('Test3'),0,3) 
     _layout.addWidget(QPushButton('Test4'),0,4) 
     _layout.addWidget(QPushButton('Test5'),0,5) 
     _layout.addWidget(QPushButton('Test6'),0,6) 

回答

4

PLS,檢查下面的例子會幫助你,它應該創建水平滾動區,並在其按鈕的對話框。

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

     self.setWindowTitle('Testing scrolling') 
     self.setGeometry(250, 200, 350, 400) 

     widget = QWidget() 
     widgetLayout = QHBoxLayout() 
     for i in range(0, 25): 
      button = QPushButton("test button {0}".format(i)) 
      widgetLayout.addWidget(button)   
     widget.setLayout(widgetLayout) 

     scrollArea = QScrollArea() 
     scrollArea.setWidget(widget) 

     dialogLayout = QVBoxLayout() 
     dialogLayout.addWidget(scrollArea)  
     self.setLayout(dialogLayout) 

if __name__ == "__main__": 
    app = QtGui.QApplication(sys.argv) 
    dlg = MyDialog() 
    dlg.show() 
    sys.exit(app.exec_()) 

希望這會有所幫助,至於

+0

工作就像一個魅力,謝謝! - 奇怪的是,'.format(i)'部分不起作用。這不是Python的基本功能嗎? – skerit 2010-09-06 20:27:58

+0

是的,它是2.6版本的新功能。這裏詳細http://docs.python.org/library/stdtypes.html尋找str.format(* args,** kwargs) – 2010-09-06 20:30:49