Results 1 to 2 of 2

Thread: QTableWidget with selection bounding box - like Excel

  1. #1
    Join Date
    Jul 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QTableWidget with selection bounding box - like Excel

    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
    Last edited by PJH; 8th June 2012 at 09:15.

  2. #2
    Join Date
    Jun 2012
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget with selection bounding box - like Excel

    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:

    Qt Code:
    1. void paint (QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{
    2. QItemDelegate::paint(painter,option,index);
    3. QRect rect1 = option.rect;
    4. rect1.setHeight(rect1.height()+1);
    5. rect1.setWidth(rect1.width()+1);
    6.  
    7. if(option.state & QStyle::State_Selected)
    8. {
    9. painter->setPen(Qt::red);
    10. painter->drawRect(rect1);
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 


    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:
    Qt Code:
    1. QPixmap pix(myfile);
    2. QIcon ico;
    3. ico.addPixmap(pix);
    4. iconitem->setIcon(ico);
    5. ui->tableWidgetZusatzaufnahme->setIconSize(size);
    6. ui->tableWidgetZusatzaufnahme->setItem(row,column,iconitem);
    To copy to clipboard, switch view to plain text mode 
    Last edited by pinkiP; 2nd August 2012 at 11:17.

Similar Threads

  1. Replies: 2
    Last Post: 15th December 2011, 22:57
  2. Replies: 3
    Last Post: 1st February 2011, 12:57
  3. Replies: 0
    Last Post: 2nd November 2010, 19:54
  4. QTableWidget row selection
    By BingoMaster in forum Newbie
    Replies: 5
    Last Post: 17th October 2009, 23:39
  5. Selection in QTableWidget
    By Nippler in forum Qt Programming
    Replies: 3
    Last Post: 30th April 2008, 09:32

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.