How can I inherit QTableWidget and my class MainWindow? - python

First off I'm relatively new to python and very new to PyQt. I'm taking my first steps into object oriented programming and I have been looking at online tutorials, but I'm stuck on a multiple inheritance issue.
I have class called CreateTable() whose purpose is to make a QTableWidget in my app window and when a row is clicked it opens a context menu and you can then choose the option to graph the selected row. Where the graph option is connected to another class Plotter().
My problem is that CreateTable needs to inherit both the QTableWidget from PyQt and my class MainWindow because I need to reference the layout variable in order to embed my graph into my application window.
My code for attempted inheritance is as follows and heavily borrows from here:How does Python's super() work with multiple inheritance?
class QTable(QTableWidget):
def __init__(self):
super(QTable, self).__init__()
class PassMain(MainWindow):
def __init__(self):
super(PassMain, self).__init__()
class PassMainTable(PassMain, QTable):
def __init__(self):
super(PassMainTable, self).__init__()
The main problem is when I try to place the graph inside my MainWindow layout.
self.vboxRight.addWidget(self.Graph)
Here is my code for creating the table and calling plotter
class CreateTable(PassMainTable): #QTableWidget
def __init__(self, Data, row, col, colHeaders, rowHeaders): #Index, ColumnHeaders
super(CreateTable, self).__init__()
self.setSelectionBehavior(self.SelectRows)
print("Start initialization")
self.ColHeader = colHeaders
self.setRowCount(row)
self.setColumnCount(col)
self.data = Data
self.setHorizontalHeaderLabels(colHeaders)
print("Right before for loop")
n = len(Data)
m = len(colHeaders)
for i in range(n):
DataValues = self.data.iloc[i,:]
print("values are {}".format(DataValues))
#m = len(values)
ConvertedVals = pd.to_numeric(DataValues)
ValList = DataValues.values.tolist()
print(ValList)
for j in range(0,m):
self.item = QTableWidgetItem(str(round(ValList[j],5)))
#print("{}, {}".format(i, j))
self.setItem(i,j, self.item)
def contextMenuEvent(self, event):
menu = QMenu(self)
graphAction = menu.addAction("Graph")
compareAction = menu.addAction("Compare")
scatterAction = menu.addAction("Plot types")
aboutAction = menu.addAction("about")
quitAction = menu.addAction("quit")
printAction = menu.addAction("Print Row")
action = menu.exec_(self.mapToGlobal(event.pos()))
if action == quitAction:
qApp.quit()
elif action == printAction:
self.selected = self.selectedItems()
n = len(self.selected)
print("n is {}".format(n))
for i in range(n):
self.selected[i] = str(self.selected[i].text())
for i in range(n):
self.selected[i] = float(self.selected[i])
print(self.selected)
elif action == graphAction:
self.selected = self.selectedItems()
n = len(self.selected)
for i in range(n):
self.selected[i] = str(self.selected[i].text())
for i in range(n):
self.selected[i] = float(self.selected[i])
print("right before plotter called")
self.Graph = Plotter(self.selected, self.ColHeader)
self.vboxRight.addWidget(self.Graph)
else:
print("u clicked something other than quit")
To make matters worse PyQt is catching all my errors as exceptions so all I get for errors is "Process finished with exit code 1"
if you need further reference to my full code I have provided a link here: https://github.com/Silvuurleaf/Data-Analysis-and-Visualization-GUI/blob/master/Plotter3.1.py
Thank you I appreciate any help y'all can give me.

In order to share data it is not necessary to inherit from the widget, just use signals, this is the natural way in PyQt to share data asynchronously. In your case for example we will create a signal called dataSignal, according to what is observed of your code you want to use the variables self.selected, self.ColHeader, the first one is of type list, and the second numpy.ndarray so with that We will build the signal:
class CreateTable(QTableWidget): #QTableWidget
dataSignal = pyqtSignal(list, np.ndarray)
def __init__(self, Data, row, col, colHeaders, rowHeaders): #Index, ColumnHeaders
super(CreateTable, self).__init__()
self.setSelectionBehavior(self.SelectRows)
print("Start initialization")
self.ColHeader = colHeaders
self.setRowCount(row)
self.setColumnCount(col)
self.data = Data
self.setHorizontalHeaderLabels(colHeaders)
print("Right before for loop")
n = len(Data)
m = len(colHeaders)
for i in range(n):
DataValues = self.data.iloc[i,:]
print("values are {}".format(DataValues))
#m = len(values)
ConvertedVals = pd.to_numeric(DataValues)
ValList = DataValues.values.tolist()
print(ValList)
for j in range(0,m):
self.item = QTableWidgetItem(str(round(ValList[j],5)))
#print("{}, {}".format(i, j))
self.setItem(i,j, self.item)
def contextMenuEvent(self, event):
menu = QMenu(self)
graphAction = menu.addAction("Graph")
compareAction = menu.addAction("Compare")
scatterAction = menu.addAction("Plot types")
aboutAction = menu.addAction("about")
quitAction = menu.addAction("quit")
printAction = menu.addAction("Print Row")
action = menu.exec_(self.mapToGlobal(event.pos()))
if action == quitAction:
qApp.quit()
elif action == printAction:
self.selected = self.selectedItems()
n = len(self.selected)
print("n is {}".format(n))
for i in range(n):
self.selected[i] = str(self.selected[i].text())
for i in range(n):
self.selected[i] = float(self.selected[i])
print(self.selected)
elif action == graphAction:
self.selected = self.selectedItems()
n = len(self.selected)
for i in range(n):
self.selected[i] = str(self.selected[i].text())
for i in range(n):
self.selected[i] = float(self.selected[i])
print("right before plotter called")
print(type(self.selected), type(self.ColHeader))
self.dataSignal.emit(self.selected, self.ColHeader)
else:
print("u clicked something other than quit")
Then in the MainWindow class we create a slot, in this create the Plotter object and add it to the layout.
def dataPlotter(self, x_data,y_data):
self.Graph = Plotter(x_data, y_data)
self.vboxRightBottom.addWidget(self.Graph)
To do this we connect the signal when we create the CreateTable object:
self.Table = CreateTable(self.BaseStats, row, col, colHeaders, rowHeaders)
self.Table.dataSignal.connect(self.dataPlotter)
self.vboxRightBottom.addWidget(self.Table)
The complete code is here.

