PyQt5 TableModel(QAbstractTableModel) shows no data - python

Hello i started with PyQt5 recently and tried to implement a csv parser:
But i struggled with the descendant of the QAbstractTableModel.
No data in the view!
The tutorial from qt5 (https://doc.qt.io/qt-5/qabstracttablemodel.html) says i have to implement some methods in the TableModel - class.
I think i did that but - no data in the view.
from PyQt5.QtWidgets import \
QApplication, QWidget, QVBoxLayout, QTableView
from PyQt5.QtCore import \
QAbstractTableModel, QVariant, Qt
class TableModel(QAbstractTableModel):
def __init__(self):
super().__init__()
self._data = []
def set(self, data):
self._data = data
def rowCount(self, index):
return len(self._data)
def columnCount(self, index):
return len(self._data[0])
def data(self, index, role=Qt.DisplayRole):
if not index.isValid():
return QVariant()
elif role == Qt.DisplayRole:
row = index.row()
return self._data[row]
else:
return QVariant()
def headerData(self, p_int, Qt_Orientation, role=None):
return self._data[0]
class View(QWidget):
def __init__(self):
super().__init__()
model = TableModel()
model.set(data)
self.tbl = QTableView(self)
self.tbl.setModel(model)
def setup(self):
self.setMinimumHeight(600)
self.setMinimumWidth(800)
self.vlayout = QVBoxLayout(self)
self.vlayout.addWidget(self.tbl)
def main():
import sys
app = QApplication(sys.argv)
window = View()
window.setup()
window.show()
sys.exit(app.exec_())
main()
Some Mock data from here:
data = [f.strip().split(",") for f in """\
id,first_name,last_name,email,gender,ip_address
1,Hillard,Tasseler,htasseler0#google.com.br,Male,104.153.237.243
2,Tyrus,Oley,toley1#ft.com,Male,163.73.24.45
3,Kora,Covil,kcovil2#privacy.gov.au,Female,158.44.166.87
4,Phineas,McEntee,pmcentee3#rambler.ru,Male,71.82.246.45
5,Dottie,Spraging,dspraging4#berkeley.edu,Female,226.138.241.22
6,Andria,Ivatts,aivatts5#about.com,Female,57.5.76.78
7,Missy,Featherstone,mfeatherstone6#unblog.fr,Female,9.56.215.203
8,Anstice,Sargant,asargant7#about.me,Female,36.115.185.109
9,Teresita,Trounce,ttrounce8#myspace.com,Female,240.228.133.166
10,Sib,Thomke,sthomke9#ibm.com,Female,129.191.2.7
11,Amery,Dallander,adallandera#elpais.com,Male,4.115.194.100
12,Rourke,Rowswell,rrowswellb#bloomberg.com,Male,48.111.190.66
13,Cloe,Benns,cbennsc#slideshare.net,Female,142.48.24.44
14,Enos,Fery,eferyd#pen.io,Male,59.19.200.235
15,Russell,Capelen,rcapelene#fc2.com,Male,38.205.20.141""".split()]

The data method must return a value as a string, not a list, in your case self._data[row] is a list so the solution is to use the column to obtain a single element from that list:
def data(self, index, role=Qt.DisplayRole):
if not index.isValid():
return QVariant()
elif role == Qt.DisplayRole:
row = index.row()
col = index.column()
return self._data[row][col]
else:
return QVariant()

Related

How can you set header labels for QTableView columns?

I’m working on a Python GUI application with PyQt5 which has a QTableView for showing data.
Here is the code:
import sys
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import Qt
class DataModel(QtCore.QAbstractTableModel):
def __init__(self):
super().__init__()
self.data = []
def data(self, index, role):
if role == Qt.DisplayRole:
return self.data[index.row()][index.column()]
def rowCount(self, index):
return len(self.data)
def columnCount(self, index):
return len(self.data[0])
class MainWindow(UI.UserInterface):
def __init__(self):
super().__init__()
self.model = DataModel()
self.load()
self.TableView.setModel(self.model)
self.TableView.resizeColumnsToContents()
self.TableView.horizontalHeader().setStretchLastSection(True)
def load(self):
try:
self.model.data = [(1, '2020-01-10 00:00:00', 'KANIA', 'HENRYK', 4219)]
except Exception:
pass
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
The class UI.UserInterface is in separate module. It has the QWidgets of the interface and layout QWidgets. One of them is QTableView.
I can't seem to find a way to set the header labels for the QTableView.
I looked for different solutions (some of them below) but none of them worked:
https://doc.qt.io/qt-5/sql-presenting.html (this one is written in C++. I don't quite understand it)
You must implement headerData():
class DataModel(QtCore.QAbstractTableModel):
# ...
def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole):
if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
return 'Column {}'.format(section + 1)
return super().headerData(section, orientation, role)
Obviously you can set your own labels even with a simple list containing the labels you want to show.
Note that you should be very careful with naming new attributes to subclasses as they might already exist.
Most importantly, you should not overwrite self.data.
here is an exemple using a QtableView and a set headerdata and you willbe able to modifier the data from the tableview
def exemple_table(self):
database = QSqlDatabase("QPSQL")
database.setHostName("localhost")
database.setDatabaseName("database")
database.setUserName("postgres")
database.setPassword("password")
database.open()
model_ft = QSqlTableModel(db=database)
model_ft.setTable('table')
model_ft.setHeaderData(0, Qt.Horizontal,"id")
model_ft.setHeaderData(1, Qt.Horizontal,"exemple01")
model_ft.setHeaderData(2, Qt.Horizontal,"exemple02")
model_ft.setHeaderData(3, Qt.Horizontal,"exemple03")
model_ft.setHeaderData(4, Qt.Horizontal,"exemple04")
model_ft.setHeaderData(5, Qt.Horizontal,"exemple05")
model_ft.setHeaderData(6, Qt.Horizontal,"exemple06")
model_ft.setHeaderData(7, Qt.Horizontal,"exemple07")
model_ft.removeColumns(8,1)
date = str(datetime.date.today())
self.tableView.setModel(model_ft)
self.tableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
model_ft.setSort(0, Qt.DescendingOrder)
model_ft.select()
filter_ft = "date_d ='%s' " % (date_1)
model_ft.setFilter(filter_ft)
ps im using postgresql you can find other drivers here https://doc.qt.io/qt-5/sql-driver.html
and the filtre you can use all the SQL function
Override headerData method of QTableAbstractModel to set Columns and Rows name
def headerData(self, section: int, orientation: PySide6.QtCore.Qt.Orientation, role: int = ...):
#for setting columns name
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return f"Column {section + 1}"
#for setting rows name
if orientation == Qt.Vertical and role == Qt.DisplayRole:
return f"Row {section + 1}"
Working code:
import sys
import requests
import PySide6
from PySide6.QtWidgets import QTableView, QWidget, QApplication, QGridLayout, QHeaderView
from PySide6.QtCore import Qt, QAbstractTableModel
from PySide6.QtGui import QColor, QIcon, QPixmap
from datetime import datetime
class MagicIcon():
def __init__(self, link):
self.link = link
self.icon = QIcon()
try:
response = requests.get(self.link)
pixmap = QPixmap()
pixmap.loadFromData(response.content)
self.icon = QIcon(pixmap)
except:
pass
class TableModel(QAbstractTableModel):
def headerData(self, section: int, orientation: PySide6.QtCore.Qt.Orientation, role: int = ...):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
# return f"Column {section + 1}"
return self.columns[section]
if orientation == Qt.Vertical and role == Qt.DisplayRole:
return f"{section + 1}"
def __init__(self, _data):
self.columns = ["Account", "Investment", "KYC", "Investment Date"]
# super().__init__(self)
super(TableModel, self).__init__()
self._data = _data
self.calendarLink = "https://img.icons8.com/fluency/48/000000/windows-calendar.png"
self.dollarLink = "https://img.icons8.com/external-vitaliy-gorbachev-lineal-color-vitaly-gorbachev/40/000000/external-dollar-currency-vitaliy-gorbachev-lineal-color-vitaly-gorbachev-1.png"
self.analysis = "https://img.icons8.com/external-flatarticons-blue-flatarticons/65/000000/external-analysis-digital-marketing-flatarticons-blue-flatarticons-1.png"
self.bug = "https://img.icons8.com/offices/30/000000/bug.png"
self.account = "https://img.icons8.com/plumpy/24/000000/edit-administrator.png"
self.approvedLink = "https://img.icons8.com/external-bearicons-flat-bearicons/40/000000/external-approved-approved-and-rejected-bearicons-flat-bearicons-9.png"
self.rejectedLink = "https://img.icons8.com/external-bearicons-flat-bearicons/40/000000/external-rejected-approved-and-rejected-bearicons-flat-bearicons-11.png"
self.naLink = "https://img.icons8.com/color/48/000000/not-applicable.png"
self.calendarIcon = MagicIcon(self.calendarLink).icon
self.accountIcon = MagicIcon(self.account).icon
self.dollarIcon = MagicIcon(self.dollarLink).icon
self.approvedIcon = MagicIcon(self.approvedLink).icon
self.rejectedIcon = MagicIcon(self.rejectedLink).icon
self.naIcon = MagicIcon(self.naLink).icon
def data(self, index, role):
if role == Qt.DisplayRole:
value = self._data[index.row()][index.column()]
if isinstance(value, datetime):
return value.strftime("%Y-%m-%d")
if isinstance(value, float):
return f"{value:.2f}"
return value
if role == Qt.TextAlignmentRole:
return Qt.AlignHCenter + Qt.AlignVCenter
if role == Qt.BackgroundRole:
return QColor("#adcdff") if index.row() % 2 == 0 else QColor("#d8ffc2")
if role == Qt.DecorationRole:
value = self._data[index.row()][index.column()]
if value is None:
return self.naIcon
if isinstance(value, datetime):
return self.calendarIcon
if index.column() == 0:
return self.accountIcon
if index.column() == 1:
return self.dollarIcon
if index.column() == 2:
if value == True:
return self.approvedIcon
elif value == False:
return self.rejectedIcon
return self.naIcon
def rowCount(self, index):
return len(self._data)
def columnCount(self, index):
return len(self._data[0])
class MainWindow(QWidget):
def __init__(self):
# super().__init__()
super(MainWindow, self).__init__()
self.resizeEvent = self.onResize
self.table = QTableView()
self.setWindowIcon(MagicIcon(
"https://img.icons8.com/external-flatarticons-blue-flatarticons/65/000000/external-analysis-digital-marketing-flatarticons-blue-flatarticons-1.png"
).icon)
self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
# self.table.setSizeAdjustPolicy(QHeaderView.AdjustIgnored)
# self.table.verticalHeader().setSectionResizeMode(QHeaderView.Stretch)
data = [
["Andrew Mike", 15.255, True, datetime(2022, 1, 5)],
["Eliza Petterson", 353.555, False, datetime(2020, 1, 5)],
["Joseph Samuel", 123, None, datetime(2020, 1, 15)],
["Nita Singh", 266, True, datetime(2022, 2, 7)],
["Rahul Chakrabarti", 102, True, datetime(2019, 10, 15)],
]
self.model = TableModel(data)
self.table.setModel(self.model)
self.header = self.table.horizontalHeader()
# self.header.setSectionResizeMode(0, QHeaderView.Stretch)
# self.header.setSectionResizeMode(1, QHeaderView.)
# self.header.setSectionResizeMode(2, QHeaderView.ResizeToContents)
self.layout = QGridLayout()
self.layout.addWidget(self.table, 0, 0)
self.setLayout(self.layout)
def onResize(self, event):
# print('old', event.oldSize(), 'new', event.size())
# super(MainWindow, self).resizeEvent(event)
pass
if __name__ == "__main__":
app = QApplication(sys.argv)
wid = MainWindow()
wid.show()
sys.exit(app.exec())

