PDA

View Full Version : How to paint a table from an item delegate?



frank100
5th April 2011, 18:20
I have a table where I display mostly text, however in some cases some cells have a list of values.

I wanted to render those lists as a one column table within the cell. For that reason I made my own item delegate and overloaded the QitemDelegate : : paint() function.

Inside, if I do not detect a list I just pass the flow :



QItemDelegate::paint( painter, option, index );


However, if I have a list I want to build that table. For testing I have:


QTableWidget* tableWidget = new QTableWidget(0);
tableWidget->setRowCount(4);
tableWidget->setColumnCount(1);

tableWidget->setItem(0, 0, new QTableWidgetItem("x"));
tableWidget->setItem(1, 0, new QTableWidgetItem("x"));
tableWidget->setItem(2, 0, new QTableWidgetItem("x"));
tableWidget->setItem(3, 0, new QTableWidgetItem("x"));


Now my idea was to use that QTableWidget to paint the table, an so far the only way I saw to pass a QPainter parameter was with the render function:



tableWidget->render(painter);


That does not work and the cell looks empty (with/within saving/restoring the state before using render() ). Any idea about how could I reuse the painter of QTableWidget to draw that table???

wysota
5th April 2011, 21:39
Why don't you just draw the items directly?

frank100
6th April 2011, 09:58
I want to have have the list separated by lines, later I might need to add a second column. I did not like the idea of reimplementing the table widget/view painter... plus, I would like to learn how to reuse the painter of other widgets

wysota
6th April 2011, 10:20
I did not like the idea of reimplementing the table widget/view painter...
Creating a new widget every time some other widget is painted is a very bad idea.


plus, I would like to learn how to reuse the painter of other widgets
When painting yourself, you'd be doing the exact same thing when it comes to "reusing the painter of another widget".

frank100
6th April 2011, 13:56
Thanks for the help, I realize I did not make a clear question :-(


Creating a new widget every time some other widget is painted is a very bad idea.

That was not my intention. Although I do that in my example, actually I defined one static QTableWidget instance inside the constructor of my item delegate. I rewrote that part within the paint() function of the question to make it less cluttered with code.


When painting yourself, you'd be doing the exact same thing when it comes to "reusing the painter of another widget".

That is what I want. For drawing a table within a cell I must align the text and draw lines for the grid. I expected that if the QTableView/QTableWidget have a painter doing what I want, there should be a way to reuse it.

Somehow I suppose I can give them the QPainter I receive from one of the parameters and let them do the drawing for me.

I could also make the question more generic. I have a table with a custom item delegate and I want this delegate to draw tables within some of the cells. I thought that painting myself was the way to go :confused:

wysota
6th April 2011, 14:22
That was not my intention. Although I do that in my example, actually I defined one static QTableWidget instance inside the constructor of my item delegate. I rewrote that part within the paint() function of the question to make it less cluttered with code.
But you still have to repopulate the widget each time you paint a cell.


That is what I want. For drawing a table within a cell I must align the text and draw lines for the grid. I expected that if the QTableView/QTableWidget have a painter doing what I want, there should be a way to reuse it.

I think you misinterpret what "painter" is.