一半君的总结纸

听话只听一半君

QListWidget QTableWidget 和 QTreeWidget 亦即Convenience Item Widgets 备忘录

一句话总结就是简单的情况下用widget,复杂的情况下用view

QListWidget, QTableWidget, 和 QTreeWidget 这三个所谓叫做Convenience Item Widgets的widget是自带models和delegate的

把大神们的例子总结改短一点

  • QListWidget

    qlistwidget

    
    import sys
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    
    data = ['row1', 'row2', 'row3']
    
    
    class MyList(QListWidget):
    
        def __init__(self, data, parent=None):
            super(MyList, self).__init__(parent)
    
            vertical_headers = []
            for d in data:
                newitem = QListWidgetItem(d)
                self.addItem(newitem)
                self.setAlternatingRowColors(True)
    
    
    def main(args):
        app = QApplication(args)
        my_list = MyList(data)
        my_list.show()
        sys.exit(app.exec_())
    
    if __name__ == "__main__":
        main(sys.argv)
    
  • QTableWidget

    qtablewidget

    import sys
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    
    data = {'col1': ['1', '2', '3'],
            'col2': ['4', '5', '6'],
            'col3': ['7', '8', '9']}
    
    
    class MyTable(QTableWidget):
    
        def __init__(self, data, *args):
            super(MyTable,self).__init__(*args)
    
            # fill the table
            horHeaders = []
            for n, key in enumerate(sorted(data.keys())):
                horHeaders.append(key)
                for m, item in enumerate(data[key]):
                    newitem = QTableWidgetItem(item)
                    self.setItem(m, n, newitem)
            self.setHorizontalHeaderLabels(horHeaders)
    
            self.resizeColumnsToContents()
            self.resizeRowsToContents()
            self.horizontalHeader().setStretchLastSection(True)
    
            self.move(120,120)
    
    def main(args):
        app = QApplication(args)
        table = MyTable(data, 5, 3)
        table.show()
        sys.exit(app.exec_())
    
    if __name__ == "__main__":
        main(sys.argv)
    
    
  • QTreeWidget

    QTreeWidget

    import sys
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    
    data = {'level1': ['1', '2', '3'],
            'level2': ['4', '5', '6'],
            'level3': ['7', '8', '9']}
    
    
    class MyTree(QTreeWidget):
    
        def __init__(self, data, parent=None):
            super(MyTree, self).__init__(parent)
            self.setHeaderLabel("Tree Demo")
            self.setColumnCount(1)
            root_item = self.invisibleRootItem()
            for key, val in sorted(data.iteritems()):
                child = QTreeWidgetItem()
                child.setText(0, key)
                root_item.addChild(child)
                child.setExpanded(True)
                for v in val:
                    grand_child = QTreeWidgetItem()
                    grand_child.setText(0, v)
                    child.addChild(grand_child)
    
            self.move(120, 120)
            self.resize(220,300)
    
    
    def main(args):
        app = QApplication(args)
        table = MyTree(data)
        table.show()
        sys.exit(app.exec_())
    
    if __name__ == "__main__":
        main(sys.argv)
    

参考:

PyQt4: example of TableWidget usage
Rapid GUI Programming with Python and Qt
QTreeWidget to Mirror python Dictionary

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: