QTableWidget draw/Paint over widget in a cell ?



  • Hey

    I have a QTableWidget with QWidget in each cell with my lineedits/buttons/checkboxes etc etc. I would like to draw a custom graphic over all these widgets - mainly drop indicator, as I have a different implementation than what Qt offers. I've subclassed the drag/dorp/etc events and detected the QRect / interconnecting areas where I'd like to draw but when I subclassed the :

    void testTable::paintEvent(QPaintEvent *event) {
        QTableWidget::paintEvent(event);
        QPainter paint(viewport());
        paint.setPen(Qt::blue);
        paint.drawText(rect(), Qt::AlignCenter, "The Text");
        paint.drawLine(0, 0, 100, 100);
        //paintDropIndicator();
    }
    
    

    To do a test, it ended up painting the line under the QWidget that I have inserted in the cell.

    How can I paint the desired effect over the QWidget in cell?

    TIA


  • Qt Champions 2018

    The painting is done by the delegate, you need to subclass QStyledItemDelegate and reimplement paint.

    @Dariusz said in QTableWidget draw/Paint over widget in a cell ?:

    I have a QTableWidget with QWidget in each cell

    You should consider abandon this evil way and just go the delegate way instead


  • Lifetime Qt Champion

    Hi
    Since you want to draw on top of the actual widget, drawing on the viewport() wont work as
    its under.
    One way is to user an overlay widget
    https://stackoverflow.com/questions/19199863/draw-rectangular-overlay-on-qwidget-at-click
    ITs completely transparent and non clickable ( setAttribute(Qt::WA_TransparentForMouseEvents);
    and you can use that for paint area.



  • @VRonin sounds like the correct way to do it, will try it out thanks!

    @mrjj sounds like ninja way of doing it! Thanks that definitely juggle up some ideas for ninja stuff! Thanks!

    Regards
    Dariusz


  • Lifetime Qt Champion

    @Dariusz
    Yes drawing on top of other widgets is kinda hax-ish. :) In all cases.
    Doing with a delegate is much better and as a bonus also have better performance on large data sets.



  • I just tried using delegate, I don't get a paint call on it while I drag/drop items in view. The only thing that gets calles is treeWidget::paint and my dragMoveEvent()...

    Did I break something or do I need to force delegate paint somehow ?


  • Qt Champions 2018

    You won't get a paint event at all during drag'n'drop I would guess since the cursor is not directly painted by Qt when I'm correct.