Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-75057

QAbstractItemView : cannot drag non-selectable items

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • 5.12.2
    • None
    • Windows 10 pro x64
    • Windows

    Description

      I have a model derived from QAbstractItemModel. When I displayed it in a view, items are not draggable unless Qt::ItemIsSelectable flag is set.

      Minimal example :

      #include <QApplication>
      #include <QAbstractItemModel>
      #include <QListView>
      #include <QMimeData>
      
      class Model : public QAbstractItemModel
      {
      public:
          Model(QObject* parent = nullptr) : QAbstractItemModel { parent } { }
      
      protected:
          virtual QModelIndex index(int row, int column, const QModelIndex &parent) const override
          {
              if(row >= 0 && row < 2)
                  return createIndex(row, column);
              return { };
          }
      
          virtual QModelIndex parent(const QModelIndex &index) const override
          {
              return { };
          }
      
          virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override
          {
              return 2;
          }
      
          virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override
          {
              return 1;
          }
      
          virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override
          {
              if(role == Qt::DisplayRole)
                  return QString("%1selectable -> %1draggable").arg(index.row() == 0 ? "" : "not ");
              return { };
          }
      
          virtual Qt::ItemFlags flags(const QModelIndex &index) const override
          {
              if(index.row() == 0)
                  return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled;
              else
                  return Qt::ItemIsEnabled | Qt::ItemIsDragEnabled;
          }
      
          virtual QMimeData* mimeData(const QModelIndexList &indexes) const override
          {
              return new QMimeData();
          }
      };
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
          QListView view;
          view.show();
          view.setDragEnabled(true);
          view.setDragDropMode(QAbstractItemView::DragOnly);
      
          Model model;
          view.setModel(&model);
      
          return a.exec();
      }
      
      

       

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            dfaure_kdab David Faure
            gruk Gruk
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes