PDA

View Full Version : Draw on a QTableWidget



Corran
6th January 2006, 20:23
I'm reletively new to Qt so I'm still learning the best ways to do thing. Quite often I guess there's a simple solution which I don't know about.

Anyway, I have a QTableWidget which I've set up so that each cell is square. However, I want to delineate blocks of cells to make it clearer. I want blocks of 3x3 cells to have a darker border around them as in the picture attached.

As far as I can tell, to do this I will have to subclass QTableWidget and reimplement its paintevent() function and draw the lines there. However, if I do this I don't know how to make it draw the table, text, etc as usual. The lines are static and just have to sit atop the table so is there any easier way do do this?

Many thanks

axeljaeger
6th January 2006, 20:38
You can overwrite paintEvent and call the old paintEvent in your new paintEvent using QTableWidget::paintEvent, e.g:



void MyTableWidget::paintEvent(QPaintEvent* event) {
// first call the old paintEvent to draw text and lines and stuff:
QTableWidget::paintEvent(event);

// Now add your own drawing code here:
...
}

Corran
6th January 2006, 22:08
I used the code you gave me and just to try it I used an example given in the QPainter documentation:
void TableGrid::paintEvent(QPaintEvent* event)
{
QTableWidget::paintEvent(event);

QPainter paint(this);
paint.setPen(Qt::blue);
paint.drawText(rect(), Qt::AlignCenter, "The Text");
} However, this gives me run-time warnings saying "QPainter::begin: Widget painting can only begin as a result of a paintEvent" yet it compiles just fine. This is the only place I have implemented any painting functions so this must be the cause. However, the error doesn't make any sense as the call is from within the paintEvent function.

It is not a problem with the code you gave me as it gives the same errors if I reduce the code to:
void TableGrid::paintEvent(QPaintEvent* event)
{
QPainter paint(this);
}So it is obviously being caused by the begin() call in the constructor which for some reason thinks it's not being called from a paintEvent function. Any ideas?

I don't know if this makes any difference but I'm using Qt 4.1 under Linux.

axeljaeger
6th January 2006, 22:12
How do you trigger the paintEvent? By calling update? Or dont't you trigger it by yourself and only let Qt trigger it when neccessary?

Corran
6th January 2006, 22:41
I'm letting Qt call the event whenever it wants to. It seems to do it whenever a cell is selected or deselected and when it is moved. The function is definitely being called though.

I can even reproduce the problem in a minimal program where the only thing I do is create the custom table and try to draw in it's paintEvent function.

axeljaeger
6th January 2006, 22:56
From http://doc.trolltech.com/4.1/qabstractscrollarea.html#paintEvent


void QAbstractScrollArea::paintEvent ( QPaintEvent * event ) [virtual protected]

This event handler can be reimplemented in a subclass to receive paint events (passed in event), for the viewport() widget.

Note: If you open a painter, make sure to open it on the viewport().

Reimplemented from QWidget.

See also QWidget::paintEvent().


Maybe this helps

Corran
6th January 2006, 23:17
That sorted it, thanks a lot. I'd only read the QWidget paintEvent documentation. It didn't occur to me that it would be subclassed halfway through the system like that.