Sort QTableView in pyqt5 - python

I want to sort a QTableView in PyQT5.
I found an example that uses PyQT4, but in PyQT5 SIGNALs are not existing anymore.
This is my example code
class MainWindow(QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
# create table
self.get_table_data()
table = self.createTable()
# layout
layout = QVBoxLayout()
layout.addWidget(table)
self.setLayout(layout)
def get_table_data(self):
stdouterr = os.popen("dir c:\\").read()
lines = stdouterr.splitlines()
lines = lines[5:]
lines = lines[:-2]
self.tabledata = [re.split(r"\s+", line, 4)
for line in lines]
def createTable(self):
# create the view
tv = QTableView()
# set the table model
header = ['date', 'time', '', 'size', 'filename']
tm = MyTableModel(self.tabledata, header, self)
tv.setModel(tm)
# set the minimum size
tv.setMinimumSize(400, 300)
# hide grid
tv.setShowGrid(False)
tv.setSelectionBehavior(QAbstractItemView.SelectRows)
# set the font
# hide vertical header
vh = tv.verticalHeader()
vh.setVisible(False)
# set horizontal header properties
hh = tv.horizontalHeader()
hh.setStretchLastSection(True)
# set column width to fit contents
tv.resizeColumnsToContents()
# set row height
nrows = len(self.tabledata)
for row in range(nrows):
tv.setRowHeight(row, 18)
# enable sorting
tv.setSortingEnabled(True)
return tv
self.setWindowTitle("Finance")
class MyTableModel(QAbstractTableModel):
def __init__(self, datain, headerdata, parent=None, *args):
""" datain: a list of lists
headerdata: a list of strings
"""
QAbstractTableModel.__init__(self, parent, *args)
self.arraydata = datain
self.headerdata = headerdata
def rowCount(self, parent):
return len(self.arraydata)
def columnCount(self, parent):
return len(self.arraydata[0])
def data(self, index, role):
if not index.isValid():
return QVariant()
elif role != Qt.DisplayRole:
return QVariant()
return QVariant(self.arraydata[index.row()][index.column()])
def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return QVariant(self.headerdata[col])
return QVariant()
def sort(self, Ncol, order):
"""Sort table by given column number.
"""
self.emit(SIGNAL("layoutAboutToBeChanged()"))
self.arraydata = sorted(self.arraydata, key=operator.itemgetter(Ncol))
if order == Qt.DescendingOrder:
self.arraydata.reverse()
self.emit(SIGNAL("layoutChanged()"))
if __name__ == '__main__':
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
helloPythonWidget = MainWindow()
helloPythonWidget.show()
sys.exit(app.exec_())
I tried many different ways to use self.layoutAboutToBeChanged() and pyqtSignal, but to be honest, I don't understand it, since I'm new to python and PyQT in general yet.
I tried to get infos from the Documentation, but i didn't get a clue from documentation and didn't found an good example on the web.
UPDATE:
I solved the puzzle:
self.layoutAboutToBeChanged.emit() emits the signal (codecompletion in eclipse is a bit misleading)
def sort(self, Ncol, order):
"""Sort table by given column number.
"""
self.layoutAboutToBeChanged.emit()
self.arraydata = sorted(self.arraydata, key=operator.itemgetter(Ncol))
if order == Qt.DescendingOrder:
self.arraydata.reverse()
self.layoutChanged.emit()
This is the solution

for those of you guys who wants to import pandas dataframe into qt model try this:
class PandasModel(QtCore.QAbstractTableModel):
def __init__(self, data, parent=None):
"""
:param data: a pandas dataframe
:param parent:
"""
QtCore.QAbstractTableModel.__init__(self, parent)
self._data = data
# self.headerdata = data.columns
def rowCount(self, parent=None):
return len(self._data.values)
def columnCount(self, parent=None):
return self._data.columns.size
def data(self, index, role=QtCore.Qt.DisplayRole):
if index.isValid():
if role == QtCore.Qt.DisplayRole:
return str(self._data.values[index.row()][index.column()])
return None
def headerData(self, rowcol, orientation, role):
# print(self._data.columns[rowcol])
# print(self._data.index[rowcol])
if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
return self._data.columns[rowcol]
if orientation == QtCore.Qt.Vertical and role == QtCore.Qt.DisplayRole:
return self._data.index[rowcol]
return None
def flags(self, index):
flags = super(self.__class__, self).flags(index)
flags |= QtCore.Qt.ItemIsEditable
flags |= QtCore.Qt.ItemIsSelectable
flags |= QtCore.Qt.ItemIsEnabled
flags |= QtCore.Qt.ItemIsDragEnabled
flags |= QtCore.Qt.ItemIsDropEnabled
return flags
def sort(self, Ncol, order):
"""Sort table by given column number.
"""
try:
self.layoutAboutToBeChanged.emit()
self._data = self._data.sort_values(self._data.columns[Ncol], ascending=not order)
self.layoutChanged.emit()
except Exception as e:
print(e)
I have forgotten where I got the base pandasmodel, probably from here

Related

Why does my QStyleItemDelegate pop up in a new window instead of in my TableView?

I'm trying to create a pretty simple app with Pyside2 that displays some data in a table that the user can edit. I want the cells of the table to autocomplete based on other values in the table, so I implemented a custom delegate following this tutorial. It was pretty straightforward and the delegate works as expected except for some reason the delegate pops up in a new window instead of being attached to the cell in the table, illustrated by this example (the custom delegate is defined at the end):
import pandas as pd
from PySide2 import QtWidgets, QtCore
from PySide2.QtCore import Qt
class TableView(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setObjectName(u"TableView")
self.centralwidget = QtWidgets.QWidget(self)
self.centralwidget.setObjectName(u"centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName(u"verticalLayout")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setObjectName(u"frame")
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.frame)
self.verticalLayout_2.setObjectName(u"verticalLayout_2")
self.tableView = QtWidgets.QTableView(self.frame)
self.tableView.setObjectName(u"tableView")
delegate = QAutoCompleteDelegate(self.tableView)
self.tableView.setItemDelegate(delegate)
self.tableModel = TableModel(self.tableView)
self.tableView.setModel(self.tableModel)
self.verticalLayout_2.addWidget(self.tableView)
self.verticalLayout.addWidget(self.frame)
self.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(self)
self.statusbar.setObjectName(u"statusbar")
self.setStatusBar(self.statusbar)
QtCore.QMetaObject.connectSlotsByName(self)
class TableModel(QtCore.QAbstractTableModel):
def __init__(self, parent=None):
super(TableModel, self).__init__(parent=parent)
self._data = pd.DataFrame([["A0", "B0", "C0"], ["A1", "B1", "C1"], ["A2", "B2", "C2"]], columns=["A", "B", "C"])
def data(self, index, role):
# Should only ever refer to the data by iloc in this method, unless you
# go specifically fetch the correct loc based on the iloc
row = index.row()
col = index.column()
if role == Qt.DisplayRole or role == Qt.EditRole:
return self._data.iloc[row, col]
def rowCount(self, index):
return len(self._data)
def columnCount(self, index):
return len(self._data.columns)
def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self._data.columns[col]
return None
def unique_vals(self, index):
""" Identify the values to include in an autocompletion delegate for the given index """
column = index.column()
col_name = self._data.columns[column]
return list(self._data[col_name].unique())
def setData(self, index, value, role):
if role != Qt.EditRole:
return False
row = self._data.index[index.row()]
column = self._data.columns[index.column()]
self._data.loc[row, column] = value
self.dataChanged.emit(index, index)
return True
def flags(self, index):
flags = super(self.__class__, self).flags(index)
flags |= Qt.ItemIsEditable
flags |= Qt.ItemIsSelectable
flags |= Qt.ItemIsEnabled
flags |= Qt.ItemIsDragEnabled
flags |= Qt.ItemIsDropEnabled
return flags
class QAutoCompleteDelegate(QtWidgets.QStyledItemDelegate):
def createEditor(self, parent: QtWidgets.QWidget, option: QtWidgets.QStyleOptionViewItem, index: QtCore.QModelIndex) -> QtWidgets.QWidget:
le = QtWidgets.QLineEdit()
test = ["option1", "option2", "option3"]
complete = QtWidgets.QCompleter(test)
le.setCompleter(complete)
return le
def setEditorData(self, editor:QtWidgets.QLineEdit, index:QtCore.QModelIndex):
val = index.model().data(index, Qt.EditRole)
options = index.model().unique_vals(index)
editor.setText(val)
completer = QtWidgets.QCompleter(options)
editor.setCompleter(completer)
def updateEditorGeometry(self, editor: QtWidgets.QWidget, option: QtWidgets.QStyleOptionViewItem, index: QtCore.QModelIndex):
editor.setGeometry(option.rect)
app = QtWidgets.QApplication()
tv = TableView()
tv.show()
app.exec_()
Image showing table behavior when a cell is active
From everything I've read, the updateEditorGeometry method on the custom delegate should anchor it to the active cell, but something clearly isn't working as expected in my case. And I am still learning Qt so I can't claim to understand what it is doing. Is there anyone out there who might be able to explain what is going on here? Much appreciated.
OS: Ubuntu 20.04
Python 3.8.5
PySide2 5.15.0
Qt 5.12.9
Any widget that does not have a parent will be a window and since the QLineEdit has no parent then you observe the behavior that the OP indicates, for that reason the createEditor method has "parent" as an argument.
On the other hand, it is better to create a custom QLineEdit that easily implements the handling of adding suggestions to the QCompleter through a model.
class AutoCompleteLineEdit(QtWidgets.QLineEdit):
def __init__(self, parent=None):
super().__init__(parent)
self._completer_model = QtGui.QStandardItemModel()
completer = QtWidgets.QCompleter()
completer.setModel(self._completer_model)
self.setCompleter(completer)
#property
def suggestions(self):
return [
self._completer_model.item(i).text()
for i in range(self._completer_model.rowCount())
]
#suggestions.setter
def suggestions(self, suggestions):
self._completer_model.clear()
for suggestion in suggestions:
item = QtGui.QStandardItem(suggestion)
self._completer_model.appendRow(item)
class QAutoCompleteDelegate(QtWidgets.QStyledItemDelegate):
def createEditor(
self,
parent: QtWidgets.QWidget,
option: QtWidgets.QStyleOptionViewItem,
index: QtCore.QModelIndex,
) -> QtWidgets.QWidget:
le = AutoCompleteLineEdit(parent)
return le
def setEditorData(self, editor: QtWidgets.QWidget, index: QtCore.QModelIndex):
val = index.model().data(index, Qt.EditRole)
options = index.model().unique_vals(index)
editor.setText(val)
editor.suggestions = options

QSortFilterProxyModel by column value

Example of table model data
I have a gui with a tabWidget and inside each I have a tableview. Each tab describes a folder (see column type) with subdirs from which I pull the data. I want to have one main model that drives all views by attaching a QSortFilterProxy Model inbetween each tableview that filters the main model for the "type" of each individual TableView (passed into the subclass RenderTypeProxyModel). Bonus: Ideally, they should be sorted as well, so that the most recent view (see date column) comes out on top.
Here is my current version but the table remains blank for a reason I can't figure out:
import sys
import os
from datetime import datetime
from pprint import pprint
from PySide2 import QtCore, QtGui, QtWidgets
#To be replaced by env variable
pathToProject = "/run/media/centos7/Data/Projects/Programming/Pipeline/SampleProject"
allowedExportTypes = ["img-prv", "img-final", "img-cg", "img-src", "camera"]
class ExportTableModel(QtCore.QAbstractTableModel):
def __init__(self, exportData, horizontalHeaders, parent=None):
QtCore.QAbstractTableModel.__init__(self, parent)
self.__exportData = exportData
self.__horizontalHeaders = horizontalHeaders
def rowCount(self, parent):
return len(self.__exportData)
def columnCount(self, parent):
return len(self.__horizontalHeaders)
def data(self, index, role): #Returns the data stored under the given role for the item referred to by the index.
if role == QtCore.Qt.DisplayRole:
row = index.row()
column = index.column()
value = self.__exportData[row][column]
return value
def headerData(self, section, orientation, role):
if role == QtCore.Qt.DisplayRole:
if orientation == QtCore.Qt.Horizontal:
if section < len(self.__horizontalHeaders):
return self.__horizontalHeaders[section]
else:
return "not implemented"
def tableSetup(tableView):
tableView.setAlternatingRowColors(True)
tableView.setSelectionBehavior(QtWidgets.QTableView.SelectRows)
tableView.setSortingEnabled(True)
class RenderTypeProxyModel (QtCore.QSortFilterProxyModel): #Custom Proxy Model
def __init__(self, type, parent=None):
super(RenderTypeProxyModel,self).__init__(parent)
self.__type = type
def filterAcceptsRow(self, row, parent): #returns true if the given row should be included in the model
model = self.sourceModel()
index = model.index(row, 3, parent)
if model.data(index,QtCore.Qt.DisplayRole) == type:
return True
else:
return False
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
horizontalHeaders = ["status", "comment", "task", "type", "version", "user", "date", "filepath"] #horizontal header data
#exportData = #insert sample data list of lists here for testing
tableView = QtWidgets.QTableView()
tableView.show()
model = ExportTableModel(exportData,horizontalHeaders)
proxyModel = RenderTypeProxyModel("img-prv")
proxyModel.setSourceModel(model)
tableView.setModel(proxyModel)
tableSetup(tableView)
sys.exit(app.exec_())
Here is sample data to use for debugging: https://pastebin.com/AB1XvKju
Just feed it into the exportData variable inside the __main__ method.
The error is caused because type is a reserved word in python:
if model.data(index,QtCore.Qt.DisplayRole) == type:
you must use self.__type.
On the other hand if you want to sort the data by date it is not necessary to enable setSortingEnabled(), just use sort().
from PySide2 import QtCore, QtGui, QtWidgets
#To be replaced by env variable
allowedExportTypes = ["img-prv", "img-final", "img-cg", "img-src", "camera"]
class ExportTableModel(QtCore.QAbstractTableModel):
def __init__(self, exportData, horizontalHeaders, parent=None):
super(ExportTableModel, self).__init__(parent)
self.__exportData = exportData
self.__horizontalHeaders = horizontalHeaders
def rowCount(self, parent=QtCore.QModelIndex()):
return len(self.__exportData)
def columnCount(self, parent=QtCore.QModelIndex()):
return len(self.__horizontalHeaders)
def data(self, index, role=QtCore.Qt.DisplayRole):
if role == QtCore.Qt.DisplayRole:
row = index.row()
column = index.column()
value = self.__exportData[row][column]
return value
def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole):
if role == QtCore.Qt.DisplayRole and orientation == QtCore.Qt.Horizontal:
if section < self.columnCount():
return self.__horizontalHeaders[section]
return "not implemented"
def tableSetup(tableView):
tableView.setAlternatingRowColors(True)
tableView.setSelectionBehavior(QtWidgets.QTableView.SelectRows)
class RenderTypeProxyModel (QtCore.QSortFilterProxyModel): #Custom Proxy Model
def __init__(self, _type, parent=None):
super(RenderTypeProxyModel,self).__init__(parent)
self.__type = _type
def filterAcceptsRow(self, row, parent):
_type = self.sourceModel().index(row, 3, parent).data()
return _type == self.__type
def lessThan(self, left, right):
fmt = "yyyy-MM-dd hh:mm:ss"
left_data = self.sourceModel().data(left)
right_data = self.sourceModel().data(right)
return QtCore.QDateTime.fromString(left_data, fmt) < QtCore.QDateTime.fromString(right_data, fmt)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
horizontalHeaders = ["status", "comment", "task", "type", "version", "user", "date", "filepath"] #horizontal header data
exportData = # ...
model = ExportTableModel(exportData, horizontalHeaders)
tab_widget = QtWidgets.QTabWidget()
for _type in allowedExportTypes:
tableView = QtWidgets.QTableView()
c = horizontalHeaders.index("date")
tableSetup(tableView)
proxy = RenderTypeProxyModel(_type, tableView)
proxy.setSourceModel(model)
proxy.sort(c, QtCore.Qt.AscendingOrder)
tableView.setModel(proxy)
tab_widget.addTab(tableView ,_type)
tab_widget.show()
sys.exit(app.exec_())
Update:
In the next part I have made improvements to your original code avoiding overwriting unnecessary methods and adding a delegate:
from PySide2 import QtCore, QtGui, QtWidgets
#To be replaced by env variable
allowedExportTypes = ["img-prv", "img-final", "img-cg", "img-src", "camera"]
class DateDelegate(QtWidgets.QStyledItemDelegate):
def initStyleOption(self, option, index):
super(DateDelegate, self).initStyleOption(option, index)
option.text = index.data().toString("yyyy-MM-dd hh:mm:ss")
class ExportTableModel(QtCore.QAbstractTableModel):
def __init__(self, exportData, horizontalHeaders, parent=None):
super(ExportTableModel, self).__init__(parent)
self.__exportData = exportData
self.__horizontalHeaders = horizontalHeaders
def rowCount(self, parent=QtCore.QModelIndex()):
return len(self.__exportData)
def columnCount(self, parent=QtCore.QModelIndex()):
return len(self.__horizontalHeaders)
def data(self, index, role=QtCore.Qt.DisplayRole):
if role == QtCore.Qt.DisplayRole:
row = index.row()
column = index.column()
value = self.__exportData[row][column]
header = self.headerData(column, QtCore.Qt.Horizontal)
if header == "date":
value = QtCore.QDateTime.fromString(value, "yyyy-MM-dd hh:mm:ss")
return value
def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole):
if role == QtCore.Qt.DisplayRole and orientation == QtCore.Qt.Horizontal:
if section < self.columnCount():
return self.__horizontalHeaders[section]
return "not implemented"
def create_tableview():
tableView = QtWidgets.QTableView()
tableView.setAlternatingRowColors(True)
tableView.setSelectionBehavior(QtWidgets.QTableView.SelectRows)
return tableView
class RenderTypeProxyModel (QtCore.QSortFilterProxyModel):
def __init__(self, c_type, _type, c_date, model, parent=None):
super(RenderTypeProxyModel,self).__init__(parent)
self.setSourceModel(model)
self.setFilterKeyColumn(c_type)
self.setFilterFixedString(_type)
self.sort(c_date, QtCore.Qt.AscendingOrder)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
horizontalHeaders = ["status", "comment", "task", "type", "version", "user", "date", "filepath"] #horizontal header data
exportData = # ...
c_date = horizontalHeaders.index("date")
c_type = horizontalHeaders.index("type")
tab_widget = QtWidgets.QTabWidget()
for _type in allowedExportTypes:
tableView = create_tableview()
tab_widget.addTab(tableView ,_type)
proxy = RenderTypeProxyModel(c_type, _type, c_date, model, tableView)
tableView.setModel(proxy)
delegate = DateDelegate(tableView)
tableView.setItemDelegateForColumn(c_date, delegate)
tab_widget.show()
sys.exit(app.exec_())

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)

