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. QTableView Drag and Drop
Qt 6.11 is out! See what's new in the release blog

QTableView Drag and Drop

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 9.9k 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.
  • P Offline
    P Offline
    PeterPan32
    wrote on last edited by
    #1

    Hi everybody,
    I want to realize following issue with two TableViews:

    • TableView A and TableView B have a different type of data
    • The user shall be able to reorder the elements of B (! without overwriting existing data-sets !)
    • The user shall be able to drag elements from A to B (data-set must be "converted" of course)

    I thought that can't be so difficult...
    Currently I have problems with step 2 (reorder elements), because if I move on row over another the other row will be overwritten.

    Code for Table A:

    //selection behavior
        tableViewA->setSelectionBehavior(QAbstractItemView::SelectRows);
        tableViewA->setSelectionMode(QAbstractItemView::SingleSelection);
    //drag and drop
        tableViewA->setDragEnabled(true);
        tableViewA->setDropIndicatorShown(true);
        tableViewA->setDragDropMode(QAbstractItemView::InternalMove);
        tableViewA->setDragDropOverwriteMode(false);
    

    Thank you for your help.

    1 Reply Last reply
    0
    • X Offline
      X Offline
      Xicor
      wrote on last edited by
      #2

      usually when i do stuff like this i will subclass and modify the drag drop events/signals/slots to do what i want them to do.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        PeterPan32
        wrote on last edited by
        #3

        Thank you.
        I'm new to Qt. Do you have an example or something like that?

        1 Reply Last reply
        0
        • RatzzR Offline
          RatzzR Offline
          Ratzz
          wrote on last edited by
          #4

          @PeterPan32
          May be this helps you http://doc.qt.io/qt-4.8/dnd.html.

          --Alles ist gut.

          1 Reply Last reply
          0
          • P Offline
            P Offline
            PeterPan32
            wrote on last edited by
            #5

            I decided first to read the article about Model/View Programming (http://doc.qt.io/qt-5.5/model-view-programming.html).
            There is a simple dnd example. I tried it but it doesn't work. Either the example or me is wrong.
            Here is my code of the main function:

            #include "stringlistmodel.h"
            #include <QApplication>
            #include <QListView>
            
            int main(int argc, char *argv[])
            {
                QApplication app(argc, argv);
            
                QStringList list({"a","b","c","d","e","f"});
                StringListModel *model = new StringListModel(list);
                QListView *view = new QListView();
                view->setModel(model);
                view->setSelectionMode(QAbstractItemView::SingleSelection);
                view->setDragDropMode(QAbstractItemView::InternalMove);
                view->setDragEnabled(true);
                view->setAcceptDrops(true);
                view->setDropIndicatorShown(true);
                view->show();
            
                return app.exec();
            }
            

            stringlistmodel.h:

            #include <QAbstractListModel>
            #include <QStringList>
            
            class StringListModel : public QAbstractListModel
            {
                Q_OBJECT
            public:
                StringListModel(const QStringList &strings, QObject *parent = 0);
            
            
                int rowCount(const QModelIndex &parent = QModelIndex()) const;
                QVariant data(const QModelIndex &index, int role) const;
                QVariant headerData(int section, Qt::Orientation orientation,
                                    int role = Qt::DisplayRole) const;
            
                Qt::ItemFlags flags(const QModelIndex &index) const;
            
                bool insertRows(int position, int rows, const QModelIndex &index = QModelIndex());
                bool removeRows(int position, int rows, const QModelIndex &index = QModelIndex());
            
                Qt::DropActions supportedDropActions() const;
            
            private:
                QStringList stringList;
            };
            

            stringlistmodel.cpp:

            #include "stringlistmodel.h"
            
            StringListModel::StringListModel(const QStringList &strings, QObject *parent)
                : QAbstractListModel(parent), stringList(strings)
            {
            }
            
            int StringListModel::rowCount(const QModelIndex &parent) const
            {
                return stringList.count();
            }
            
            QVariant StringListModel::data(const QModelIndex &index, int role) const
            {
                if (!index.isValid())
                    return QVariant();
            
                if (index.row() >= stringList.size())
                    return QVariant();
            
                if (role == Qt::DisplayRole)
                    return stringList.at(index.row());
                else
                    return QVariant();
            }
            
            QVariant StringListModel::headerData(int section, Qt::Orientation orientation,
                                                 int role) const
            {
                if (role != Qt::DisplayRole)
                    return QVariant();
            
                if (orientation == Qt::Horizontal)
                    return QString("Column %1").arg(section);
                else
                    return QString("Row %1").arg(section);
            }
            
            Qt::ItemFlags StringListModel::flags(const QModelIndex &index) const
            {
                Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index);
            
                if (index.isValid())
                    return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
                else
                    return Qt::ItemIsDropEnabled | defaultFlags;
            }
            
            bool StringListModel::insertRows(int position, int rows, const QModelIndex &parent)
            {
                beginInsertRows(QModelIndex(), position, position+rows-1);
            
                for (int row = 0; row < rows; ++row) {
                    stringList.insert(position,"");
                }
            
                endInsertRows();
                return true;
            }
            
            bool StringListModel::removeRows(int position, int rows, const QModelIndex &parent)
            {
                beginRemoveRows(QModelIndex(), position, position+rows-1);
            
                for (int row = 0; row < rows; ++row) {
                    stringList.removeAt(position);
                }
            
                endRemoveRows();
                return true;
            }
            
            Qt::DropActions StringListModel::supportedDropActions() const
            {
                return Qt::MoveAction | Qt::CopyAction;
            }
            
            
            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