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 move row
QtWS25 Last Chance

QTableWidget move row

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtablewidgetdrag and drop
4 Posts 3 Posters 4.3k 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.
  • U Offline
    U Offline
    UnitScan
    wrote on 19 Aug 2016, 19:55 last edited by
    #1

    I created, with great difficulty, a class derived from QTableWidget interacting with external files Drag'n Drop. Now, I would make sure that my class is able to move the lines with her item inside by clicking on the row and dragging up or down with the mouse. For now, I only did this:

    Header file: https://nopaste.me/view/6e7faf69
    Source file: https://nopaste.me/view/f2dac97b

    The only problem now is determine whether the line is moved up or down, and distinguish an external file drop from a table internal move.

    Can you help me?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jan-Willem
      wrote on 19 Aug 2016, 21:37 last edited by
      #2

      Perhaps by comparing the row number before the move and after it? See QTableWidget::currentRow() which returns an int value.

      When an external file is dropped, a new row is added. So you could compare the row count before and after the drop.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Ratzz
        wrote on 20 Aug 2016, 11:57 last edited by
        #3

        Here is a zip sample. You may try.

        --Alles ist gut.

        1 Reply Last reply
        0
        • U Offline
          U Offline
          UnitScan
          wrote on 20 Aug 2016, 22:14 last edited by
          #4

          Ok, now works perfectly, but I would ask if you can change the animation drag'n'drop between the lines. Currently, during the row drag it appears a copy of a line anchored to the mouse cursor. You can change this animation?

          #include <QWidget>
          #include <QTableWidget>
          #include <QDropEvent>
          #include <QDragMoveEvent>
          #include <QDropEvent>
          #include <QMimeData>
          #include <QDebug>
          #include <QKeyEvent>
          #ifndef DTABLEWIDGET_H
          #define DTABLEWIDGET_H
          
          class DTableWidget : public QTableWidget {
              Q_OBJECT
          
              public:
                  DTableWidget(QWidget *parent = 0);
          
              public slots:
          
              signals:
                  void keyboard(QKeyEvent *event);
                  void dropped(const QMimeData* mimeData = 0);
                  void moved(int old_row, int new_row);
              protected:
                  void dragEnterEvent(QDragEnterEvent *event);
                  void dragMoveEvent(QDragMoveEvent *event);
                  void dragLeaveEvent(QDragLeaveEvent *event);
                  void dropEvent(QDropEvent *event);
                  void keyPressEvent(QKeyEvent* event);
              private:
          };
          
          #endif
          
          
          #include <QtGui>
          #include "dtablewidget.h"
          #include "nofocusproxystyle.cpp"
          
          DTableWidget::DTableWidget(QWidget *parent) : QTableWidget(parent) {
              //set widget default properties:
              setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
              setDropIndicatorShown(true);
              setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
              setEditTriggers(QAbstractItemView::NoEditTriggers);
              setDragDropMode(QAbstractItemView::DropOnly);
              setAlternatingRowColors(true);
              setSelectionBehavior(QAbstractItemView::SelectRows);
              setShowGrid(false);
              setAcceptDrops(true);
              setWordWrap(false);
              setStyleSheet("selection-background-color: yellow;"
                            "selection-color: #002041;"
                            "font-size: 75%;"
                            );
              setStyle(new NoFocusProxyStyle());
          
          }
          
          void DTableWidget::dragEnterEvent(QDragEnterEvent *event) {
              event->acceptProposedAction();
          }
          
          void DTableWidget::dragMoveEvent(QDragMoveEvent *event) {
              event->acceptProposedAction();
          }
          
          void DTableWidget::dropEvent(QDropEvent *event) {
          
              event->acceptProposedAction();
          
              if (event->mimeData()->urls().size() > 0)
                  emit dropped(event->mimeData());
              else {
                  QPoint old_coordinates = QPoint(-1,-1);
                  int dropAction = event->dropAction();
                  if(currentItem() != NULL) //Check if user is not accessing empty cell
                  {
                      old_coordinates = QPoint(currentItem()->row(), currentItem()->column());
                  }
                  QTableWidget::dropEvent(event);
                  qDebug() << "Detected drop event...";
                  if(this->itemAt(event->pos().x(), event->pos().y()) != NULL && old_coordinates != QPoint(-1, -1))
                  {
                      qDebug() << "Drop Event Accepted.";
                      qDebug() << "Source: " << old_coordinates.x() << old_coordinates.y()
                               << "Destinition: " << this->itemAt( event->pos().x(), event->pos().y() )->row()
                               << this->itemAt( event->pos().x(), event->pos().y() )->column()
                               << "Type: " << dropAction;
          
                      emit moved(old_coordinates.x(), itemAt( event->pos().x(), event->pos().y())->row());
          
                  }
              }
          }
          
          void DTableWidget::dragLeaveEvent(QDragLeaveEvent *event) {
              event->accept();
          }
          
          void DTableWidget::keyPressEvent(QKeyEvent *event) {
              emit keyboard(event);
          }
          
          
          1 Reply Last reply
          0

          3/4

          20 Aug 2016, 11:57

          • Login

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