How to prevent QTableView item from getting cleared on double-click - python

With QTableView set as editable using QAbstractTableModel's flag() method:
def flags(self, index):
return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable
double-clicking the QTableView's item puts this item into the editing mode. By default the pre-existing string disappears from the field and an entire item is blank. I wonder if this behavior can be avoided or overridden?
Here is the QTableView field before the user double-clicks it:
And here is how it looks on double-click:
EDITED WORKING CODE (Many thanks to M4rtini):
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys, os
class Model(QAbstractTableModel):
def __init__(self, parent=None, *args):
QAbstractTableModel.__init__(self, parent, *args)
self.items =[
['Row0_Column0','Row0_Column1','Row0_Column2'],
['Row1_Column0','Row1_Column1','Row1_Column2'],
['Row2_Column0','Row2_Column1','Row2_Column2']
]
def flags(self, index):
return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable
def rowCount(self, parent):
return len(self.items)
def columnCount(self, parent):
return 3
def data(self, index, role):
if not index.isValid(): return QVariant()
row=index.row()
column=index.column()
if row>len(self.items): return QVariant()
if column>len(self.items[row]): return QVariant()
if role == Qt.EditRole or role == Qt.DisplayRole:
return QVariant(self.items[row][column])
return QVariant()
def setData(self, index, value, role=Qt.EditRole):
if index.isValid():
if role == Qt.EditRole:
row = index.row()
column=index.column()
if row>len(self.items) or column>len(self.items[row]):
return False
else:
self.items[row][column]=value
return True
return False
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
tablemodel=Model(self)
tableview=QTableView(self)
tableview.setModel(tablemodel)
layout=QVBoxLayout(self)
layout.addWidget(tableview)
self.setLayout(layout)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())

You need to sett the return value for data when called with EditRole:
def data(self, index, role):
if not index.isValid(): return false
row=index.row()
if row>len(self.items): return false
if role == Qt.DisplayRole or role == Qt.EditRole:
return self.items[row]

Related

QTableView clicked signal never emited?

