Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableWidget draw/Paint over widget in a cell ?

QTableWidget draw/Paint over widget in a cell ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtablewidgetqpainterqwidget
7 Posts 4 Posters 3.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dariusz
    wrote on last edited by Dariusz
    #1

    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

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      3
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          Dariusz
          wrote on last edited by
          #4

          @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

          mrjjM 1 Reply Last reply
          0
          • D Dariusz

            @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

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @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.

            1 Reply Last reply
            2
            • D Offline
              D Offline
              Dariusz
              wrote on last edited by
              #6

              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 ?

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved