码迷,mamicode.com
首页 > 其他好文 > 详细

pyqt基础之2:listModel基础

时间:2015-02-21 15:29:02      阅读:366      评论:0      收藏:0      [点我收藏+]

标签:

import sys

import os

 

from PyQt4 import QtCore, QtGui

from PyQt4.QtCore import * 

from PyQt4.QtGui import * 

 

class asset(QtGui.QDialog):

 

    def __init__(self,parent=None):

        super(asset, self).__init__(parent)

        self.assetList = QtGui.QListView(self)

        self.assetList.clicked.connect(self.on_treeView_clicked)

 

        ######################################################################

        # ----------------- ADD ITEMS----------------------------------------

        ######################################################################

 

        list_data = listDirs(‘D:\\‘)

        dir = listModel(list_data)

        self.assetList.setModel(dir)

 

        self.setStyleSheet(‘‘‘

 

                            *{

                            background-color : rgb(65,65,65);

                            color : rgb(210,210,210);

                            alternate-background-color:rgb(55,55,55);

                            }

 

                            QTreeView,QListView,QLineEdit{

                            background-color : rgb(50,50,50);

                            color : rgb(210,210,210);

                            }

 

                            ‘‘‘

                           )

        self.setFocus()

 

    @QtCore.pyqtSlot(QtCore.QModelIndex)

    def on_treeView_clicked(self, index):

        itms = self.assetList.selectedIndexes()

        for it in itms:

            print ‘selected item index found at %s‘ % it.row()

 

class listModel(QAbstractListModel): 

    def __init__(self, datain, parent=None, *args): 

        """ datain: a list where each item is a row

        """

        QAbstractListModel.__init__(self, parent, *args) 

        self.listdata = datain

 

    def rowCount(self, parent=QModelIndex()): 

        return len(self.listdata) 

 

    def data(self, index, role): 

        if index.isValid() and role == Qt.DisplayRole:

            return QVariant(self.listdata[index.row()])

        else: 

            return QVariant()

 

def listDirs(*path):

    completePath = os.path.join(*path)

    dirs = os.listdir(os.path.abspath(completePath))

    outputDir = []

    for dir in dirs:

        if os.path.isdir(os.path.join(completePath,dir)):

            outputDir.append(dir)

    return outputDir

 

 

 

if __name__ == "__main__":

 

    app = QtGui.QApplication(sys.argv)

    app.setStyle(‘plastique‘)

    main = asset()

    main.resize(200,200)

    main.show()

    sys.exit(app.exec_())   

 技术分享

pyqt基础之2:listModel基础

标签:

原文地址:http://www.cnblogs.com/mhxy13867806343/p/4296999.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!