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"
{
public:
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);
signals:
public slots:
private:
QStyledItemDelegate* iListViewDelegate;
};
#endif // WIDGETSETTINGS_H
#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
To copy to clipboard, switch view to plain text mode
#include "widgetsettings.h"
{
}
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;
}
{
if(iListView)
{
iListView
->setGridSize
(QSize(screensizerect.
width() - 20,Constants
::SETTINGS_ICON_HEIGHT + (2 * 10)));
}
}
WidgetSettings
::WidgetSettings(QStyledItemDelegate
* listviewDelegate,
QWidget *parent
) : iListViewDelegate(listviewDelegate)
{
iMainLayout->setContentsMargins(0,0,0,0);
iMainLayout->setSpacing(0);
iListView->setFocusPolicy(Qt::NoFocus);
iListView->setUniformItemSizes (true);
iListView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
iListView->setItemDelegate(iListViewDelegate);
iListView->setDragDropOverwriteMode(false);
MyModel* model = new MyModel;
QSize size
(Constants
::SETTINGS_ICON_WIDTH ,Constants
::SETTINGS_ICON_HEIGHT);
imageread.setScaledSize(size);
imageread.read(&image);
for(int i = 0; i < 12 ; i++)
{
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);
}
#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);
}
To copy to clipboard, switch view to plain text mode
Bookmarks