coloring int and float cells based on rules in custom QTableView model

I'm trying to color int and float numbers in QTableView based on some rule.
I have following custom table model which works ok for pandas dataframe data source:
from PyQt5 import QtCore, QtGui
class PandasTableModel(QtGui.QStandardItemModel):
def __init__(self, data, parent=None):
QtGui.QStandardItemModel.__init__(self, parent)
self._data = data
for row in data.values.tolist():
data_row = [ QtGui.QStandardItem("{}".format(x)) for x in row ]
self.appendRow(data_row)
return
def rowCount(self, parent=None):
return len(self._data.values)
def columnCount(self, parent=None):
return self._data.columns.size
def headerData(self, x, orientation, role):
if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
return self._data.columns[x]
if orientation == QtCore.Qt.Vertical and role == QtCore.Qt.DisplayRole:
return self._data.index[x]
return None
but when I add function for coloring:
def data(self, index, role):
if role == Qt.ForegroundRole:
value = self._data.iloc[index.row()][index.column()]
if (
(isinstance(value, int) or isinstance(value, float))
and value < 0
):
return QtGui.QColor('red')
inside my PandasTableModel class, program still loads without errors only all cells are white and seem empty but filters on headers are still showing all unique values which means that data is loaded.
EDIT
Here is reproducible copy-paste example.
from PyQt5 import QtGui, QtWidgets
from PyQt5.QtCore import Qt
import sys
import pandas as pd
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, obj=None, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.centralwidget = QtWidgets.QWidget(self)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.Fixed)
self.centralwidget.setSizePolicy(sizePolicy)
self.pdtable = QtWidgets.QTableView(self.centralwidget)
dataPD = [['tom', 10, 180.3], ['nick', 15, 175.7], ['juli', 14, 160.6]]
df = pd.DataFrame(dataPD, columns=['Name', 'Age', 'Height'])
self.model = PandasTableModel(df)
self.pdtable.setModel(self.model)
self.setCentralWidget(self.centralwidget)
class PandasTableModel(QtGui.QStandardItemModel):
def __init__(self, data, parent=None):
QtGui.QStandardItemModel.__init__(self, parent)
self._data = data
for row in data.values.tolist():
data_row = [ QtGui.QStandardItem("{}".format(x)) for x in row ]
self.appendRow(data_row)
return
def rowCount(self, parent=None):
return len(self._data.values)
def columnCount(self, parent=None):
return self._data.columns.size
def headerData(self, x, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self._data.columns[x]
if orientation == Qt.Vertical and role == Qt.DisplayRole:
return self._data.index[x]
return None
def data(self, index, role):
if role == Qt.ForegroundRole:
value = self._data.iloc[index.row()][index.column()]
if isinstance(value, (int, float)) and value < 0:
return QtGui.QColor("red")
else:
QtGui.QStandardItemModel.data(self, index, role)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
app.setStyle("Fusion")
main = MainWindow()
main.show()
main.resize(600, 400)
sys.exit(app.exec_())
If you comment out whole def data(self, index, role) function you can see original dataframe in Gui App.
Here is working solution:
from PyQt5 import QtGui, QtWidgets
from PyQt5.QtCore import Qt
import sys
import pandas as pd
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, obj=None, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.centralwidget = QtWidgets.QWidget(self)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.Fixed)
self.centralwidget.setSizePolicy(sizePolicy)
self.pdtable = QtWidgets.QTableView(self.centralwidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.Fixed)
self.pdtable.setSizePolicy(sizePolicy)
dataPD = [['tom', 10.0, 180.3], ['nick', 15.0, 175.7], ['juli', 14.0, 160.6]]
df = pd.DataFrame(dataPD, columns=['Name', 'Age', 'Height'])
print(df.dtypes)
self.model = PandasTableModel(df)
self.pdtable.setModel(self.model)
self.setCentralWidget(self.centralwidget)
class PandasTableModel(QtGui.QStandardItemModel):
def __init__(self, data, parent=None):
QtGui.QStandardItemModel.__init__(self, parent)
self._data = data
for row in data.values.tolist():
data_row = [ QtGui.QStandardItem("{}".format(x)) for x in row ]
self.appendRow(data_row)
return
def rowCount(self, parent=None):
return len(self._data.values)
def columnCount(self, parent=None):
return self._data.columns.size
def headerData(self, x, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self._data.columns[x]
if orientation == Qt.Vertical and role == Qt.DisplayRole:
return self._data.index[x]
return None
def data(self, index, role):
if role == Qt.ForegroundRole:
value = self._data.iloc[index.row()][index.column()]
if isinstance(value, (int, float)) and value > 0:
return QtGui.QColor("red")
elif role == Qt.DisplayRole:
return str(self._data.iloc[index.row(), index.column()])
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
app.setStyle("Fusion")
main = MainWindow()
main.show()
main.resize(600, 400)
sys.exit(app.exec_())

How could I highlight the cell with the highest value in each row in a PyQT4 Table View

I am very new to PyQT and stumbled upon the following obstacle. I have created a pandas DataFrame and managed to visualize it in a pyqt4 Table View with some code I found in another thread (Fastest way to populate QTableView from Pandas data frame). Now I want to highlight the highest value in each row.
Currently the code that I'm using for loading a pandas DataFrame into the Table View looks like this:
class PandasModel(QtCore.QAbstractTableModel):
"""
Class to populate a table view with a pandas dataframe
"""
def __init__(self, data, parent=None):
QtCore.QAbstractTableModel.__init__(self, parent)
self._data = data
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):
if index.isValid():
if role == QtCore.Qt.DisplayRole:
return str(self._data.values[index.row()][index.column()])
return None
def headerData(self, col, orientation, role):
if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
return self._data.columns[col]
return None
I have no idea whether what I want to achieve is possible using this method, since I don't think I understand the PyQT structure well enough, so any solution is welcome.
The desired result I have in mind would finally look like this for example
Thank you in advance!
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QTreeWidgetItem
import sys
import pandas as pd
import numpy as np
class PandasModel(QtCore.QAbstractTableModel):
"""
Class to populate a table view with a pandas dataframe
"""
def __init__(self, data, parent=None):
QtCore.QAbstractTableModel.__init__(self, parent)
self._data = data
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):
if index.isValid():
if role == QtCore.Qt.DisplayRole:
return str(self._data.values[index.row()][index.column()])
if role == QtCore.Qt.BackgroundColorRole:
row = index.row()
col = index.column()
if self._data.iloc[row,col] == self._data.iloc[row].max():
color = QtGui.QColor('red')
else:
color = QtGui.QColor('white')
pixmap = QtGui.QPixmap(26, 26)
pixmap.fill(color)
return color
return None
def headerData(self, col, orientation, role):
if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
return self._data.columns[col]
return None
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
df = pd.DataFrame(np.random.randn(8,3).round(3))
view = QtWidgets.QTableView()
view.setModel(PandasModel(df))
view.show()
sys.exit(app.exec_())

