PDA

View Full Version : Internal Drag and Drop in list view , sometimes deletes items on nokia device



GuusDavidson
5th April 2011, 01:44
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.

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

I can only seem to reproduce this on the actual device.


would anyone be able to propose a solution?

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

thanks in advance

Guus Davidson





#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






#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::ScrollBarAlwaysOf f);
iListView->setItemDelegate(iListViewDelegate);

iListView->setViewMode(QListView::ListMode);
iListView->setMovement(QListView::Snap);
iListView->setSelectionMode(QAbstractItemView::ExtendedSelect ion);
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::T itleTextRole);

item->setData(itemImage,SettingsWidgetDelgate::IconRole) ;
item->setData(checkboxImageOn,SettingsWidgetDelgate::Sel ectionOnIconRole);
item->setData(checkboxImageOff,SettingsWidgetDelgate::Se lectionOffIconRole);

model->appendRow(item);
}

iListView->setModel(model);

iMainLayout ->addWidget(iListView);
setLayout(iMainLayout);
}

GuusDavidson
6th April 2011, 07:24
I should add that I can now reproduce this on the emulator.
Guus .