Related

Add multiple event filters for multiple widgets or add same event filter to multiple widgets?

the following code is an example of QTableWidget with contextMenu (right click and CTRl+C/V), containing a couple of actions such as copy/paste and add/delete rows...just for one QTableWidget.
I have two other QTableWidgets on the same Window next to the first QTableWidget and I want the same contextMenu for them too, how can I do that?
I appreciate any help with that.
class Calculation_window_Specific(QMainWindow):
def __init__(self):
super().__init__()
uic.loadUi("specific_calculation_window.ui",self)
self.setStyleSheet(stylesheet2)
self.setWindowTitle('specific calculation table')
self.setWindowIcon(QtGui.QIcon("logo.ico"))
self.results_table_Specific.installEventFilter(self)
def eventFilter(self,source,event):
if event.type()== QtCore.QEvent.KeyPress:
if event==QtGui.QKeySequence.Copy:
self.copySelection()
return True
elif event==QtGui.QKeySequence.Paste:
self.pasteSelection()
return True
elif event.type() == QtCore.QEvent.ContextMenu:
# a context menu for the copy/paste operations
menu = QtWidgets.QMenu()
removeRow=menu.addAction('Delete selected row')
removeRow.triggered.connect(self.removeRow)
removeColumn=menu.addAction('Delete selected column')
removeColumn.triggered.connect(self.removeColumn)
copyAction = menu.addAction('Copy')
copyAction.triggered.connect(self.copySelection)
pasteAction = menu.addAction('Paste')
pasteAction.triggered.connect(self.pasteSelection)
addRowSubMenu=menu.addMenu('Add Row')
addBefore=addRowSubMenu.addAction('add before')
addBefore.triggered.connect(self.addRowBefore)
addAfter=addRowSubMenu.addAction('add after')
addAfter.triggered.connect(self.addRowAfter)
self.LineEdit1=QLineEdit('Rows number before',self)
widget1=QWidgetAction(self)
widget1.setDefaultWidget(self.LineEdit1)
self.LineEdit2=QLineEdit('Rows number After',self)
widget2=QWidgetAction(self)
widget2.setDefaultWidget(self.LineEdit2)
addMultipleRowsMenu=menu.addMenu('Add Rows')
addMultipleRowsBefore=addMultipleRowsMenu.addAction(widget1)
self.LineEdit1.returnPressed.connect(self.addMultipleRowBefore)
addMultipleRowsAfter=addMultipleRowsMenu.addAction(widget2)
self.LineEdit2.returnPressed.connect(self.addMultipleRowAfter)
if not self.results_table_Specific.selectedIndexes():
# no selection available, both copy and paste are disabled
copyAction.setEnabled(False)
pasteAction.setEnabled(False)
menu.exec(event.globalPos())
return True
return super(Calculation_window_Specific, self).eventFilter(source, event)
def copySelection(self):
# clear the current contents of the clipboard
self.clipboard=''
copied_cells = sorted(self.results_table_Specific.selectedIndexes())
print(copied_cells)
max_column = copied_cells[-1].column()
for c in copied_cells:
self.clipboard += self.results_table_Specific.item(c.row(), c.column()).text()
if c.column() == max_column:
self.clipboard += '\n'
else:
self.clipboard += '\t'
QApplication.clipboard().setText(self.clipboard)
def pasteSelection(self):
selection = self.results_table_Specific.selectedIndexes()
if selection:
row_anchor = selection[0].row()
column_anchor = selection[0].column()
clipboard = QApplication.clipboard()
rows = clipboard.text().split('\n')
for indx_row, row in enumerate(rows):
values = row.split('\t')
for indx_col, value in enumerate(values):
item = QTableWidgetItem(value)
self.results_table_Specific.setItem(row_anchor + indx_row, column_anchor + indx_col, item)
def removeColumn(self):
selection=sorted(self.results_table_Specific.selectionModel().selectedColumns())
print(selection)
for idx in selection:
self.results_table_Specific.removeColumn(idx.column())
def removeRow(self):
selection=sorted(self.results_table_Specific.selectionModel().selectedRows())
print(selection)
for idx in selection:
self.results_table_Specific.removeRow(idx.row())
def addRowBefore(self):
selection=self.results_table_Specific.selectedIndexes()
rowPosition=selection[0].row()
#print(rowPosition)
self.results_table_Specific.insertRow(rowPosition)
def addRowAfter(self):
selection=self.results_table_Specific.selectedIndexes()
rowPosition=selection[0].row()
#print(rowPosition)
self.results_table_Specific.insertRow(rowPosition+1)
def addMultipleRowBefore(self):
selection=self.results_table_Specific.selectedIndexes()
rowPosition=selection[0].row()
#print(rowPosition)
for r in range(int(self.LineEdit1.text())):
self.results_table_Specific.insertRow(rowPosition)
def addMultipleRowAfter(self):
selection=self.results_table_Specific.selectedIndexes()
rowPosition=selection[0].row()
#print(rowPosition)
for r in range(int(self.LineEdit2.text())):
self.results_table_Specific.insertRow(rowPosition+1) ```

Making a specified table using pyqt5 designer

I want to make a specific table like shown in the picture below using pyqt designer and I coulnd't make a good result.
I want to make this table in a window and contains the same elements and same dimensions.
I tried to use layouts using LineEdits and Qlabels but I couldnt make it too .
Thank you.
Premise: your question didn't show lots of research efforts, and from what was said it's quite clear that you're still a bit inexperienced; this will probably make this answer very complicated, but that's because what you asked is not simple.
While achieving what it is asked is not impossible, it is not easy.
Also, you cannot do it directly in designer.
The main problem is that Qt's item views use QHeaderView, which uses a monodimensional structure; adding another "dimension" layer makes things much more difficult.
So, the first aspect you need to consider is that the table widget needs to have a new, custom QHeaderView set for the horizontal header, so you'll obviously need to subclass QHeaderView; but in order to make things work you'll also need to subclass QTableWidget too.
Due to the "monodimensionality" of the header (which only uses a single coordinate for its data), you need to "flatten" the structure and create an abstraction layer in order to access it.
In order to achieve that, I created a Structure class, with functions that allow access to it as some sort of tree-model:
class Section(object):
def __init__(self, label='', children=None, isRoot=False):
self.label = label
self._children = []
if children:
self._children = []
for child in children:
child.parent = self
self._children.append(child)
self._isRoot = isRoot
self.parent = None
def children(self):
return self._children
def isRoot(self):
return self._isRoot
def iterate(self):
# an iterator that cycles through *all* items recursively
if not self._isRoot:
yield self
items = []
for child in self._children:
items.extend([i for i in child.iterate()])
for item in items:
yield item
def sectionForColumn(self, column):
# get the first (child) item for the given column
if not self._isRoot:
return self.root().sectionForColumn(column)
for child in self.iterate():
if not child._children:
if child.column() == column:
return child
def root(self):
if self._isRoot:
return self
return self.parent.root()
def level(self):
# while levels should start from -1 (root), we're using levels starting
# from 0 (which is root); this is done for simplicity and performance
if self._isRoot:
return 0
parent = self.parent
level = 0
while parent:
level += 1
parent = parent.parent
return level
def column(self):
# root column should be -1; see comment on level()
if self._isRoot:
return 0
parentColIndex = self.parent._children.index(self)
column = self.parent.column()
for c in self.parent._children[:parentColIndex]:
column += c.columnCount()
return column
def columnCount(self):
# return the column (child) count for this section
if not self._children:
return 1
columns = 0
for child in self._children:
columns += child.columnCount()
return columns
def subLevels(self):
if not self._children:
return 0
levels = 0
for child in self._children:
levels = max(levels, child.subLevels())
return 1 + levels
class Structure(Section):
# a "root" class created just for commodity
def __init__(self, label='', children=None):
super().__init__(label, children, isRoot=True)
With this class, you can create your own header structure like this:
structure = Structure('Root item', (
Section('First parent, two sub levels', (
Section('First child, no children'),
Section('Second child, two children', (
Section('First subchild'),
Section('Second subchild')
)
)
)),
# column index = 3
Section('Second parent', (
Section('First child'),
Section('Second child')
)),
# column index = 5
Section('Third parent, no children'),
# ...
))
And here are the QHeaderView and QTableWidget subclasses, with a minimal reproducible code:
class AdvancedHeader(QtWidgets.QHeaderView):
_resizing = False
_resizeToColumnLock = False
def __init__(self, view, structure=None):
super().__init__(QtCore.Qt.Horizontal, view)
self.structure = structure or Structure()
self.sectionResized.connect(self.updateSections)
self.sectionHandleDoubleClicked.connect(self.emitHandleDoubleClicked)
def setStructure(self, structure):
if structure == self.structure:
return
self.structure = structure
self.updateGeometries()
def updateSections(self, index=0):
# ensure that the parent section is always updated
if not self.structure.children():
return
section = self.structure.sectionForColumn(index)
while not section.parent.isRoot():
section = section.parent
leftColumn = section.column()
left = self.sectionPosition(leftColumn)
width = sum(self.sectionSize(leftColumn + c) for c in range(section.columnCount()))
self.viewport().update(left - self.offset(), 0, width, self.height())
def sectionRect(self, section):
if not self.structure.children():
return
column = section.column()
left = 0
for c in range(column):
left += self.sectionSize(c)
bottom = self.height()
rowHeight = bottom / self.structure.subLevels()
if section.parent.isRoot():
top = 0
else:
top = (section.level() - 1) * rowHeight
width = sum(self.sectionSize(column + c) for c in range(section.columnCount()))
if section.children():
height = rowHeight
else:
root = section.root()
rowCount = root.subLevels()
parent = section.parent
while parent.parent:
rowCount -= 1
parent = parent.parent
height = rowHeight * rowCount
return QtCore.QRect(left, top, width, height)
def paintSubSection(self, painter, section, level, rowHeight):
sectionRect = self.sectionRect(section).adjusted(0, 0, -1, -1)
painter.drawRect(sectionRect)
painter.save()
font = painter.font()
selection = self.selectionModel()
column = section.column()
sectionColumns = set([column + c for c in range(section.columnCount())])
selectedColumns = set([i.column() for i in selection.selectedColumns()])
if ((section.children() and selectedColumns & sectionColumns == sectionColumns) or
(not section.children() and column in selectedColumns)):
font.setBold(True)
painter.setFont(font)
painter.drawText(sectionRect, QtCore.Qt.AlignCenter, section.label)
painter.restore()
for child in section.children():
self.paintSubSection(painter, child, child.level(), rowHeight)
def sectionHandleAt(self, pos):
x = pos.x() + self.offset()
visual = self.visualIndexAt(x)
if visual < 0:
return visual
for section in self.structure.iterate():
rect = self.sectionRect(section)
if pos in rect:
break
else:
return -1
grip = self.style().pixelMetric(QtWidgets.QStyle.PM_HeaderGripMargin, None, self)
if x < rect.x() + grip:
return section.column() - 1
elif x > rect.x() + rect.width() - grip:
return section.column() + section.columnCount() - 1
return -1
logical = self.logicalIndex(visual)
position = self.sectionViewportPosition(logical)
atLeft = x < (position + grip)
atRight = x > (position + self.sectionSize(logical) - grip)
if self.orientation() == QtCore.Qt.Horizontal and self.isRightToLeft():
atLeft, atRight = atRight, atLeft
if atLeft:
while visual >= 0:
visual -= 1
logical = self.logicalIndex(visual)
if not self.isSectionHidden(logical):
break
else:
logical = -1
elif not atRight:
logical = -1
return logical
def emitHandleDoubleClicked(self, index):
if self._resizeToColumnLock:
# avoid recursion
return
pos = self.viewport().mapFromGlobal(QtGui.QCursor.pos())
handle = self.sectionHandleAt(pos)
if handle != index:
return
self._resizeToColumnLock = True
for section in self.structure.iterate():
if index in range(section.column(), section.column() + section.columnCount()):
rect = self.sectionRect(section)
if rect.y() <= pos.y() <= rect.y() + rect.height():
sectCol = section.column()
for col in range(sectCol, sectCol + section.columnCount()):
if col == index:
continue
self.sectionHandleDoubleClicked.emit(col)
break
self._resizeToColumnLock = False
# -------- base class reimplementations -------- #
def sizeHint(self):
hint = super().sizeHint()
hint.setHeight(hint.height() * self.structure.subLevels())
return hint
def mousePressEvent(self, event):
super().mousePressEvent(event)
if event.button() != QtCore.Qt.LeftButton:
return
handle = self.sectionHandleAt(event.pos())
if handle >= 0:
self._resizing = True
else:
# if the clicked section has children, select all of its columns
cols = []
for section in self.structure.iterate():
sectionRect = self.sectionRect(section)
if event.pos() in sectionRect:
firstColumn = section.column()
columnCount = section.columnCount()
for column in range(firstColumn, firstColumn + columnCount):
cols.append(column)
break
self.sectionPressed.emit(cols[0])
for col in cols[1:]:
self.sectionEntered.emit(col)
def mouseMoveEvent(self, event):
super().mouseMoveEvent(event)
handle = self.sectionHandleAt(event.pos())
if not event.buttons():
if handle < 0:
self.unsetCursor()
elif handle < 0 and not self._resizing:
# update sections when click/dragging (required if highlight is enabled)
pos = event.pos()
pos.setX(pos.x() + self.offset())
for section in self.structure.iterate():
if pos in self.sectionRect(section):
self.updateSections(section.column())
break
# unset the cursor, in case it was set for a section handle
self.unsetCursor()
def mouseReleaseEvent(self, event):
self._resizing = False
super().mouseReleaseEvent(event)
def paintEvent(self, event):
qp = QtGui.QPainter(self.viewport())
qp.setRenderHints(qp.Antialiasing)
qp.translate(.5, .5)
height = self.height()
rowHeight = height / self.structure.subLevels()
qp.translate(-self.horizontalOffset(), 0)
column = 0
for parent in self.structure.children():
self.paintSubSection(qp, parent, 0, rowHeight)
column += 1
class CustomHeaderTableWidget(QtWidgets.QTableWidget):
structure = None
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
customHeader = AdvancedHeader(self)
self.setHorizontalHeader(customHeader)
customHeader.setSectionsClickable(True)
customHeader.setHighlightSections(True)
self.cornerHeader = QtWidgets.QLabel(self)
self.cornerHeader.setAlignment(QtCore.Qt.AlignCenter)
self.cornerHeader.setStyleSheet('border: 1px solid black;')
self.cornerHeader.setAttribute(QtCore.Qt.WA_TransparentForMouseEvents)
self.verticalHeader().setMinimumWidth(
self.cornerHeader.minimumSizeHint().width() + self.fontMetrics().width(' '))
self._cornerButton = self.findChild(QtWidgets.QAbstractButton)
self.setStructure(kwargs.get('structure') or Section('ROOT', isRoot=True))
self.selectionModel().selectionChanged.connect(self.selectionModelSelChanged)
def setStructure(self, structure):
if structure == self.structure:
return
self.structure = structure
if not structure:
super().setColumnCount(0)
self.cornerHeader.setText('')
else:
super().setColumnCount(structure.columnCount())
self.cornerHeader.setText(structure.label)
self.horizontalHeader().setStructure(structure)
self.updateGeometries()
def selectionModelSelChanged(self):
# update the corner widget
selected = len(self.selectionModel().selectedIndexes())
count = self.model().rowCount() * self.model().columnCount()
font = self.cornerHeader.font()
font.setBold(selected == count)
self.cornerHeader.setFont(font)
def updateGeometries(self):
super().updateGeometries()
vHeader = self.verticalHeader()
if not vHeader.isVisible():
return
style = self.verticalHeader().style()
opt = QtWidgets.QStyleOptionHeader()
opt.initFrom(vHeader)
margin = style.pixelMetric(style.PM_HeaderMargin, opt, vHeader)
width = self.cornerHeader.minimumSizeHint().width() + margin * 2
vHeader.setMinimumWidth(width)
self.cornerHeader.setGeometry(self._cornerButton.geometry())
def setColumnCount(self, count):
# ignore column count, as we're using setStructure() instead
pass
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
structure = Structure('UNITE', (
Section('Hrs de marche', (
Section('Expl'),
Section('Indi', (
Section('Prev'),
Section('Accid')
))
)),
Section('Dem', (
Section('TST'),
Section('Epl')
)),
Section('Decle'),
Section('a'),
Section('Consom'),
Section('Huile'),
))
tableWidget = CustomHeaderTableWidget()
tableWidget.setStructure(structure)
tableWidget.setRowCount(2)
tableWidget.setVerticalHeaderLabels(
['Row {}'.format(r + 1) for r in range(tableWidget.rowCount())])
tableWidget.show()
sys.exit(app.exec())
Some considerations, since the above example is not perfect:
sections are not movable (if you try to set setSectionsMovable and try to drag a section, it would probably crash at some point);
while I tried to avoid resizing of a "parent" section (the resize cursor is not shown), it is still possible to resize a child section from the parent rectangle;
changing the horizontal structure of the model might give unexpected results (I only implemented basic operations);
Structure is a standard python object subclass, and it's completely unlinked from the QTableWidget;
considering the above, using functions like horizontalHeaderItem, setHorizontalHeaderItem or setHorizontalHeaderLabels might not work as expected;
Now, how to use it in designer? You need to use a promoted widget.
Add the QTableWidget, right click on it and select Promote to..., ensure that "QTableWidget" is selected in the "Base class name" combo, type "CustomHeaderTableWidget" in the "Promoted class name" field and then the file name that contains the subclass in the "Header file" field (note that it's treated like a python module name, so it has to be without the .py file extension); click "Add", click "Promote" and save it.
Consider that, from there, you must still provide the custom Structure, and if you added any row and column in Designer it must reflect the structure column count.
Finally, since the matter is interesting, I might return on it in the future and update the code, eventually.
In the meantime, I strongly suggest you to carefully study the code, explore all the reimplementations of QHeaderView (see what's below base class reimplementations comment) and what the original methods actually do by reading the QHeaderView documentation.

Visual position of item in QTableView

I am working with PyQt5/PySide2. I Have a QTableView with QSortFilterProxyModel and the data is handled by QStandardItemModel.
I am using QStandardItemModel.findItems() method to find some cells in the first table row. The result is a list of QStandardItems. Now I want to order these items by row in which they are displayed in GUI table i.e. the way that user sees them. Is there some way to archive this?. To translate proxy or model indices to to "view" indices.
I tought this could be done using QSortFilterProxyModel.mapFromSource() method but it appears the proxy indices do not have the desired order.
here is a minimal reproducible example written in PyQt5:
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from collections import deque
from random import randint
class Splash(QWidget):
def __init__(self):
super().__init__()
# create model
self.model = QStandardItemModel(self)
self.model.setHorizontalHeaderLabels(["column 1", "column 2"])
# create sort proxy
self.proxy = NumberSortModel()
self.proxy.setSourceModel(self.model)
# create view
self.table = CustomQTableView(self)
self.table.setGeometry(0, 0, 275, 575)
self.table.setModel(self.proxy)
self.table.setSortingEnabled(True)
# create buttons
button = QPushButton('Find cells containing 1', self)
button.move(300, 70)
button.clicked.connect(lambda: self.table.search_string("1"))
button1 = QPushButton('next', self)
button1.move(300, 100)
button1.clicked.connect(self.table._search_next)
button2 = QPushButton('previous', self)
button2.move(300, 130)
button2.clicked.connect(self.table._search_previous)
# fill model
for i in range(15):
self.model.appendRow([QStandardItem(str(i)),
QStandardItem(str(randint(1, 100)))])
self.show()
# takes care of the coloring of results
class _HighlightDelegate(QStyledItemDelegate):
def __init__(self, parent=None) -> None:
QStyledItemDelegate.__init__(self, parent)
self._parent = parent
def paint(self, painter: "QPainter", option: "QStyleOptionViewItem",
index: "QModelIndex"):
painter.save()
if len(self._parent.proxy_indices) > 0:
if index == self._parent.proxy_indices[0]:
painter.fillRect(option.rect, Qt.red)
elif index in self._parent.proxy_indices:
painter.fillRect(option.rect, option.palette.highlight())
else:
if (option.state & QStyle.State_Selected):
painter.fillRect(option.rect, option.palette.highlight())
elif (option.state & QStyle.State_None):
painter.fillRect(option.rect, option.palette.base())
painter.drawText(option.rect, Qt.AlignLeft, index.data(Qt.DisplayRole))
painter.restore()
class CustomQTableView(QTableView):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.real_indices = deque()
self.proxy_indices = deque()
self.horizontalHeader().sortIndicatorChanged.connect(self._re_sort)
self.setItemDelegate(_HighlightDelegate(self))
def _re_sort(self):
# pretty print indices
def ind_to_py(indices):
py_ind = list()
for i in indices:
py_ind.append((i.row(), i.column(), i.data(Qt.DisplayRole)))
return py_ind
print("real ", ind_to_py(self.real_indices))
print("proxy ", ind_to_py(self.proxy_indices))
real_ind, proxy_ind = zip(*sorted(zip(self.real_indices, self.proxy_indices),
key=lambda x: (x[1].row(),
x[1].column())))
self.real_indices = deque(real_ind)
self.proxy_indices = deque(proxy_ind)
print("sorted real ", ind_to_py(self.real_indices))
print("sorted proxy", ind_to_py(self.proxy_indices))
print("---------------------------------------------------")
self.re_draw()
#property
def _model(self):
return self.model().sourceModel()
def re_draw(self):
self.viewport().update()
# we are always searching only in first column
def search_string(self, string: str):
indices = self._model.findItems(string, Qt.MatchContains, 0)
# get QModelIndex from found data
self.real_indices = deque([i.index() for i in indices])
self.proxy_indices = [QPersistentModelIndex(self.model().mapFromSource(i))
for i in self.real_indices]
# sort indeces according to their row and column
self._re_sort()
# update the view to highlight data
self.re_draw()
def _search_next(self):
self.real_indices.rotate(-1)
self.proxy_indices.rotate(-1)
self.re_draw()
def _search_previous(self):
self.real_indices.rotate(1)
self.proxy_indices.rotate(1)
self.re_draw()
# custom implementation to sort according to numbers not strings
class NumberSortModel(QSortFilterProxyModel):
def lessThan(self, left_index: "QModelIndex",
right_index: "QModelIndex") -> bool:
left_var: str = left_index.data(Qt.EditRole)
right_var: str = right_index.data(Qt.EditRole)
try:
return float(left_var) < float(right_var)
except (ValueError, TypeError):
pass
try:
return left_var < right_var
except TypeError: # in case of NoneType
return True
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
ex = Splash()
sys.exit(app.exec_())
In short when I run the search and click next the red marked cell moves down. It moves up when previous is clicked. But when I apply sorting by clicking table header it is messes up next/previous funstions. I want the red cell to always go down irrespective of applied sorting when next is clicked same with previous.
The logic of the painting should not be done directly but through the roles that must be used by the delegate to do the painting.
On the other hand, the following item must be selected based on the visual position of the item, so it must be mapped using the proxy.
Considering the above, the solution is:
CurrentRole = Qt.UserRole + 1000
SelectedRole = Qt.UserRole + 1001
# takes care of the coloring of results
class _HighlightDelegate(QStyledItemDelegate):
def initStyleOption(self, option: "QStyleOptionViewItem", index: "QModelIndex"):
super().initStyleOption(option, index)
is_current = index.data(CurrentRole) or False
is_selected = index.data(SelectedRole) or False
if is_current:
option.backgroundBrush = QColor(Qt.red)
option.palette.setColor(QPalette.Normal, QPalette.Highlight, QColor(Qt.red))
elif is_selected:
option.backgroundBrush = option.palette.highlight()
class CustomQTableView(QTableView):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.selected_indexes = []
self.current_index = None
self.setItemDelegate(_HighlightDelegate(self))
#property
def _model(self):
return self.model().sourceModel()
def search_string(self, string: str):
# restore
for index in self.selected_indexes:
self._model.setData(QModelIndex(index), False, SelectedRole)
if self.current_index is not None:
self._model.setData(QModelIndex(self.current_index), False, CurrentRole)
self.current_index = None
column = 0
match_indexes = self._model.match(
self._model.index(0, column), Qt.DisplayRole, string, -1, Qt.MatchContains
)
self.selected_indexes = [
QPersistentModelIndex(index) for index in match_indexes
]
self._sort_indexes_by_view()
if self.selected_indexes:
self.current_index = self.selected_indexes[0]
for index in self.selected_indexes:
self._model.setData(QModelIndex(index), True, SelectedRole)
if self.current_index is not None:
self._model.setData(QModelIndex(self.current_index), True, CurrentRole)
def _search_next(self):
if self.current_index is not None:
self._model.setData(QModelIndex(self.current_index), False, CurrentRole)
self._sort_indexes_by_view()
pos = self.selected_indexes.index(self.current_index)
next_pos = (pos + 1) % len(self.selected_indexes)
self.current_index = self.selected_indexes[next_pos]
self._model.setData(QModelIndex(self.current_index), True, CurrentRole)
def _search_previous(self):
if self.current_index is not None:
self._model.setData(QModelIndex(self.current_index), False, CurrentRole)
self._sort_indexes_by_view()
pos = self.selected_indexes.index(self.current_index)
next_pos = (pos - 1) % len(self.selected_indexes)
self.current_index = self.selected_indexes[next_pos]
self._model.setData(QModelIndex(self.current_index), True, CurrentRole)
def _sort_indexes_by_view(self):
self.selected_indexes.sort(
key=lambda index: self.model().mapFromSource(QModelIndex(index)).row()
)

Sorting qtablewidget by qcombobox currentText

I have a qtablewidget and one of its cell widgets in each row is a qcombobox. I would like to sort the table according to the contents of the qcomboboxes in each row. How to do that? Any help is greatly appreciated.
self.table = QTableWidget(0,5)
self.table.setSortingEnabled(True)
self.table.horizontalHeader().sortIndicatorChanged.connect(self.table_sorting_clicked)
'''some code here'''
def table_sorting_clicked(self,logicalIndex,order):
'''some code here'''
install a QSignalMapper
connect it to the sorting method
connect currentTextChanged()-signal of the comboboxes with the signalmapper
and set the itemData of the cells with the comboboxes to currentText() (necessary, because there is nothing to sort, if all cells contain only comboboxes)
update itemData if currentText() changes.
here a working example in pyqt5:
class MyTableWidget(QtWidgets.QTableWidget):
def __init__(self, parent = None):
QtWidgets.QTableWidget.__init__(self, parent)
self.setRowCount(5)
self.setColumnCount(2)
self.signalMapper = QtCore.QSignalMapper(self)
self.signalMapper.mapped.connect(self.table_sorting_clicked)
self.horizontalHeader().sortIndicatorChanged.connect(self.changeSortOrder)
texts = []
for i in ['A','B','C','D','E','F','G','H','I','J']:
texts.append(i)
for i in range(0,self.rowCount()):
combo = QtWidgets.QComboBox()
combo.addItems(texts)
combo.setCurrentIndex(i)
combo.currentTextChanged.connect(self.signalMapper.map)
self.signalMapper.setMapping(combo, i)
self.setItem(i,0,QtWidgets.QTableWidgetItem(str(i+1)))
self.setItem(i,1,QtWidgets.QTableWidgetItem())
self.setCellWidget(i,1,combo)
self.item(i,1).setData(0, combo.currentText())
self.setSortingEnabled(True)
self.sortOrder = 0
def table_sorting_clicked(self, index):
self.item(index,1).setData(0, self.cellWidget(index,1).currentText())
self.sortByColumn(1,self.sortOrder)
# edit 04.09.2015 19:40
#this doesn't work correctly if called several times
# add the following code:
for i in range(self.rowCount()):
self.signalMapper.removeMappings(self.cellWidget(i,1))
self.cellWidget(i,1).currentTextChanged.connect(self.signalMapper.map)
self.signalMapper.setMapping(self.cellWidget(i,1), i)
def changeSortOrder(self,logicalIndex,order):
self.sortOrder = order
self.sortByColumn(logicalIndex,self.sortOrder)
But perhaps using a delegate would be the better choice for Your purposes. There You can set a QListWidget as editor/selector for the user and You don't need to set item data additionally, the signals will be simpler:
class MyDelegate(QtWidgets.QStyledItemDelegate):
def __init__(self, t, parent=None,):
QtWidgets.QStyledItemDelegate.__init__(self)
self.setParent(parent)
self.editorTexte = t
def createEditor(self,parent, option, index):
if index.column() == 1:
return QtWidgets.QListWidget(parent)
return QtWidgets.QStyledItemDelegate.createEditor(self,parent, option, index)
def setEditorData(self, editor, index):
value = str(index.data())
if index.column() == 1:
for i, v in enumerate(self.editorTexte):
item = QtWidgets.QListWidgetItem(v)
editor.addItem(item)
if v == value:
editor.setCurrentItem(editor.item(i))
x = self.parent().columnViewportPosition(index.column())
y = self.parent().rowViewportPosition(index.row())
w = self.parent().columnWidth(index.column())
h = self.parent().rowHeight(index.row())*5
editor.setGeometry(x,y,w,h)
else:
QtWidgets.QStyledItemDelegate.setEditorData(self, editor, index)
def setModelData(self,editor,model,index):
if index.column() == 1:
v = editor.currentItem().text()
else:
v = editor.text()
model.setData(index, v)
class MyTableWidget(QtWidgets.QTableWidget):
def __init__(self, parent = None):
QtWidgets.QTableWidget.__init__(self, parent)
self.setRowCount(5)
self.setColumnCount(2)
texts = []
for i in ['A','B','C','D','E','F','G','H','I','J']:
texts.append(i)
self.setItemDelegate(MyDelegate(texts, self))
for i in range(0,self.rowCount()):
self.setItem(i,0,QtWidgets.QTableWidgetItem(str(i+1)))
self.setItem(i,1,QtWidgets.QTableWidgetItem(texts[i]))
self.setSortingEnabled(True)
self.sortOrder = 0
self.sortByColumn(1,self.sortOrder)
self.horizontalHeader().sortIndicatorChanged.connect(self.changeSortOrder)
def changeSortOrder(self,logicalIndex,order):
self.sortOrder = order
self.sortByColumn(logicalIndex,self.sortOrder)

Adding checkBox as vertical header in QtableView

I am trying to have a QTableView of checkboxes, so I can use them for row selections... I have managed to do that, now I want the header Itself to be checkbox so I can check/Uncheck All or any row . I have been looking for days, but couldn't get to do it.
I tried to use setHeaderData to the model, but couldn't do it.
Any help would be appreciated.
I wasn't particularly happy with the C++ version that #tmoreau ported to Python as it didn't:
handle more than one column
handle custom header heights (for example multi-line header text)
use a tri-state checkbox
work with sorting
So I fixed all of those issues, and created an example with a QStandardItemModel which I generally would advocate over trying to create your own model based on QAbstractTableModel.
There are probably still some imperfections, so I welcome suggestions for how to improve it!
import sys
from PyQt4 import QtCore, QtGui
# A Header supporting checkboxes to the left of the text of a subset of columns
# The subset of columns is specified by a list of column_indices at
# instantiation time
class CheckBoxHeader(QtGui.QHeaderView):
clicked=QtCore.pyqtSignal(int, bool)
_x_offset = 3
_y_offset = 0 # This value is calculated later, based on the height of the paint rect
_width = 20
_height = 20
def __init__(self, column_indices, orientation = QtCore.Qt.Horizontal, parent = None):
super(CheckBoxHeader, self).__init__(orientation, parent)
self.setResizeMode(QtGui.QHeaderView.Stretch)
self.setClickable(True)
if isinstance(column_indices, list) or isinstance(column_indices, tuple):
self.column_indices = column_indices
elif isinstance(column_indices, (int, long)):
self.column_indices = [column_indices]
else:
raise RuntimeError('column_indices must be a list, tuple or integer')
self.isChecked = {}
for column in self.column_indices:
self.isChecked[column] = 0
def paintSection(self, painter, rect, logicalIndex):
painter.save()
super(CheckBoxHeader, self).paintSection(painter, rect, logicalIndex)
painter.restore()
#
self._y_offset = int((rect.height()-self._width)/2.)
if logicalIndex in self.column_indices:
option = QtGui.QStyleOptionButton()
option.rect = QtCore.QRect(rect.x() + self._x_offset, rect.y() + self._y_offset, self._width, self._height)
option.state = QtGui.QStyle.State_Enabled | QtGui.QStyle.State_Active
if self.isChecked[logicalIndex] == 2:
option.state |= QtGui.QStyle.State_NoChange
elif self.isChecked[logicalIndex]:
option.state |= QtGui.QStyle.State_On
else:
option.state |= QtGui.QStyle.State_Off
self.style().drawControl(QtGui.QStyle.CE_CheckBox,option,painter)
def updateCheckState(self, index, state):
self.isChecked[index] = state
self.viewport().update()
def mousePressEvent(self, event):
index = self.logicalIndexAt(event.pos())
if 0 <= index < self.count():
x = self.sectionPosition(index)
if x + self._x_offset < event.pos().x() < x + self._x_offset + self._width and self._y_offset < event.pos().y() < self._y_offset + self._height:
if self.isChecked[index] == 1:
self.isChecked[index] = 0
else:
self.isChecked[index] = 1
self.clicked.emit(index, self.isChecked[index])
self.viewport().update()
else:
super(CheckBoxHeader, self).mousePressEvent(event)
else:
super(CheckBoxHeader, self).mousePressEvent(event)
if __name__=='__main__':
def updateModel(index, state):
for i in range(model.rowCount()):
item = model.item(i, index)
item.setCheckState(QtCore.Qt.Checked if state else QtCore.Qt.Unchecked)
def modelChanged():
for i in range(model.columnCount()):
checked = 0
unchecked = 0
for j in range(model.rowCount()):
if model.item(j,i).checkState() == QtCore.Qt.Checked:
checked += 1
elif model.item(j,i).checkState() == QtCore.Qt.Unchecked:
unchecked += 1
if checked and unchecked:
header.updateCheckState(i, 2)
elif checked:
header.updateCheckState(i, 1)
else:
header.updateCheckState(i, 0)
app = QtGui.QApplication(sys.argv)
tableView = QtGui.QTableView()
model = QtGui.QStandardItemModel()
model.itemChanged.connect(modelChanged)
model.setHorizontalHeaderLabels(['Title 1\nA Second Line','Title 2'])
header = CheckBoxHeader([0,1], parent = tableView)
header.clicked.connect(updateModel)
# populate the models with some items
for i in range(3):
item1 = QtGui.QStandardItem('Item %d'%i)
item1.setCheckable(True)
item2 = QtGui.QStandardItem('Another Checkbox %d'%i)
item2.setCheckable(True)
model.appendRow([item1, item2])
tableView.setModel(model)
tableView.setHorizontalHeader(header)
tableView.setSortingEnabled(True)
tableView.show()
sys.exit(app.exec_())
I had the same issue, and find a solution here, in C++. There is no easy solution, you have to create your own header.
Here's my full code with PyQt4. It seems to work with Python2 and Python3.
I also implemented the select all / select none functionality.
import sys
import signal
#import QT
from PyQt4 import QtCore,QtGui
#---------------------------------------------------------------------------------------------------------
# Custom checkbox header
#---------------------------------------------------------------------------------------------------------
#Draw a CheckBox to the left of the first column
#Emit clicked when checked/unchecked
class CheckBoxHeader(QtGui.QHeaderView):
clicked=QtCore.pyqtSignal(bool)
def __init__(self,orientation=QtCore.Qt.Horizontal,parent=None):
super(CheckBoxHeader,self).__init__(orientation,parent)
self.setResizeMode(QtGui.QHeaderView.Stretch)
self.isChecked=False
def paintSection(self,painter,rect,logicalIndex):
painter.save()
super(CheckBoxHeader,self).paintSection(painter,rect,logicalIndex)
painter.restore()
if logicalIndex==0:
option=QtGui.QStyleOptionButton()
option.rect= QtCore.QRect(3,1,20,20) #may have to be adapt
option.state=QtGui.QStyle.State_Enabled | QtGui.QStyle.State_Active
if self.isChecked:
option.state|=QtGui.QStyle.State_On
else:
option.state|=QtGui.QStyle.State_Off
self.style().drawControl(QtGui.QStyle.CE_CheckBox,option,painter)
def mousePressEvent(self,event):
if self.isChecked:
self.isChecked=False
else:
self.isChecked=True
self.clicked.emit(self.isChecked)
self.viewport().update()
#---------------------------------------------------------------------------------------------------------
# Table Model, with checkBoxed on the left
#---------------------------------------------------------------------------------------------------------
#On row in the table
class RowObject(object):
def __init__(self):
self.col0="column 0"
self.col1="column 1"
class Model(QtCore.QAbstractTableModel):
def __init__(self,parent=None):
super(Model,self).__init__(parent)
#Model= list of object
self.myList=[RowObject(),RowObject()]
#Keep track of which object are checked
self.checkList=[]
def rowCount(self,QModelIndex):
return len(self.myList)
def columnCount(self,QModelIndex):
return 2
def addOneRow(self,rowObject):
frow=len(self.myList)
self.beginInsertRows(QtCore.QModelIndex(),row,row)
self.myList.append(rowObject)
self.endInsertRows()
def data(self,index,role):
row=index.row()
col=index.column()
if role==QtCore.Qt.DisplayRole:
if col==0:
return self.myList[row].col0
if col==1:
return self.myList[row].col1
elif role==QtCore.Qt.CheckStateRole:
if col==0:
if self.myList[row] in self.checkList:
return QtCore.Qt.Checked
else:
return QtCore.Qt.Unchecked
def setData(self,index,value,role):
row=index.row()
col=index.column()
if role==QtCore.Qt.CheckStateRole and col==0:
rowObject=self.myList[row]
if rowObject in self.checkList:
self.checkList.remove(rowObject)
else:
self.checkList.append(rowObject)
index=self.index(row,col+1)
self.dataChanged.emit(index,index)
return True
def flags(self,index):
if index.column()==0:
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsUserCheckable
return QtCore.Qt.ItemIsEnabled
def headerData(self,section,orientation,role):
if role==QtCore.Qt.DisplayRole:
if orientation==QtCore.Qt.Horizontal:
if section==0:
return "Title 1"
elif section==1:
return "Title 2"
def headerClick(self,isCheck):
self.beginResetModel()
if isCheck:
self.checkList=self.myList[:]
else:
self.checkList=[]
self.endResetModel()
if __name__=='__main__':
app=QtGui.QApplication(sys.argv)
#to be able to close with ctrl+c
signal.signal(signal.SIGINT, signal.SIG_DFL)
tableView=QtGui.QTableView()
model=Model(parent=tableView)
header=CheckBoxHeader(parent=tableView)
header.clicked.connect(model.headerClick)
tableView.setModel(model)
tableView.setHorizontalHeader(header)
tableView.show()
sys.exit(app.exec_())
NB: You could store the rows in self.checkList. In my case, I often have to delete rows in random position so it was not sufficient.

Categories