[PySide] TableWidget in a GraphicsView and drawings

Sean Fisk sean at seanfisk.com
Thu Apr 4 23:11:27 CEST 2013


Hello Christophe, 

Give this code a try. I tried to make it as straightforward as possible. Use the ResizeTableGaphicsView class at the top to allow the table to resize to the graphics view if that's what you want.

Basically, we are overriding the paintEvent method of QTableWidget to allow us to paint the ellipses on the correct row and column. Don't forget that the rows and columns are labeled one-indexed, but are referred to by zero-index in the code.
I hope it helps. Let me know how it works.
#!/usr/bin/env python import sys from PySide import QtGui # Use this if you want the the table to resize with the graphics view. class TableResizingGraphicsView(QtGui.QGraphicsView): """Graphics view which contains a table widget that it resizes.""" def __init__(self, scene, table_widget, parent=None): super(TableResizingGraphicsView, self).__init__(scene, parent) self.table_widget = table_widget def resizeEvent(self, resize_event): self.table_widget.resize(resize_event.size()) class EllipseTableWidget(QtGui.QTableWidget): """Table widget that allows drawing ellipses over table cells.""" def __init__(self, rows, columns, parent=None): super(EllipseTableWidget, self).__init__(rows, columns, parent) self._ellipses = [] @property def ellipses(self): return self._ellipses def paintEvent(self, event): """Draw the widget on the screen.""" # Draw the table as normal. super(EllipseTableWidget, self).paintEvent(event) # Paint the table widget. # As stated in the QAbstractScrollArea::paintEven
t() documentation, all # painting must be done on the viewport(). painter = QtGui.QPainter(self.viewport()) for ellipse_row, ellipse_col in self.ellipses: left_offset = self.columnViewportPosition(ellipse_col) top_offset = self.rowViewportPosition(ellipse_row) width = self.columnWidth(ellipse_col) height = self.rowHeight(ellipse_row) painter.drawEllipse(left_offset, top_offset, width, height) def main(argv): app = QtGui.QApplication(argv) table_widget = EllipseTableWidget(10, 13) table_widget.ellipses.append((0, 0)) table_widget.ellipses.append((2, 9)) scene = QtGui.QGraphicsScene() proxy = scene.addWidget(table_widget) graphics_view = QtGui.QGraphicsView(scene) # graphics_view = TableResizingGraphicsView(scene, table_widget) graphics_view.showMaximized() graphics_view.raise_() app.exec_() if __name__ == '__main__': raise SystemExit(main(sys.argv))

Sincerely,

-- 
Sean Fisk


On Thursday, April 4, 2013 at 1:52 AM, Christophe BAL wrote:

> Hello,
> I would like to do one "simple" thing that is to put one TableWidget into a GraphicsView and then draw one cercle over a cell of the table, this drawing beeing done using the "table coordinates" of the cell with a ray equal to the width of the cell.
> 
> Can you give me some hints ?
> 
> Best regards.
> Christophe
> 
> 
> _______________________________________________
> PySide mailing list
> PySide at qt-project.org (mailto:PySide at qt-project.org)
> http://lists.qt-project.org/mailman/listinfo/pyside
> 
> 


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20130404/d5c75307/attachment.html>


More information about the PySide mailing list