Hi,

I am developing with QT4.6, and deploying onto a Nokia N8
I am a bit stuck getting my listview with internal drag and drop working correctly.
I am trying to create functionality that will allow the reordering of items in the list.
However I have an edge case whereby *sometimes* items just disapear from the list, when attempting to move them, or if I just keep tapping on an item it will disapear.

I understand that this is controlled from MyModel::flags but I am at a loss on how to fix it.

I have also tried the flag settings as described here http://doc.qt.nokia.com/4.6/model-view-dnd.html but always deletes the items.

I have also tried just supporting the Qt::MoveAction but this also doesn't fix my issue.

I have also tried setting no edit triggers

iListView->setEditTriggers (QAbstractItemView::NoEditTriggers), but this doesn't solve it

would anyone be able to propose a solution?

I have included my source code below (minus my drawing delegate)

thanks in advance

Guus Davidson

Code:
    #ifndef WIDGETSETTINGS_H
    #define WIDGETSETTINGS_H
     
    #include <QWidget>
    #include <QLabel>
    #include <QListView>
    #include <QListWidget>
    #include <QList>
    #include <QIcon>
    #include <QStandardItemModel>
    #include <QStyledItemDelegate>
    #include <QVBoxLayout>
     
    #include "src/ui/delegates/settingswidgetdelgate.h"
     
    class MyModel : public QStandardItemModel
    {
    public:
    MyModel(QObject *parent = 0 );
    Qt::ItemFlags flags(const QModelIndex&index) const;
    Qt::DropActions supportedDropActions() const;
    };
     
    class WidgetSettings : public QWidget
    {
    Q_OBJECT
    public:
    explicit WidgetSettings(QStyledItemDelegate* listviewDelegate= 0,QWidget *parent = 0);
    virtual void resizeEvent(QResizeEvent *);
     
    signals:
     
    public slots:
     
    private:
    QListView* iListView;
    QStyledItemDelegate* iListViewDelegate;
    QVBoxLayout *iMainLayout;
    };
     
    #endif // WIDGETSETTINGS_H

Code:
    #include "widgetsettings.h"
     
     
    MyModel::MyModel(QObject *parent) : QStandardItemModel(parent)
    {
     
    }
     
    Qt::ItemFlags MyModel::flags(const QModelIndex& index) const
    {
    Qt::ItemFlags flags;
     
    if (index.isValid())
    {
    flags = Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled;
    qDebug() << "MyModel::flags index is valid";
    }
    else
    {
    flags = Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled;
    qDebug() << "MyModel::flags index is invalid";
    }
    return flags;
    }
     
     
     
     
    Qt::DropActions MyModel::supportedDropActions() const
    {
    return Qt::CopyAction | Qt::MoveAction;
    }
     
    void WidgetSettings::resizeEvent(QResizeEvent *event)
    {
    QWidget::resizeEvent (event);
     
    QRect screensizerect = QApplication::desktop()->screenGeometry();
     
    if(iListView)
    {
    iListView->setGridSize(QSize(screensizerect.width() - 20,Constants::SETTINGS_ICON_HEIGHT + (2 * 10)));
    }
    }
     
    WidgetSettings::WidgetSettings(QStyledItemDelegate* listviewDelegate,QWidget *parent) :
    QWidget(parent),
    iListViewDelegate(listviewDelegate)
    {
    iMainLayout = new QVBoxLayout;
    iMainLayout->setContentsMargins(0,0,0,0);
    iMainLayout->setSpacing(0);
     
    iListView = new QListView();
    iListView->setFocusPolicy(Qt::NoFocus);
    iListView->setUniformItemSizes (true);
    iListView->setVerticalScrollMode (QAbstractItemView::ScrollPerPixel);
    iListView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    iListView->setItemDelegate(iListViewDelegate);
     
    iListView->setViewMode(QListView::ListMode);
    iListView->setMovement(QListView::Snap);
    iListView->setSelectionMode(QAbstractItemView::ExtendedSelection);
    iListView->setDragDropMode(QAbstractItemView::InternalMove);
    iListView->setDragDropOverwriteMode(false);
     
    MyModel* model = new MyModel;
     
    QSize size(Constants::SETTINGS_ICON_WIDTH ,Constants::SETTINGS_ICON_HEIGHT);
     
    QImageReader imageread(":/menuicons/icon_app_menu");
    imageread.setScaledSize(size);
    QImage image;
    imageread.read(&image);
    QPixmap itemImage = QPixmap::fromImage(image);
     
    QPixmap checkboxImageOn = QPixmap(":/general/btn_checkbox_on");
    QPixmap checkboxImageOff = QPixmap(":/general/btn_checkbox_off");
     
     
    QString text = "item ";
    for(int i = 0; i < 12 ; i++)
    {
    QStandardItem* item = new QStandardItem();
     
    item->setData(QString("App Name %1").arg(QString::number(i)),SettingsWidgetDelgate::TitleTextRole);
     
    item->setData(itemImage,SettingsWidgetDelgate::IconRole);
    item->setData(checkboxImageOn,SettingsWidgetDelgate::SelectionOnIconRole);
    item->setData(checkboxImageOff,SettingsWidgetDelgate::SelectionOffIconRole);
     
    model->appendRow(item);
    }
     
    iListView->setModel(model);
     
    iMainLayout ->addWidget(iListView);
    setLayout(iMainLayout);
    }