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 internal drag animation
Forum Update on Monday, May 27th 2025

QTableWidget internal drag animation

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 2.4k Views
  • 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.
  • UnitScanU Offline
    UnitScanU Offline
    UnitScan
    wrote on last edited by
    #1

    This is my QTableWidget: http://imgur.com/a/0V4rh

    When I drag a line to move it to a different location, while dragging appears a copy of the line anchored to the mouse cursor. I want to know: can I change the drag 'drop animation? For example, highlight the top edge of the destination row?

    Some hint?

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      you need to reimplement the dragEnter()/dragMove()/dropEvent() of your target widget.
      With the position (taken from the event) you can determine the index at this position using QAbstractItemView::indxAt() and use QAbstractItemView::visualRect() to store the rect. Overload the paintEvent() and do your drawing on top with the rect.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      3
      • UnitScanU Offline
        UnitScanU Offline
        UnitScan
        wrote on last edited by UnitScan
        #3

        This is my attempt:

        void MainWindow::onTracklistMoved(int old_row, int new_row) {
            QTableWidgetItem *itemat = tracklist->itemAt(new_row,0);
            QRect rect = tracklist->visualItemRect(itemat);
        }
        

        Called on row internal move (note: I need to paint the entire row, not the single cell)

        void DTableWidget::paintEvent(QPaintEvent *event)
        {
            QRect rect = event->rect();
            QPainter painter(this);
            painter.setRenderHint(QPainter::Antialiasing);
            painter.setPen(Qt::black);
            painter.drawText(rect, Qt::AlignCenter,
                              "Data");
            painter.drawRect(rect);
        }
        
        

        Where

        • DTableWidget = QTableWidget subclass, with reimplemented dragEnterEvent. dragMoveEvent, dragLeaveEvent, dropEvent, keyPressEvent and paintEvent method.
        Whole DTableWidget class: header and source

        At the app lauch, QtCreator send me these errors:

        QWidget::paintEngine: Should no longer be called
        QPainter::begin: Paint device returned engine == 0, type: 1
        QPainter::setRenderHint: Painter must be active to set rendering hints
        QPainter::setPen: Painter not active
        QPainter::drawRects: Painter not active

        Can you help me?

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          your paintEvent() handler isn't correct. Should look like this:

          void DTableWidget::paintEvent(QPaintEvent *event)
          {
              QTableWidget::paintEvent();  //let it paint the default stuff!
          
              QPainter painter(this->viewport());  //you need to paint on the viewport
              painter.setRenderHint(QPainter::Antialiasing);
              painter.setPen(Qt::black);
              painter.drawText(rect, Qt::AlignCenter,  "Data");   // you need to take the rect you computed before
              painter.drawRect(rect);
          }
          

          If you need the rect for the whole line you need to take the rect of all sibling items (column count) in the row. QRect has a united() method to "concatenate" 2 rects into 1 surrounding rect.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          1
          • UnitScanU Offline
            UnitScanU Offline
            UnitScan
            wrote on last edited by
            #5

            Very well. Last question: how can I remove the copy of the row anchored to the mouse cursor while dragging?

            1 Reply Last reply
            0
            • UnitScanU Offline
              UnitScanU Offline
              UnitScan
              wrote on last edited by UnitScan
              #6

              [SOLVED]

              To remove the row copy achored to the mouse cursor while dragging, you must reimplement the startDrag() method:

              void DTableWidget::startDrag(Qt::DropActions supportedActions) {
                  QModelIndexList indexes = selectedIndexes();
                  if (indexes.count() > 0) {
                      QMimeData *data = model()->mimeData(indexes);
                      if (!data)
                          return;
                      QDrag *drag = new QDrag(this);
                      drag->setMimeData(data);
                      Qt::DropAction defaultDropAction = Qt::IgnoreAction;
                      if (defaultDropAction != Qt::IgnoreAction && (supportedActions & defaultDropAction)) 
                          defaultDropAction = defaultDropAction;
                      else if (supportedActions & Qt::CopyAction && dragDropMode() != QAbstractItemView::InternalMove) 
                          defaultDropAction = Qt::CopyAction;
                      drag->exec(defaultDropAction);
                  }
              }
              
              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