how to set default value of Qdatetimeedit used as qdelegate

I'm using QDateTimeEdit as a delegate on my QTableview to show start date and end date.
when I try to populate data I receive from database, QDateTimeEdit delegate does not display it.
Here is my code:
Class DateDelegate:
class DateDelegate(QtGui.QItemDelegate):
def __init__(self, parent):
QtGui.QItemDelegate.__init__(self, parent)
def createEditor(self, parent, option, index):
self.dateEdit = QtGui.QDateTimeEdit(parent)
self.dateEdit.setCalendarPopup(True)
self.dateEdit.setMinimumDate(QtCore.QDate(2014, 03, 01))
self.dateEdit.setDisplayFormat(_translate("Form", "dd/mm/yyyy", None))
return self.dateEdit
def setModelData(self, editor, model, index):
value = self.dateEdit.dateTime().toPyDateTime()
strDate = value.strftime('%d/%m/%Y')
model.setData(index, strDate, QtCore.Qt.EditRole)
Class AssetTableModel:
class AssetTableModel(QtCore.QAbstractTableModel):
def __init__(self, assets = [], headers = [], parent = None):
QtCore.QAbstractTableModel.__init__(self, parent)
self.__assets = assets
self.__headers = headers
def rowCount(self, parent):
return len(self.__assets)
def columnCount(self, parent):
return len(self.__assets[0])
def flags(self, index):
return QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsEditable
def data(self, index, role):
row = index.row()
column = index.column()
if role == QtCore.Qt.EditRole:
return self.__assets[row][column]
if role == QtCore.Qt.DisplayRole:
print self.__assets[row][column]
return self.__assets[row][column]
def setData(self, index, value, role = QtCore.Qt.EditRole):
if role == QtCore.Qt.EditRole:
row = index.row()
column = index.column()
self.__assets[row][column] = value
self.dataChanged.emit(index, index)
return True
return False
def headerData(self, section, orientation, role):
if role == QtCore.Qt.DisplayRole:
if orientation == QtCore.Qt.Horizontal:
if section < len(self.__headers):
return self.__headers[section]
else:
return "not implimented"
else:
return "verticle not implimented"
def insertRows(self, position, rows, parent = QtCore.QModelIndex()):
self.beginInsertRows( parent, position, position + rows - 1 )
for i in range(rows):
defaultValues = [ "" for c in range( self.columnCount( None ) ) ]
self.__assets.insert( position, defaultValues )
self.endInsertRows()
return True
Class AssetWidget:
class AssetWidget(QtGui.QDialog):
def __init__(self, parent = None):
super(AssetWidget, self).__init__(parent)
uic.loadUi(uipath+'/AssetTable.ui', self)
# DB call here
self.loadAssetData()
# db call ends here
self.model = None
self.fillCombo(self.assetType)
self.cellDelegate = CellDelegate(self)
for i in range(10):
self.assetTV.setItemDelegateForColumn(i, self.cellDelegate)
self.sDateDelegate = DateDelegate(self)
self.assetTV.setItemDelegateForColumn(10, self.sDateDelegate )
self.assetTV.setItemDelegateForColumn(11, self.sDateDelegate)
self.connect(self.assettypeCB, QtCore.SIGNAL("currentIndexChanged(int)"), self.loadAssets )
self.connect(self.closeBTN , QtCore.SIGNAL("clicked()"), self.close )
self.connect(self.addRowBTN, QtCore.SIGNAL("clicked()"), self.addRow )
self.connect(self.assetTV, QtCore.SIGNAL("doubleClicked(QModelIndex)"), self.tableEdited )
self.show()
I think you are missing the setEditorData() method in your ItemDelegate.
From your attached sourcecode, I assume you are storing the date as a string? In my opinion, it is better to use a QDateTime object to store your date/time. If you do this, you do not need a ItemDelegate to provide an appropriate editor, because Qt knows which editor it needs to provide for this kind of datatype. (see Qt Documentation - Standard Editing Widgets.
However, if you still want to store your date as a string, see this sample program below on how to use delegtes.
from PyQt4 import QtCore
from PyQt4 import QtGui
import sys
class myModel(QtCore.QAbstractTableModel):
def __init__(self, parent):
QtCore.QAbstractTableModel.__init__(self, parent)
self.lst = []
#populate with a few dummy dates
#store dates as str values
dateTime = QtCore.QDateTime.currentDateTime()
for i in range(10):
strDate = dateTime.toString("dd/mm/yyyy")
self.lst.append([strDate])
dateTime = dateTime.addDays(1)
def rowCount(self, parent = QtCore.QModelIndex()):
return len(self.lst)
def columnCount(self, parent = QtCore.QModelIndex()):
return 1
def data(self, index, role = QtCore.Qt.DisplayRole):
row = index.row()
col = index.column()
if role == QtCore.Qt.DisplayRole:
return self.lst[row][col]
if role == QtCore.Qt.EditRole:
return self.lst[row][col]
def setData(self, index, value, role = QtCore.Qt.EditRole):
row = index.row()
col = index.column()
self.lst[row][col] = value
def flags(self, index):
return (QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEditable)
class DateDelegate(QtGui.QItemDelegate):
def __init__(self, parent):
QtGui.QItemDelegate.__init__(self, parent)
def createEditor(self, parent, option, index):
dateTimeEdit = QtGui.QDateTimeEdit(parent) #create new editor
#set properties of editor
dateTimeEdit.setDisplayFormat("dd/mm/yyyy")
dateTimeEdit.setCalendarPopup(True)
return dateTimeEdit
def setModelData(self, editor, model, index):
value = editor.dateTime().toString("dd/mm/yyyy")
model.setData(index, value)
def setEditorData(self, editor, index):
value = index.model().data(index, QtCore.Qt.EditRole)
qdate = QtCore.QDateTime().fromString(value, "dd/mm/yyyy")
editor.setDateTime(qdate)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
table = QtGui.QTableView()
data = myModel(table)
table.setModel(data)
d = DateDelegate(table)
table.setItemDelegateForColumn(0, d)
table.resize(800, 600)
table.show()
sys.exit(app.exec_())

pyqt - Change row~cell color in TableView

I have a QTableView with three columns
The second column is about numbers, there are only three types: 1, -1 and 0.
I want to have different colors for this three "types" of numbers (1,-1,0), coloring their rows with different colors. How can i do it?
self.tableView = QTableView(self.tabSentimento)
self.tableView.setGeometry(QRect(550,10,510,700))
self.tableView.setObjectName(_fromUtf8("TabelaSentimento"))
self.tableView.setModel(self.model)
self.tableView.horizontalHeader().setStretchLastSection(True)
obs: I used horizontalheader().setStrechLastSection(True) because I opened an existing csv file (using a button) into my tableview.
You have to define the color in the model, not in the view:
def data(self, index, role):
...
if role == Qt.BackgroundRole:
return QBrush(Qt.yellow)
Edit:
Here's a working example, except for the color part completely stolen from http://www.saltycrane.com/blog/2007/06/pyqt-42-qabstracttablemodelqtableview/
from PyQt4.QtCore import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
my_array = [['00','01','02'],
['10','11','12'],
['20','21','22']]
def main():
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
class MyWindow(QTableView):
def __init__(self, *args):
QTableView.__init__(self, *args)
tablemodel = MyTableModel(my_array, self)
self.setModel(tablemodel)
class MyTableModel(QAbstractTableModel):
def __init__(self, datain, parent=None, *args):
QAbstractTableModel.__init__(self, parent, *args)
self.arraydata = datain
def rowCount(self, parent):
return len(self.arraydata)
def columnCount(self, parent):
return len(self.arraydata[0])
def data(self, index, role):
if not index.isValid():
return QVariant()
# vvvv this is the magic part
elif role == Qt.BackgroundRole:
if index.row() % 2 == 0:
return QBrush(Qt.yellow)
else:
return QBrush(Qt.red)
# ^^^^ this is the magic part
elif role != Qt.DisplayRole:
return QVariant()
return QVariant(self.arraydata[index.row()][index.column()])
if __name__ == "__main__":
main()

Categories