pythonqtpython-2.7pyqt

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


Solution

  • What are you are looking for is,

    https://doc.qt.io/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()