我有这个模型:
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
然后我添加了进入数据库的代码 然后我得到一个列表“listE”,我创建了一个模型实例,然后我将这个实例添加到 listeView 注意:此代码运行良好 问题仍然在于我如何在列表中获取所选项目
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)
请帮帮我
最佳答案
你要找的是,
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()
因此您可以使用 QListView.selectedIndexes() 或 QListView.selectionModel().selectedIndexes()
关于python - 如何获取从 QListView 中选择的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22538372/