PDA

View Full Version : QTableWidget with selection bounding box - like Excel



PJH
8th June 2012, 09:15
We want a QTableWidget which overlays a rectangle around a selection - just like Excel. When a contiguous area is selection, we want a black rectangle drawn around the selection. Eventually we want to add a small square or cross to the bottom right hand corner of the bounding rectangle (again just like Excel) where the user can choose to fill down.

So far we have tried subclassing QTableWidget and reimplementing paintEvent so that QTableWidget paintEvent is called and then we get a painter from the viewpoint and draw the rectangle ourselves. This sort of works but we are hitting all sorts of issues with artefacts from previously drawn rectangles as the selection changes shape. The QTableWidget is redrawing intelligently and many of the areas are not being redrawn. The screen ends up a mess.

At the moment I feel as if I am doing things the hard way and I am hoping that someone says "Just reimplement function A".

Any ideas welcome

TIA

PHooper

Added after 16 minutes:

Why is it that, after working on something for hours, you find the solution a few minutes after asking for help? Oh well.

We were on the right track, we just had to use setDirtyRegion for the entire area of the selection rather than let QTableWidget sort it out.

It all seems so obvious now...http://www.qtcentre.org/images/smilies/redface.png Once we have cleaned up the code, I will post it in case anyone else hits the same problem.

Having said all that, if anyone has a great alternative I am more than happy to listen.

Thanks

PJH

pinkiP
2nd August 2012, 11:17
:) please post the code. I'm near to the solution. A rectangle is showing, but if I set a picture as cell content, I can't see the border of the drawn rectangle :(.
For drawing the rectangle, I wrote my own itemdelegate class and rewrote the paint event like this:


void paint (QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{
QItemDelegate::paint(painter,option,index);
QRect rect1 = option.rect;
rect1.setHeight(rect1.height()+1);
rect1.setWidth(rect1.width()+1);

if(option.state & QStyle::State_Selected)
{
painter->setPen(Qt::red);
painter->drawRect(rect1);
}
}

Added after 44 minutes:

:) as you said: post to get help and get the solution by yourself some minutes later.

My Solution: I used the paint event of the previous post. Than I set the images as icons an add it to the cell:

QPixmap pix(myfile);
QIcon ico;
ico.addPixmap(pix);
QTableWidgetItem *iconitem = new QTableWidgetItem;
iconitem->setIcon(ico);
ui->tableWidgetZusatzaufnahme->setIconSize(size);
ui->tableWidgetZusatzaufnahme->setItem(row,column,iconitem);