I've got a grid of analysis results that shows up in my results frame, and after creating the grid object I call grid.AutoSizeColumns() to fit the columns to the data contents. This works fine for the actual columns containing data, but the leftmost column (the 'index' column?) containing just the row indices remains fairly wide -- much wider than needed, since there's only four rows.
What I'd like to do:
-Remove the leftmost column (the gray one containing only row indices), and/or
-Remove the actual "Index" column (with the header "Index") as it's redundant, and narrow the leftmost gray column, OR
-Remove both the leftmost column AND the column labeled "index".
Note: I am aware of the grid.DeleteCols(pos=0, numCols=1) method, but this always throws an error:
wx._core.wxAssertionError: C++ assertion ""Assert failure"" failed at ..\..\src\generic\grid.cpp(1471) in wxGridTableBase::DeleteCols(): Called grid table class function DeleteCols
but your derived table class does not override this function
and I'm not sure how to "override this function". If someone could explain the error to me/how to fix it so that grid.DeleteCols() works, that would be halfway to solving my issue (the other half being narrowing or deleting that gray far-left column in the grid)
Minimal code example below, along with a screenshot of what it currently looks like (taken from the actual program, not the minimal code)
On a related note, is there an easy way to right-justify the cell values in the grid? I format the results to all have the same number of decimal places, and I think it would look neater if the values were right-justfied (or even centered).
Minimal example:
import wx
import wx.grid
import numpy as np
import pandas as pd
class StartFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent=parent,
id=wx.ID_ANY, title="title", size=wx.Size(900,600),
style=wx.CAPTION|wx.CLOSE_BOX|wx.MINIMIZE_BOX|wx.SYSTEM_MENU|wx.RESIZE_BORDER)
self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)
self.numarray=[[1,0.0000,0.071,3.535,3.313,0.22],
[10,1.000,0.071,3.535,3.888,-0.353],
[20,1.301,0.357,4.634,4.526,0.108],
[50,1.699,0.929,6.456,6.442,0.023]]
self.df = pd.DataFrame(self.numarray, columns=['Parasites/ml','log_qty',
'probability','probit','est. probit','residual'])
self.table = DataTable(self.df)
grid = wx.grid.Grid(self, -1)
grid.SetTable(self.table, takeOwnership=True)
grid.AutoSizeColumns()
self.pnl = wx.Panel(self)
self.vsizer = wx.BoxSizer(wx.VERTICAL)
self.hsizer = wx.BoxSizer(wx.HORIZONTAL)
self.hsizer.AddStretchSpacer()
self.hsizer.Add(grid, 0, wx.CENTER)
self.hsizer.AddStretchSpacer()
self.vsizer.AddStretchSpacer()
self.vsizer.Add(self.hsizer, wx.SizerFlags().Expand().Border(wx.ALL, 5))
self.vsizer.AddStretchSpacer()
self.pnl.SetSizerAndFit(self.vsizer)
class DataTable(wx.grid.GridTableBase):
def __init__(self, printdata=None):
wx.grid.GridTableBase.__init__(self)
self.headerRows = 1
if printdata is None: data = pd.DataFrame()
self.data = printdata
def GetNumberRows(self):
return len(self.data)
def GetNumberCols(self):
return len(self.data.columns) + 1
def GetValue(self, row, col):
if col == 0: return self.data.index[row]
return self.data.iloc[row, col - 1]
def SetValue(self, row, col, value):
self.data.iloc[row, col - 1] = value
def GetColLabelValue(self, col):
if col == 0:
if self.data.index.name is None: return 'Index'
else: return self.data.index.name
return str(self.data.columns[col - 1])
def main():
app = wx.App()
frm = StartFrame(None)
frm.Show()
app.MainLoop()
if __name__ == "__main__":
main()
You can Hide Columns and/or Rows, so that is an easy method of dealing with the errant left-hand column.
Hide comes in useful for other things as well e.g. grid.HideRowLabels(), will hide those pesky row numbers of the left.
The alignment issue could probably be handled by using a wx.grid.GridCellAttrProvider or you can simply iterate over the grid setting each cell, as I have done below for simplicity.
import wx
import wx.grid
import numpy as np
import pandas as pd
class StartFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent=parent,
id=wx.ID_ANY, title="title", size=wx.Size(900,600),
style=wx.CAPTION|wx.CLOSE_BOX|wx.MINIMIZE_BOX|wx.SYSTEM_MENU|wx.RESIZE_BORDER)
self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)
self.numarray=[[1,0.0000,0.071,3.535,3.313,0.22],
[10,1.000,0.071,3.535,3.888,-0.353],
[20,1.301,0.357,4.634,4.526,0.108],
[50,1.699,0.929,6.456,6.442,0.023]]
self.df = pd.DataFrame(self.numarray, columns=['Parasites/ml','log_qty',
'probability','probit','est. probit','residual'])
self.table = DataTable(self.df)
grid = wx.grid.Grid(self, -1)
grid.SetTable(self.table, takeOwnership=True)
# Hide Index column
grid.HideCol(0)
grid.HideRowLabels()
# Alignment of cells
r = grid.GetNumberRows()
c = grid.GetNumberCols()
for row in range(r):
for cell in range(c):
grid.SetCellAlignment(row, cell, wx.ALIGN_RIGHT, wx.ALIGN_CENTER)
grid.AutoSizeColumns()
self.pnl = wx.Panel(self)
self.vsizer = wx.BoxSizer(wx.VERTICAL)
self.hsizer = wx.BoxSizer(wx.HORIZONTAL)
self.hsizer.AddStretchSpacer()
self.hsizer.Add(grid, 0, wx.CENTER)
self.hsizer.AddStretchSpacer()
self.vsizer.AddStretchSpacer()
self.vsizer.Add(self.hsizer, wx.SizerFlags().Expand().Border(wx.ALL, 5))
self.vsizer.AddStretchSpacer()
self.pnl.SetSizerAndFit(self.vsizer)
class DataTable(wx.grid.GridTableBase):
def __init__(self, printdata=None):
wx.grid.GridTableBase.__init__(self)
self.headerRows = 1
if printdata is None: data = pd.DataFrame()
self.data = printdata
def GetNumberRows(self):
return len(self.data)
def GetNumberCols(self):
return len(self.data.columns) + 1
def GetValue(self, row, col):
if col == 0: return self.data.index[row]
return self.data.iloc[row, col - 1]
def SetValue(self, row, col, value):
self.data.iloc[row, col - 1] = value
def GetColLabelValue(self, col):
if col == 0:
if self.data.index.name is None: return 'Index'
else: return self.data.index.name
return str(self.data.columns[col - 1])
def main():
app = wx.App()
frm = StartFrame(None)
frm.Show()
app.MainLoop()
if __name__ == "__main__":
main()
Related
I have an application which contains a QTableView for which I would like to have the possibility to sort its contents but also to remove one or more rows. Below is an example code that implements this.
from PyQt5 import QtCore, QtWidgets
class NeXuSFilesModel(QtCore.QAbstractTableModel):
fields = ['col1','col2']
def __init__(self,parent):
super(NeXuSFilesModel,self).__init__(parent)
self._nexus_contents = [['aaa','111'],['bbb','222'],['ccc','333']]
def columnCount(self, parent=None):
return 2
def data(self,index,role):
if not index.isValid():
return QtCore.QVariant()
row = index.row()
col = index.column()
if role == QtCore.Qt.DisplayRole:
return str(self._nexus_contents[row][col])
else:
return QtCore.QVariant()
def headerData(self, index, orientation, role):
if role == QtCore.Qt.DisplayRole:
if orientation == QtCore.Qt.Horizontal:
return NeXuSFilesModel.fields[index]
else:
return index + 1
return None
def removeRow(self, row, parent):
self.beginRemoveRows(QtCore.QModelIndex(),row,row+1)
del self._nexus_contents[row]
self.endRemoveRows()
return True
def rowCount(self, parent=None):
return len(self._nexus_contents)
class NeXuSDataTableView(QtWidgets.QTableView):
def __init__(self,parent):
super(NeXuSDataTableView,self).__init__(parent)
self.horizontalHeader().setStretchLastSection(True)
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Delete:
model = self.model()
selected_indexes = self.selectionModel().selectedRows()
source_indexes_rows = sorted([model.mapToSource(index).row() for index in selected_indexes],reverse=True)
for row in source_indexes_rows:
model.sourceModel().removeRow(row,QtCore.QModelIndex())
super(NeXuSDataTableView, self).keyPressEvent(event)
class MainWindow(QtWidgets.QWidget):
def __init__(self):
super(MainWindow,self).__init__()
self._table = NeXuSDataTableView(self)
model = NeXuSFilesModel(self)
proxy_model = QtCore.QSortFilterProxyModel()
proxy_model.setSourceModel(model)
self._table.setModel(proxy_model)
self._table.setSortingEnabled(True)
mainLayout = QtWidgets.QVBoxLayout()
mainLayout.addWidget(self._table)
self.setLayout(mainLayout)
self.show()
if __name__ == '__main__':
app = QtWidgets.QApplication([])
_ = MainWindow()
app.exec_()
When I run that code, I fall into several problems for which I could not find the solution or could not understand the explanations given by the various sources I could find.
When the program starts, the data is showed initially in the wrong order. Indeed, it is displayed in descending order whereas I would like to display it in ascending order
When I remove one item, it removes actually two items !
Would you have any idea about what is wrong with my implementation ?
About both your questions:
proxy_model.sort(0, QtCore.Qt.AscendingOrder) after self._table.sortSortingEnabled(True) results in an ascending order:
self._table.setSortingEnabled(True)
proxy_model.sort(0, QtCore.Qt.AscendingOrder)
mainLayout = QtWidgets.QVBoxLayout()
mainLayout.addWidget(self._table)
self.setLayout(mainLayout)
self.show()
Using self.beginRemoveRows(QtCore.QModelIndex(),row,row) will remove only one row.
I'm having problem to show the Editor Widget when Delegate is applied with the Proxy situation.
-> self.table.setModel(self.proxy)
If the Delegate is applied to the View/Model structure, then there is no problem at all.
-> #self.table.setModel(self.model)
Refer to: https://www.pythonfixing.com/2021/10/fixed-adding-row-to-qtableview-with.html
See the code below:
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
class Delegate(QItemDelegate):
def __init__(self):
QItemDelegate.__init__(self)
self.type_items = ["1", "2", "3"]
def createEditor(self, parent, option, index):
if index.column() == 0:
comboBox = QComboBox(parent)
comboBox.addItems(self.type_items)
return comboBox
# no need to check for the other columns, as Qt automatically creates a
# QLineEdit for string values and QTimeEdit for QTime values;
return super().createEditor(parent, option, index)
class TableModel(QAbstractTableModel):
def __init__(self, data):
super(TableModel, self).__init__()
self._data = data
def appendRowData(self, data):
self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount())
self._data.append(data)
self.endInsertRows()
def data(self, index, role=Qt.DisplayRole):
if role in (Qt.DisplayRole, Qt.EditRole):
return self._data[index.row()][index.column()]
def setData(self, index, value, role=Qt.EditRole):
if role == Qt.EditRole:
self._data[index.row()][index.column()] = value
self.dataChanged.emit(index, index)
return True
return False
def rowCount(self, index=None):
return len(self._data)
def columnCount(self, index=None):
return len(self._data[0])
def flags(self, index):
# allow editing of the index
return super().flags(index) | Qt.ItemIsEditable
class CustomProxyModel(QSortFilterProxyModel):
def __init__(self, parent=None):
super().__init__(parent)
self._filters = dict()
#property
def filters(self):
return self._filters
def setFilter(self, expresion, column):
if expresion:
self.filters[column] = expresion
elif column in self.filters:
del self.filters[column]
self.invalidateFilter()
def filterAcceptsRow(self, source_row, source_parent):
for column, expresion in self.filters.items():
text = self.sourceModel().index(source_row, column, source_parent).data()
regex = QRegExp(
expresion, Qt.CaseInsensitive, QRegExp.RegExp
)
if regex.indexIn(text) == -1:
return False
return True
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
localWidget = QWidget()
self.table = QTableView(localWidget)
data = [["1", "Hi", QTime(2, 1)], ["2", "Hello", QTime(3, 0)]]
self.model = TableModel(data)
self.proxy = CustomProxyModel() # Customized Filter
self.proxy.setSourceModel(self.model)
#self.table.setModel(self.model) # Original code, for View/Model
self.table.setModel(self.proxy) # Revised code, for View/Proxy/Model
self.table.setItemDelegate(Delegate())
self.add_row = QPushButton("Add Row", localWidget)
self.add_row.clicked.connect(self.addRow)
for row in range(self.model.rowCount()):
for column in range(self.model.columnCount()):
index = self.model.index(row, column)
self.table.openPersistentEditor(index) # openPersistentEditor for createEditor
layout_v = QVBoxLayout()
layout_v.addWidget(self.table)
layout_v.addWidget(self.add_row)
localWidget.setLayout(layout_v)
self.setCentralWidget(localWidget)
self.show()
def addRow(self):
row = self.model.rowCount()
new_row_data = ["3", "Howdy", QTime(9, 0)]
self.model.appendRowData(new_row_data)
for i in range(self.model.columnCount()):
index = self.model.index(row, i)
self.table.openPersistentEditor(index) # openPersistentEditor for createEditor
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
Test with View/Model, Widget Editor display.
Test with View/Proxy/Model, Widget Editor not display.
Any attempt to access the view indexes must use the view's model.
Your code doesn't work because the index you are providing belongs to another model, so the editor cannot be created because the view doesn't recognize the model of the index as its own: the view uses the proxy model, while you're trying to open an editor for the source model.
While in this case the simplest solution would be to use self.proxy.index(), the proper solution is to always refer to the view's model.
Change both self.model.index(...) to self.table.model().index(...).
Yes, thanks you very much.
Revised the code as below, now the Widget Editor display accordingly.
for row in range(self.model.rowCount()):
for column in range(self.model.columnCount()):
#index = self.model.index(row, column) # original code, which get the wrong index from the model
index = self.proxy.index(row, column) # revised code, get the correct index from the proxy
self.table.openPersistentEditor(index) # openPersistentEditor for createEditor
Following on from a previous question here, I looked to add delete row on key press functionality to my qtableview table in PyQt5 by adding the removeRows function to my model. However, since adding this function it has disrupted my drag and drop functionality, whereby the dragged row disappears when dropping elsewhere in the qtableview table.
Is there anyway I can prevent the dragged row from disappearing?
NB: Interestingly, when selecting the vertical header 'column' the drag/drop functionality works, but I'm keen to find a solution for dragging and dropping on row selection.
Here's my code below with the added removeRows function in the model, and also the keyPressEvent in the view
from PyQt5.QtGui import QBrush
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QAbstractTableModel, Qt, QModelIndex
class myModel(QAbstractTableModel):
def __init__(self, data, parent=None, *args):
super().__init__(parent, *args)
self._data = data or []
self._headers = ['Type', 'result', 'count']
def rowCount(self, index=None):
return len(self._data)
def columnCount(self, index=None):
return len(self._headers)
def headerData(self, section, orientation, role=Qt.DisplayRole):
if role == Qt.DisplayRole:
if orientation == Qt.Horizontal:
if section < 0 or section >= len(self._headers):
return ""
else:
return self._headers[section]
else:
return ''
return None
def removeRows(self, position, rows, QModelIndex):
self.beginRemoveRows(QModelIndex, position, position + rows - 1)
for i in range(rows):
del (self._data[position])
self.endRemoveRows()
self.layoutChanged.emit()
return True
def data(self, index, role=None):
if role == Qt.TextAlignmentRole:
return Qt.AlignHCenter
if role == Qt.ForegroundRole:
return QBrush(Qt.black)
if role == Qt.BackgroundRole:
if (self.index(index.row(), 0).data().startswith('second')):
return QBrush(Qt.green)
else:
if (self.index(index.row(), 1).data()) == 'abc':
return QBrush(Qt.yellow)
if (self.index(index.row(), 1).data()) == 'def':
return QBrush(Qt.blue)
if (self.index(index.row(), 1).data()) == 'ghi':
return QBrush(Qt.magenta)
if role in (Qt.DisplayRole, Qt.EditRole):
return self._data[index.row()][index.column()]
def flags(self, index: QModelIndex) -> Qt.ItemFlags:
return Qt.ItemIsDropEnabled | Qt.ItemIsEnabled | Qt.ItemIsEditable | Qt.ItemIsSelectable | Qt.ItemIsDragEnabled
def supportedDropActions(self) -> bool:
return Qt.MoveAction | Qt.CopyAction
class myTableView(QTableView):
def __init__(self, parent):
super().__init__(parent)
header = self.verticalHeader()
header.setSectionsMovable(True)
header.setSectionResizeMode(QHeaderView.Fixed)
header.setFixedWidth(10)
QShortcut('F7', self, self.getLogicalRows)
QShortcut('F6', self, self.toggleVerticalHeader)
QShortcut('Alt+Up', self, lambda: self.moveRow(True))
QShortcut('Alt+Down', self, lambda: self.moveRow(False))
self.setSelectionBehavior(self.SelectRows)
self.setSelectionMode(self.SingleSelection)
self.setDragDropMode(self.InternalMove)
self.setDragDropOverwriteMode(False)
def dropEvent(self, event):
if (event.source() is not self or
(event.dropAction() != Qt.MoveAction and
self.dragDropMode() != QAbstractItemView.InternalMove)):
super().dropEvent(event)
selection = self.selectedIndexes()
from_index = selection[0].row() if selection else -1
to_index = self.indexAt(event.pos()).row()
if (0 <= from_index < self.model().rowCount() and
0 <= to_index < self.model().rowCount() and
from_index != to_index):
header = self.verticalHeader()
from_index = header.visualIndex(from_index)
to_index = header.visualIndex(to_index)
header.moveSection(from_index, to_index)
event.accept()
super().dropEvent(event)
def toggleVerticalHeader(self):
self.verticalHeader().setHidden(self.verticalHeader().isVisible())
def moveRow(self, up=True):
selection = self.selectedIndexes()
if selection:
header = self.verticalHeader()
row = header.visualIndex(selection[0].row())
if up and row > 0:
header.moveSection(row, row - 1)
elif not up and row < header.count() - 1:
header.moveSection(row, row + 1)
def getLogicalRows(self):
header = self.verticalHeader()
for vrow in range(header.count()):
lrow = header.logicalIndex(vrow)
index = self.model().index(lrow, 0)
print(index.data())
def keyPressEvent(self, event):
if event.key() == Qt.Key_Delete:
index = self.currentIndex()
try:
self.model().removeRows(index.row(), 1, index)
except IndexError:
pass
else:
super().keyPressEvent(event)
class sample_data(QMainWindow):
def __init__(self):
super().__init__()
tv = myTableView(self)
tv.setModel(myModel([
["first", 'abc', 123],
["second"],
["third", 'def', 456],
["fourth", 'ghi', 789],
]))
self.setCentralWidget(tv)
tv.setSpan(1, 0, 1, 3)
if __name__ == '__main__':
app = QApplication(['Test'])
test = sample_data()
test.setGeometry(600, 100, 350, 185)
test.show()
app.exec_()
The main "problem" is that, by default, removeRows of QAbstractItemModel doesn't do anything (and returns False).
The technical problem is a bit more subtle.
A drag operation in an item view always begins with startDrag(), which creates a QDrag object and calls its exec(). When the user drops the data, that implementation also calls a private clearOrRemove function whenever the accepted drop action is MoveAction, which eventually overwrites the data or removes the row(s).
You've used setDragDropOverwriteMode(False), so it will call removeRows. Your previous code used to work because, as said, the default implementation does nothing, but now you've reimplemented it, and it actually deletes rows in that case.
The solution is to change the drop action whenever the drop event is a move (which is a bit unintuitive, but since the operation has been already performed, that shouldn't be an issue). Using IgnoreAction will avoid the unwanted behavior, as that clearOrRemove won't be called anymore in that case:
def dropEvent(self, event):
# ...
if (0 <= from_index < self.model().rowCount() and
0 <= to_index < self.model().rowCount() and
from_index != to_index):
# ...
header.moveSection(from_index, to_index)
event.accept()
event.setDropAction(Qt.IgnoreAction)
super().dropEvent(event)
UPDATE
When the items don't fill the whole viewport, drop events can occur outside the items range, resulting in an invalid QModelIndex from indexAt() not only when dropping beyond the last row, but also the last column.
Since you're only interested in vertical movement, the solution is to get the to_index from the vertical header, and eventually set it to the last row whenever it's still invalid (-1):
def dropEvent(self, event):
if (event.source() is not self or
(event.dropAction() != Qt.MoveAction and
self.dragDropMode() != QAbstractItemView.InternalMove)):
super().dropEvent(event)
selection = self.selectedIndexes()
from_index = selection[0].row() if selection else -1
globalPos = self.viewport().mapToGlobal(event.pos())
header = self.verticalHeader()
to_index = header.logicalIndexAt(header.mapFromGlobal(globalPos).y())
if to_index < 0:
to_index = header.logicalIndex(self.model().rowCount() - 1)
if from_index != to_index:
from_index = header.visualIndex(from_index)
to_index = header.visualIndex(to_index)
header.moveSection(from_index, to_index)
event.accept()
event.setDropAction(Qt.IgnoreAction)
super().dropEvent(event)
This question already has an answer here:
pyqt QTableWidgetItem connect signal
(1 answer)
Closed 3 years ago.
I am trying to retrieve the editted data from a pyqt5 qtablewidget and am struggling to understand how to do this. My simple code is shown below:
class Delegate(QStyledItemDelegate):
def createEditor(self, parent, option, index):
DBL_MAX = 1.7976931348623157e308
editor = QDoubleSpinBox(parent, minimum=-DBL_MAX, maximum=DBL_MAX, decimals=323)
return editor
class TableView(QTableWidget):
def __init__(self, z, *args):
super(TableView, self).__init__(*args)
self.z = z
self.setz()
self.resizeColumnsToContents()
self.resizeRowsToContents()
delegate = Delegate(self)
self.setItemDelegate(delegate)
def setz(self):
horHeaders = []
for j, (key, values) in enumerate(sorted(self.z.items())):
horHeaders.append(key)
for i, value in enumerate(values):
newitem = QTableWidgetItem()
newitem.setData(Qt.EditRole, value)
self.setItem(i, j, newitem)
self.setHorizontalHeaderLabels(horHeaders)
def slot(self):
row= self.tableWidget.currentItem()
print(str(row))
def main(args):
z = {
"Let's Sum This Row 1": [0, 0],
"Let's Sum This Row 2": [0, 0],
}
app = QApplication(args)
table = TableView(z, 2, 2)
table.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main(sys.argv)
a = z
sum1=a[0][0]+a[0][1]
sum2=a[1][0]+a[1][1]
print(sum1)
print(sum2)
In my simple example my initial z data is a zeros. Let's say that the user changes the first rows to 1's and the second row to 2's.
The end output I want is sum1 = 2 and sum2 = 4.
How do i tell my program to read the end user editted matrix and store that back to a variable so that I can do more work further on in my program?
From my research, it looks like itemchanged may do the trick, but I am struggling to understand how to implement this in my code.
edited your code to add itemChanged and calculate
from PyQt5.QtWidgets import (QStyledItemDelegate, QDoubleSpinBox, QTableWidget, QTableWidgetItem, QApplication)
from PyQt5.QtCore import (Qt, QSize)
import sys
class Delegate(QStyledItemDelegate):
def createEditor(self, parent, option, index):
DBL_MAX = 1.7976931348623157e308
editor = QDoubleSpinBox(parent, minimum=-DBL_MAX, maximum=DBL_MAX, decimals=323)
return editor
class TableView(QTableWidget):
def __init__(self, z, *args):
super(TableView, self).__init__(*args)
self.setMinimumSize(QSize(800, 600))
self.z = z
self.setz()
self.resizeColumnsToContents()
self.resizeRowsToContents()
delegate = Delegate(self)
self.setItemDelegate(delegate)
def setz(self):
horHeaders = []
for j, (key, values) in enumerate(sorted(self.z.items())):
horHeaders.append(key)
for i, value in enumerate(values):
newitem = QTableWidgetItem()
newitem.setData(Qt.EditRole, value)
newitem
self.setItem(i, j, newitem)
self.setHorizontalHeaderLabels(horHeaders)
def slot(self):
row = self.tableWidget.currentItem()
print(str(row))
def main(args):
z = {
"Let's Sum This Row 1": [0, 0],
"Let's Sum This Row 2": [0, 0],
}
app = QApplication(args)
table = TableView(z, 2, 2)
def _calculate():
for j in range(table.columnCount()):
counter = 0
for i in range(table.rowCount()):
counter = counter + int(table.item(i, j).data(0))
print(f"col{j}: sum={counter}")
table.itemChanged.connect(_calculate)
table.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main(sys.argv)
With the sample code below (heavily influenced from here) the right-click context menu is not really aligned properly.
As can be seen in the screenshot, the resulting menu is above the mouse cursor quite a bit. I would expect the menu's top left corner to be exactly aligned with the mouse pointer.
Is there any way to adjust for this?
import re
import operator
import os
import sys
import sqlite3
import cookies
from PyQt4.QtCore import *
from PyQt4.QtGui import *
def main():
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
self.tabledata = [('apple', 'red', 'small'),
('apple', 'red', 'medium'),
('apple', 'green', 'small'),
('banana', 'yellow', 'large')]
self.header = ['fruit', 'color', 'size']
# create table
self.createTable()
# layout
layout = QVBoxLayout()
layout.addWidget(self.tv)
self.setLayout(layout)
def popup(self, pos):
for i in self.tv.selectionModel().selection().indexes():
print i.row(), i.column()
menu = QMenu()
quitAction = menu.addAction("Quit")
action = menu.exec_(self.mapToGlobal(pos))
if action == quitAction:
qApp.quit()
def createTable(self):
# create the view
self.tv = QTableView()
self.tv.setStyleSheet("gridline-color: rgb(191, 191, 191)")
self.tv.setContextMenuPolicy(Qt.CustomContextMenu)
self.tv.customContextMenuRequested.connect(self.popup)
# set the table model
tm = MyTableModel(self.tabledata, self.header, self)
self.tv.setModel(tm)
# set the minimum size
self.tv.setMinimumSize(400, 300)
# hide grid
self.tv.setShowGrid(True)
# set the font
font = QFont("Calibri (Body)", 12)
self.tv.setFont(font)
# hide vertical header
vh = self.tv.verticalHeader()
vh.setVisible(False)
# set horizontal header properties
hh = self.tv.horizontalHeader()
hh.setStretchLastSection(True)
# set column width to fit contents
self.tv.resizeColumnsToContents()
# set row height
nrows = len(self.tabledata)
for row in xrange(nrows):
self.tv.setRowHeight(row, 18)
# enable sorting
self.tv.setSortingEnabled(True)
return self.tv
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__":
main()
the position is in viewport coordinate, so if you are using
self.tableView.setContextMenuPolicy(Qt.CustomContextMenu)
so you don't have event passed to popup, you can do the following
action = menu.exec_(self.tableView.viewport().mapToGlobal(pos))
instead.
This was a bit tricky, but following the subclassing example in this wiki example and replacing
15 action = menu.exec_(self.mapToGlobal(event.pos()))
with
15 action = menu.exec_(event.globalPos())
will make the popup menu's top left corner match the mouse click exactly.
This will work for maximized/minified windows.
Menu will be generated at right-bottom position of mouse.
menu.exec_(self.mapToGlobal(self.mapFromGlobal(QtGui.QCursor.pos())))