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


Qt Code:
  1. #ifndef WIDGETSETTINGS_H
  2. #define WIDGETSETTINGS_H
  3.  
  4. #include <QWidget>
  5. #include <QLabel>
  6. #include <QListView>
  7. #include <QListWidget>
  8. #include <QList>
  9. #include <QIcon>
  10. #include <QStandardItemModel>
  11. #include <QStyledItemDelegate>
  12. #include <QVBoxLayout>
  13.  
  14. #include "src/ui/delegates/settingswidgetdelgate.h"
  15.  
  16. class MyModel : public QStandardItemModel
  17. {
  18. public:
  19. MyModel(QObject *parent = 0 );
  20. Qt::ItemFlags flags(const QModelIndex&index) const;
  21. Qt::DropActions supportedDropActions() const;
  22. };
  23.  
  24. class WidgetSettings : public QWidget
  25. {
  26. Q_OBJECT
  27. public:
  28. explicit WidgetSettings(QStyledItemDelegate* listviewDelegate= 0,QWidget *parent = 0);
  29. virtual void resizeEvent(QResizeEvent *);
  30.  
  31. signals:
  32.  
  33. public slots:
  34.  
  35. private:
  36. QListView* iListView;
  37. QStyledItemDelegate* iListViewDelegate;
  38. QVBoxLayout *iMainLayout;
  39. };
  40.  
  41. #endif // WIDGETSETTINGS_H
To copy to clipboard, switch view to plain text mode 



Qt Code:
  1. #include "widgetsettings.h"
  2.  
  3.  
  4. MyModel::MyModel(QObject *parent) : QStandardItemModel(parent)
  5. {
  6.  
  7. }
  8.  
  9. Qt::ItemFlags MyModel::flags(const QModelIndex& index) const
  10. {
  11. Qt::ItemFlags flags;
  12.  
  13. if (index.isValid())
  14. {
  15. flags = Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled;
  16. qDebug() << "MyModel::flags index is valid";
  17. }
  18. else
  19. {
  20. flags = Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled;
  21. qDebug() << "MyModel::flags index is invalid";
  22. }
  23. return flags;
  24. }
  25.  
  26.  
  27.  
  28.  
  29. Qt::DropActions MyModel::supportedDropActions() const
  30. {
  31. return Qt::CopyAction | Qt::MoveAction;
  32. }
  33.  
  34. void WidgetSettings::resizeEvent(QResizeEvent *event)
  35. {
  36. QWidget::resizeEvent (event);
  37.  
  38. QRect screensizerect = QApplication::desktop()->screenGeometry();
  39.  
  40. if(iListView)
  41. {
  42. iListView->setGridSize(QSize(screensizerect.width() - 20,Constants::SETTINGS_ICON_HEIGHT + (2 * 10)));
  43. }
  44. }
  45.  
  46. WidgetSettings::WidgetSettings(QStyledItemDelegate* listviewDelegate,QWidget *parent) :
  47. QWidget(parent),
  48. iListViewDelegate(listviewDelegate)
  49. {
  50. iMainLayout = new QVBoxLayout;
  51. iMainLayout->setContentsMargins(0,0,0,0);
  52. iMainLayout->setSpacing(0);
  53.  
  54. iListView = new QListView();
  55. iListView->setFocusPolicy(Qt::NoFocus);
  56. iListView->setUniformItemSizes (true);
  57. iListView->setVerticalScrollMode (QAbstractItemView::ScrollPerPixel);
  58. iListView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  59. iListView->setItemDelegate(iListViewDelegate);
  60.  
  61. iListView->setViewMode(QListView::ListMode);
  62. iListView->setMovement(QListView::Snap);
  63. iListView->setSelectionMode(QAbstractItemView::ExtendedSelection);
  64. iListView->setDragDropMode(QAbstractItemView::InternalMove);
  65. iListView->setDragDropOverwriteMode(false);
  66.  
  67. MyModel* model = new MyModel;
  68.  
  69. QSize size(Constants::SETTINGS_ICON_WIDTH ,Constants::SETTINGS_ICON_HEIGHT);
  70.  
  71. QImageReader imageread(":/menuicons/icon_app_menu");
  72. imageread.setScaledSize(size);
  73. QImage image;
  74. imageread.read(&image);
  75. QPixmap itemImage = QPixmap::fromImage(image);
  76.  
  77. QPixmap checkboxImageOn = QPixmap(":/general/btn_checkbox_on");
  78. QPixmap checkboxImageOff = QPixmap(":/general/btn_checkbox_off");
  79.  
  80.  
  81. QString text = "item ";
  82. for(int i = 0; i < 12 ; i++)
  83. {
  84.  
  85. item->setData(QString("App Name %1").arg(QString::number(i)),SettingsWidgetDelgate::TitleTextRole);
  86.  
  87. item->setData(itemImage,SettingsWidgetDelgate::IconRole);
  88. item->setData(checkboxImageOn,SettingsWidgetDelgate::SelectionOnIconRole);
  89. item->setData(checkboxImageOff,SettingsWidgetDelgate::SelectionOffIconRole);
  90.  
  91. model->appendRow(item);
  92. }
  93.  
  94. iListView->setModel(model);
  95.  
  96. iMainLayout ->addWidget(iListView);
  97. setLayout(iMainLayout);
  98. }
To copy to clipboard, switch view to plain text mode