How to Copy – Paste Multiple Items form QTableView created by QStandardItemModel to a text/excel file?

The difficulty here is that the selected cells in the table may be non-contiguous and not in any particular order. So the task is to calculate the smallest rectangle that will include all the selected cells, and then create a data structure from that which is suitable for passing to a csv writer. Below is … Read more

paintEvent in QTableView derived class: Paint device returned engine == 0, type: 1

As QTableView is a subclass of QAbstractScrollArea you should open QPainter on its viewport: void CDerivedFromQTableView::paintEvent(QPaintEvent *event) { QTableView::paintEvent(event); // draw original content QPainter p(this->viewport()); p.drawRect(0, 0, 20, 20); } The docs say it: This event handler can be reimplemented in a subclass to receive paint events (passed in event), for the viewport() widget. Note: … Read more

How to display a Pandas data frame with PyQt5/PySide2

In the case of QTableView the data must be provided through a model since it implements the MVC (Model-View-Controller) paradigm, in the case of pandas there is no default model but we can create a custom as shown in the following part: class PandasModel(QtCore.QAbstractTableModel): def __init__(self, df = pd.DataFrame(), parent=None): QtCore.QAbstractTableModel.__init__(self, parent=parent) self._df = df.copy() … Read more