How to extend Qt Widgets that do not have model support by default

CustomMenu class inherits from QMenu. Its custom setData() method accepts an argument and sets it to a class variable model. I did this because QToolButton, QToolMenu and QAction do not support model/view framework. From what I know aside from all QList-QTable-QTree Views and Trees only the QComboBox does support model.
So the question: would it be possible to extend the functionality of other non-model widgets so they could be used as driven-by-model widgets too?
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys, os
class CustomMenu(QMenu):
def __init__(self):
super(CustomMenu, self).__init__()
self.model=None
def setModel(self, model):
self.model=model
self.pupulate()
def pupulate(self):
for item in self.model.items:
self.addAction(item)
class Model(QAbstractTableModel):
def __init__(self, parent=None, *args):
QAbstractTableModel.__init__(self, parent, *args)
self.items = ['Row0_Column0','Row1_Column0','Row2_Column0']
def flags(self, index):
return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable
def rowCount(self, QModelIndex):
return len(self.items)
def columnCount(self, QModelIndex):
return 1
def data(self, index, role):
if not index.isValid(): return QVariant()
if role == Qt.DisplayRole:
return QVariant(self.items[index.row()])
return QVariant()
def setData(self, index, value, role=Qt.EditRole):
if index.isValid():
if role == Qt.EditRole:
self.items[index.row()]=value
return True
return False
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
layout=QVBoxLayout(self)
self.setLayout(layout)
tablemodel=Model(self)
tableView=QTableView()
tableView.horizontalHeader().setStretchLastSection(True)
tableView.setModel(tablemodel)
layout.addWidget(tableView)
combo=QComboBox()
combo.setModel(tablemodel)
layout.addWidget(combo)
toolButton=QToolButton(self)
toolButton.setText('Tool Button')
toolButton.setPopupMode(QToolButton.InstantPopup)
toolButton.setSizePolicy(QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed))
menu=CustomMenu()
menu.setModel(tablemodel)
toolButton.setMenu(menu)
layout.addWidget(toolButton)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
EDITED LATER: An attempt to implement a QDataWidgetMapper():
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys, os
class CustomMenuButton(QWidget):
def __init__(self, parent):
super(CustomMenuButton, self).__init__(parent)
self.dataMapper=QDataWidgetMapper()
layout=QVBoxLayout()
self.setLayout(layout)
toolButton=QToolButton(self)
toolButton.setText('Tool Button')
toolButton.setPopupMode(QToolButton.InstantPopup)
self.menu=QMenu(toolButton)
toolButton.setMenu(self.menu)
def setModel(self, model):
self.dataMapper.setModel(model)
for row in range(model.rowCount()):
action=self.menu.addAction('Item')
self.dataMapper.addMapping(action, row)
class Model(QAbstractTableModel):
def __init__(self, parent=None, *args):
QAbstractTableModel.__init__(self, parent, *args)
self.items = ['Row0_Column0','Row1_Column0','Row2_Column0']
def flags(self, index):
return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable
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()
if role == Qt.DisplayRole:
return QVariant(self.items[index.row()])
return QVariant()
def setData(self, index, value, role=Qt.EditRole):
if index.isValid():
if role == Qt.EditRole:
self.items[index.row()]=value
self.dataChanged.emit(index, index)
return True
return False
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
layout=QVBoxLayout(self)
self.setLayout(layout)
tablemodel=Model(self)
tableView=QTableView()
tableView.horizontalHeader().setStretchLastSection(True)
tableView.setModel(tablemodel)
layout.addWidget(tableView)
combo=QComboBox()
combo.setModel(tablemodel)
layout.addWidget(combo)
menuButton=CustomMenuButton(self)
layout.addWidget(menuButton)
menuButton.setModel(tablemodel)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
EDIT 2: Defined a class CustomAction(QAction) with dataChanged = pyqtSignal(QModelIndex,QModelIndex)...
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys, os
class CustomAction(QAction):
dataChanged = pyqtSignal(QModelIndex,QModelIndex)
def __init__(self, name, parent):
super(CustomAction, self).__init__(name, parent)
class CustomMenuButton(QWidget):
def __init__(self, parent):
super(CustomMenuButton, self).__init__(parent)
self.dataMapper=QDataWidgetMapper()
layout=QVBoxLayout()
self.setLayout(layout)
toolButton=QToolButton(self)
toolButton.setText('Tool Button')
toolButton.setPopupMode(QToolButton.InstantPopup)
self.menu=QMenu(toolButton)
toolButton.setMenu(self.menu)
def setModel(self, model):
self.dataMapper.setModel(model)
for row in range(model.rowCount()):
index=model.index(row,0)
itemName=model.data(index, Qt.DisplayRole).toPyObject()
actn=CustomAction(itemName, self.menu)
self.menu.addAction(actn)
self.dataMapper.addMapping(actn, row)
class Model(QAbstractTableModel):
def __init__(self, parent=None, *args):
QAbstractTableModel.__init__(self, parent, *args)
self.items = ['Row0_Column0','Row1_Column0','Row2_Column0']
def flags(self, index):
return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable
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()
if role == Qt.DisplayRole:
return QVariant(self.items[index.row()])
return QVariant()
def setData(self, index, value, role=Qt.EditRole):
if index.isValid():
if role == Qt.EditRole:
self.items[index.row()]=value
self.dataChanged.emit(index, index)
return True
return False
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
layout=QVBoxLayout(self)
self.setLayout(layout)
tablemodel=Model(self)
tableView=QTableView()
tableView.horizontalHeader().setStretchLastSection(True)
tableView.setModel(tablemodel)
layout.addWidget(tableView)
combo=QComboBox()
combo.setModel(tablemodel)
layout.addWidget(combo)
menuButton=CustomMenuButton(self)
layout.addWidget(menuButton)
menuButton.setModel(tablemodel)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
This is what the QDataWidgetMapper class was designed for. The following blog has a great tutorial covering exactly how to implement it: http://www.yasinuludag.com/blog/?p=98
EDIT: I should add that QDataWidgetMapper works with QWidget and its subclasses, so QAction would not apply. For classes that are not QWidget or its subclasses, you might have to implement your own custom model-view binding.

