简体   繁体   中英

how to get the item selected from QListView?

i have this model:

class PaletteListModel(QtCore.QAbstractListModel):
    def __init__(self,colors = [[]],headers =[],parent=None):
        QtCore.QAbstractTableModel.__init__(self, parent)
        self.__colors=colors

    def columnCount(self,parent): 
        return 0


    def rowCount(self,parent): 
        return len(self.__colors)




    def data(self,index,role):
        if role==QtCore.Qt.EditRole:
            row=index.row()


            return self.__colors[row]
        if role==QtCore.Qt.FontRole:
            font=QtGui.QFont("Segoe UI")
            font.setPixelSize(20)

            return font
        if role == QtCore.Qt.ForegroundRole:
            brush = QtGui.QBrush()

            brush.setColor(QtGui.QColor("black"))
            return brush    
        if role ==QtCore.Qt.ToolTipRole:
            row=index.row()

            return "Mex code: "+self.__colors[row]

        if role==QtCore.Qt.DisplayRole:
            row=index.row()
            column=index.column()
            value=self.__colors[row]
            return value 
        if role==QtCore.Qt.DecorationRole:


            pixmap=QtGui.QPixmap(26,26)
            pixmap.load("E:\\Users\\HA\\workspace\\Projet\\copy-icon.png")
            icon=QtGui.QIcon(pixmap)
            return icon 
    def setData(self,index,value,role=QtCore.Qt.EditRole):
        if role==QtCore.Qt.EditRole:
            row =index.row()

            color=value

            self.__colors[row]=color
            self.dataChanged.emit(index,index)

            return False
    def flags(self, index):
        return QtCore.Qt.ItemIsEnabled|QtCore.Qt.ItemIsSelectable|QtCore.Qt.ItemIsUserCheckable

and I add the code to enter the database then I get a list "listE", I created an instance of the model then I add this instance the listeView Note: this code works well the problem remains on how I get the selected item in the list

    cnx=Connexion()
    cnx.ouvrirConnexion()
    c = cnx.connexion
    cursor = cnx.connexion.cursor()            

    cursor.execute('SELECT titre From "Etude"')
    rows = cursor.fetchall()
    ListeE=[]
    for row in rows:

        ListeE.append(row[0])

    cnx.fermerConnexion()
    modele=PaletteListModel(ListeE)
    self.listEtude.setModel(modele)

help me please

What are you are looking for is,

http://qt-project.org/doc/qt-5/qabstractitemview.html#selectedIndexes

This convenience function returns a list of all selected and non-hidden item indexes in the view. The list contains no duplicates, and is not sorted.

See also QItemSelectionModel::selectedIndexes()

so you can either use QListView.selectedIndexes(), or QListView.selectionModel().selectedIndexes()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM