QTableWidget move row
-
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/f2dac97bThe 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?
-
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.
-
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); }