How to select QTableView row with one click

When one of the QTableView's QModelIndex is clicked I want to select an entire row of the same-row-indexes.
To accomplish this I connect QTableView's clicked signal to a custom viewClicked() method which receives the clicked QModelIndex automatically:
self.tableview=QTableView()
self.tableview.clicked.connect(self.viewClicked)
Inside of viewClicked(self, clickedIndex) I query clickedIndex's row number, its model and the total number of columns):
row=clickedIndex.row()
model=clickedIndex.model()
columnsTotal=model.columnCount(None)
Finally to select an every index in a row:
for i in range(columnsTotal): self.tableview.selectRow(row)
The problem is it is noticeably slow for Qt to process such an action.
I wonder if there is a faster way to select an entire row of indexes when one of the tableview items is clicked:
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 = ['Row0_Column0','Row0_Column1','Row0_Column2']
def flags(self, index):
return Qt.ItemIsEnabled | Qt.ItemIsSelectable
def rowCount(self, parent):
return 1
def columnCount(self, parent):
return len(self.items)
def data(self, index, role):
if not index.isValid(): return QVariant()
elif role != Qt.DisplayRole:
return QVariant()
column=index.column()
if column<len(self.items):
return QVariant(self.items[column])
else:
return QVariant()
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
tablemodel=Model(self)
self.tableview=QTableView()
self.tableview.setModel(tablemodel)
self.tableview.clicked.connect(self.viewClicked)
layout = QHBoxLayout(self)
layout.addWidget(self.tableview)
self.setLayout(layout)
def viewClicked(self, clickedIndex):
row=clickedIndex.row()
model=clickedIndex.model()
columnsTotal=model.columnCount(None)
for i in range(columnsTotal):
self.tableview.selectRow(row)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
EDITED LATER: REVISED WORKING CODE Thanks to Nejat! :
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 = ['Row0_Column0','Row0_Column1','Row0_Column2']
def flags(self, index):
return Qt.ItemIsEnabled | Qt.ItemIsSelectable
def rowCount(self, parent):
return 1
def columnCount(self, parent):
return len(self.items)
def data(self, index, role):
if not index.isValid(): return QVariant()
elif role != Qt.DisplayRole:
return QVariant()
column=index.column()
if column<len(self.items):
return QVariant(self.items[column])
else:
return QVariant()
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
tablemodel=Model(self)
self.tableview=QTableView()
self.tableview.setModel(tablemodel)
self.tableview.clicked.connect(self.viewClicked)
self.tableview.setSelectionBehavior(QTableView.SelectRows)
layout = QHBoxLayout(self)
layout.addWidget(self.tableview)
self.setLayout(layout)
def viewClicked(self, clickedIndex):
row=clickedIndex.row()
model=clickedIndex.model()
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
You can use setSelectionBehavior function of QTableView to set the selection behavior to QTableView.SelectRows :
self.tableview.setSelectionBehavior(QTableView.SelectRows);
Now when you click on an item, the entire row is selected.

Categories

Resources