I'm starting be more an more confused. I cannot make a QTableView emit its signal like I would like. I reduced my case to something less messy, and even in that case I cannot get any signals to be fired when I click.
For example in that code the slot "onClickedRow" is called once when starting the app (I don't know why), but then I can click as much as I want anywhere and the slot is never called :
import sys
from PySide2 import QtWidgets, QtCore, QtGui
class Message(QtCore.QAbstractItemModel):
def __init__(self):
super().__init__()
self.messageList = []
def addMessage(self, typeName, data):
self.messageList.append({"type": typeName,
"data": data})
def data(self, index, role):
if not index.isValid():
return None
if role != QtCore.Qt.DisplayRole:
return None
item = self.messageList[index.row()]
if index.column() == 0:
return str(item["type"])
else:
return str(item["data"])
def headerData(self, section, orientation, role):
if orientation == QtCore.Qt.Horizontal:
if role == QtCore.Qt.DisplayRole:
if section == 0:
return "type"
else:
return "data"
return None
def parent(self, index):
if not index.isValid():
return QtCore.QModelIndex()
return QtCore.QModelIndex()
def index(self, row, column, parent):
if not self.hasIndex(row, column, parent):
return QtCore.QModelIndex()
else:
return self.createIndex(row, column)
def flags(self, index):
if not index.isValid():
return QtCore.Qt.NoItemFlags
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
def columnCount(self, parent):
return 2
def rowCount(self, parent):
return len(self.messageList)
class FormMessageJournal(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.layout = QtWidgets.QVBoxLayout()
self.messageTable = QtWidgets.QTableView(self)
self.messageTable.clicked.connect(self.onClickedRow())
self.messageList = Message()
self.messageList.addMessage("Send", "Hello")
self.messageList.addMessage("Send", "Hello")
self.messageList.addMessage("Send", "Hello")
self.messageList.addMessage("Send", "Hello")
self.messageTable.setModel(self.messageList)
self.layout.addWidget(self.messageTable)
self.setLayout(self.layout)
def onClickedRow(self, index=None):
print("Click !")
if __name__ == "__main__":
app = QtWidgets.QApplication([])
widget = FormMessageJournal()
widget.show()
sys.exit(app.exec_())
Am I the only one having that type of issues?
self.messageTable.clicked.connect(self.onClickedRow())
Change to:
self.messageTable.clicked.connect(self.onClickedRow)

PyQt QAbstractTableModel checkbox not checkable

I am using own table model with QAbstractTableModel, where I have first col with checkbox (checkable cause flags Qt.ItemIsUserCheckable | Qt.ItemIsSelectable | Qt.ItemIsEnabled). I have trouble when I am trying use checkboxes, cause they are not checkable (can not make check or uncheck in them) in showed table.
What am I doing wrong? I am using this methods in own table model class:
def data(self, index, role):
row = index.row()
col = index.column()
if role == Qt.DisplayRole:
return '{0}'.format(self.tableData[row][col])
if role == Qt.CheckStateRole:
if col == 0:
return Qt.Unchecked
else:
return None
def setData(self, index, value, role):
if not index.isValid():
return False
if (role == Qt.CheckStateRole):
if (index.data(Qt.CheckStateRole) == Qt.Checked):
return True
else:
return False
else:
return False
You have to store the states for it, we need to have something permanent as a reference, for this we use QPersistentModelIndex, in this case a dictionary where the key is the QPersistentModelIndex and the value is the state of QCheckBox.
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class TableModel(QAbstractTableModel):
def __init__(self, parent=None):
super(TableModel, self).__init__(parent)
self.tableData = [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
self.checks = {}
def columnCount(self, *args):
return 3
def rowCount(self, *args):
return 3
def checkState(self, index):
if index in self.checks.keys():
return self.checks[index]
else:
return Qt.Unchecked
def data(self, index, role=Qt.DisplayRole):
row = index.row()
col = index.column()
if role == Qt.DisplayRole:
return '{0}'.format(self.tableData[row][col])
elif role == Qt.CheckStateRole and col == 0:
return self.checkState(QPersistentModelIndex(index))
return None
def setData(self, index, value, role=Qt.EditRole):
if not index.isValid():
return False
if role == Qt.CheckStateRole:
self.checks[QPersistentModelIndex(index)] = value
return True
return False
def flags(self, index):
fl = QAbstractTableModel.flags(self, index)
if index.column() == 0:
fl |= Qt.ItemIsEditable | Qt.ItemIsUserCheckable
return fl
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
view = QTableView()
model = TableModel()
view.setModel(model)
view.show()
sys.exit(app.exec_())

Python 3.5.1: QVariant represents a mapped type and cannot be instantiated

I'm working with Python 3.5.1 and I'm trying to run this code but I have a problem with QVariant
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
class Model(QAbstractTableModel):
def __init__(self, parent=None, *args):
QAbstractTableModel.__init__(self, parent, *args)
self.items = ['Item_A_001','Item_A_002','Item_B_001','Item_B_002']
def rowCount(self, parent=QModelIndex()):
return len(self.items)
def columnCount(self, parent=QModelIndex()):
return 1
def data(self, index, role):
if not index.isValid(): return QVariant()
elif role != Qt.DisplayRole:
return QVariant()
row=index.row()
if row<len(self.items):
return QVariant(self.items[row])
else:
return QVariant()
class Proxy(QSortFilterProxyModel):
def __init__(self):
super(Proxy, self).__init__()
self.filterActive = False
def setView(self, view):
self._view = view
def filterAcceptsRow(self, row, parent):
if self.filterActive and '_B_' in self.sourceModel().data(self.sourceModel().index(row, 0), Qt.DisplayRole).toPyObject():
self._view.selectRow(row)
return True
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
tableModel=Model(self)
proxyModel=Proxy()
proxyModel.setSourceModel(tableModel)
self.tableview=QTableView(self)
self.tableview.setModel(proxyModel)
self.tableview.horizontalHeader().setStretchLastSection(True)
self.tableview.setSelectionMode(QAbstractItemView.MultiSelection)
proxyModel.setView(self.tableview)
button=QPushButton(self)
button.setText('Select Items with B')
button.clicked.connect(self.clicked)
layout = QVBoxLayout(self)
layout.addWidget(self.tableview)
layout.addWidget(button)
self.setLayout(layout)
def clicked(self, arg):
proxyModel=self.tableview.model()
self.tableview.clearSelection()
proxyModel.filterActive = True
proxyModel.invalidateFilter()
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
Indeed, when I execute this code an error message appears: QVariant Represents a mapped kind and can not be instantiated .
I tried to remove the QVariant and putting None inplace
def data(self, index, role):
if not index.isValid(): return None
elif role != Qt.DisplayRole:
return None
row=index.row()
if row<len(self.items):
return (self.items[row])
else:
return None
But a new error message appears: 'str' object has no attribute 'toPyObject'.
Thank you for helping me
As you've found out, by default, QVariant does not exist in PyQt4 when used with Python3. Instead, everything is automatically converted to an equivalent python type. So all you need to do is use None wherever QVariant.null would normally be expected, and avoid attempting to use any QVariant methods (such as toPyObject, which is obviously redundant anyway).

How to filter and sort Items using Model and QTableView

There are three QTableViews: A, B, C. All three share the same SourceModel.
There are three column names: "Items A", "Items B" and "Items C" stored in self.columnNames list.
Each QTableView was assigned an instance of the same ProxyModel with proxy's objectName set to "proxyA", "proxyB" and "proxyC".
I want the left-side QTableView to list only the items "A" using a single column with its header named "Items A".
The middle tableViewB should only list "Items_B" with the column-header called "Items B".
And the third tableView should list only items "C" with the column name set to "Items C".
The source code is below. I really wanna know how to do it.
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys, os
class Model(QAbstractTableModel):
def __init__(self, parent=None, *args):
QAbstractTableModel.__init__(self, parent, *args)
self.items =[
['Item_A_0','Item_A_12','Item_C_330'],
['Item_C_20','Item_B_11','Item_B_12'],
['Item_C_101','Item_A_24','Item_B_77']
]
self.columnNames=['Items A', 'Items B', 'Items C']
def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self.columnNames[col]
def flags(self, index):
return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable
def rowCount(self, parent):
return len(self.items)
def columnCount(self, parent):
return len(self.columnNames)
def data(self, index, role):
if not index.isValid(): return QVariant()
row=index.row()
column=index.column()
if row>len(self.items): return QVariant()
if column>len(self.items[row]): return QVariant()
columnName=self.headerData(column, Qt.Horizontal, Qt.DisplayRole)
if role == Qt.EditRole or role == Qt.DisplayRole:
return QVariant(self.items[row][column])
return QVariant()
def setData(self, index, value, role=Qt.EditRole):
if index.isValid():
if role == Qt.EditRole:
row = index.row()
column=index.column()
if row>len(self.items) or column>len(self.items[row]):
return False
else:
self.items[row][column]=value
return True
return False
class Proxy(QSortFilterProxyModel):
def __init__(self):
super(Proxy, self).__init__()
def filterAcceptsRow(self, row, parent):
sourceModel=self.sourceModel()
columns=sourceModel.columnCount(QModelIndex())
return True
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
tablemodel=Model(self)
proxyA=Proxy()
proxyA.setObjectName('proxyA')
proxyA.setSourceModel(tablemodel)
tableviewA=QTableView(self)
tableviewA.setModel(proxyA)
proxyB=Proxy()
proxyB.setObjectName('proxyB')
proxyB.setSourceModel(tablemodel)
tableviewB=QTableView(self)
tableviewB.setModel(proxyB)
proxyC=Proxy()
proxyC.setObjectName('proxyC')
proxyC.setSourceModel(tablemodel)
tableviewC=QTableView(self)
tableviewC.setModel(proxyC)
layout=QHBoxLayout(self)
layout.addWidget(tableviewA)
layout.addWidget(tableviewB)
layout.addWidget(tableviewC)
self.setLayout(layout)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())

How to clean the mess after items deletion using model()

The code below creates a single QListView with model. Clicking its item deletes it from the model. But QListView still reports that the number of model's items remains unchanged (like there were no items deleted). Is there any way to fix it?
class Model(QtCore.QAbstractListModel):
def __init__(self):
QtCore.QAbstractListModel.__init__(self)
self.items=[]
self.modelDict={}
def rowCount(self, parent=QtCore.QModelIndex()):
return len(self.modelDict)
def data(self, index, role):
if not index.isValid():
return QtCore.QVariant()
if not (0 <= index.row() < len(self.items)):
return QtCore.QVariant()
if not index.isValid():
return QtCore.QVariant()
if role==QtCore.Qt.ItemDataRole:
key=str(index.data().toString())
returnedValue=self.modelDict.get(key)
return QtCore.QVariant(returnedValue)
elif role==QtCore.Qt.DisplayRole:
row=index.row()
itemTitle=self.items[row]
return QtCore.QVariant(itemTitle)
def addItems(self):
for key in self.modelDict:
index=QtCore.QModelIndex()
self.beginInsertRows(index, 0, 0)
self.items.append(key)
inst=self.modelDict.get(key)
self.setData(index, QtCore.QVariant(inst), QtCore.Qt.DisplayRole)
self.endInsertRows()
def removeByIndex(self, index):
if not index.isValid(): return
row=index.row()
self.beginRemoveRows(QtCore.QModelIndex(), row, 0)
self.items=self.items[:row]+self.items[row+1:]
self.endRemoveRows()
class ListView(QtGui.QListView):
def __init__(self):
super(ListView, self).__init__()
self.model= Model()
self.model.modelDict=elements
self.model.addItems()
self.setModel(self.model)
self.clicked.connect(self.itemClicked)
self.show()
def itemClicked(self, index):
print 'NUMBER OF ITEMS BEFORE DELETE: %s'%self.model.rowCount()
self.model.removeByIndex(index)
print 'NUMBER OF ITEMS AFTER DELETE: %s'%self.model.rowCount()
window=ListView()
sys.exit(app.exec_())
Fully working example on how to delete items from model.
import os,sys
from PyQt4 import QtCore, QtGui
app=QtGui.QApplication(sys.argv)
elements={'Animals':{1:'Bison',2:'Panther',3:'Elephant'},'Birds':{1:'Duck',2:'Hawk',3:'Pigeon'},'Fish':{1:'Shark',2:'Salmon',3:'Piranha'}}
class Model(QtCore.QAbstractListModel):
def __init__(self):
QtCore.QAbstractListModel.__init__(self)
self.items=[]
self.modelDict={}
def rowCount(self, parent=QtCore.QModelIndex()):
return len(self.items)
def data(self, index, role):
if not index.isValid():
return QtCore.QVariant()
if not (0 <= index.row() < len(self.items)):
return QtCore.QVariant()
if not index.isValid():
return QtCore.QVariant()
if role==QtCore.Qt.ItemDataRole:
key=str(index.data().toString())
returnedValue=self.modelDict.get(key)
return QtCore.QVariant(returnedValue)
elif role==QtCore.Qt.DisplayRole:
row=index.row()
itemTitle=self.items[row]
return QtCore.QVariant(itemTitle)
def addItems(self):
for key in self.modelDict:
index=QtCore.QModelIndex()
self.beginInsertRows(index, 0, 0)
self.items.append(key)
inst=self.modelDict.get(key)
self.setData(index, QtCore.QVariant(inst), QtCore.Qt.DisplayRole)
self.endInsertRows()
def removeByIndex(self, index):
if not index.isValid(): return
row=index.row()
self.beginRemoveRows(QtCore.QModelIndex(), row, 0)
self.items=self.items[:row]+self.items[row+1:]
key=str(index.data().toString())
if self.modelDict.get(key): self.modelDict.pop(key,None)
self.endRemoveRows()
class ListView(QtGui.QListView):
def __init__(self):
super(ListView, self).__init__()
self.model= Model()
self.model.modelDict=elements
self.model.addItems()
self.setModel(self.model)
self.clicked.connect(self.itemClicked)
self.show()
def itemClicked(self, index):
print 'NUMBER OF ITEMS BEFORE DELETE: %s'%self.model.rowCount()
self.model.removeByIndex(index)
print 'NUMBER OF ITEMS AFTER DELETE: %s'%self.model.rowCount()
window=ListView()
sys.exit(app.exec_())

Categories