×
Namespaces

Variants
Actions

Drag and drop in Qt

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): S60 Emulator
Compatibility
Platform(s): S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition
Symbian
Article
Keywords: QListWidget,QHBoxLayout
Created: mind_freak (15 Jun 2009)
Last edited: hamishwillee (11 Oct 2012)

Contents

Overview

This example shows how to drag and drop items between two QListWidget.

Qt's drag and drop infrastructure is fully supported by the model/view framework. Items in lists, tables, and trees can be dragged within the views, and data can be imported and exported as MIME-encoded data.

Preconditions

  • Download and Install the Qt SDK


Function/Property

  • This property holds the drag and drop event the view will act upon.
 list->setDragDropMode(QAbstractItemView::InternalMove);

here in the above function we can move the item or we can manually sor the item in one list..

  • This property holds the view's drag and drop behavior.

If its value is true, the selected data will overwrite the existing item data when dropped, while moving the data will clear the item.

If its value is false, the selected data will be inserted as a new item when the data is dropped. When the data is moved, the item is removed as well.


 list->setDragDropOverwriteMode ( 1 );

Source File

header.h

#include <QtGui/QWidget>
#include<QListWidget>
#include<QHBoxLayout>
 
class drag : public QWidget
{
Q_OBJECT
 
public:
drag(QWidget *parent = 0);
~drag();
 
private:
QListWidget *list;
QListWidget *list1;
QHBoxLayout *lay;
};
 
#endif // DRAG_H

Source file

#include "drag.h"
#include<QAbstractItemView>
drag::drag(QWidget *parent)
: QWidget(parent)
{
list=new QListWidget(this);
list->resize(0,0);
list1=new QListWidget(this);
lay=new QHBoxLayout(this);
list->addItem("Nokia"); //Adding item to list
list->addItem("Samsung");
list->addItem("Motorola");
list1->addItem("Nokia 6630");
list1->addItem("5800 Xpress Music");
list1->addItem("N96");
 
list->setSelectionMode(QAbstractItemView::SingleSelection); // single item can be draged or droped
list->setDragEnabled(true);
list->setDragDropMode(QAbstractItemView::InternalMove);
list->viewport()->setAcceptDrops(true);
 
list->setDropIndicatorShown(true);
 
list1->setSelectionMode(QAbstractItemView::SingleSelection);
list1->setDragEnabled(true);
list1->viewport()->setAcceptDrops(true);
 
list1->setDropIndicatorShown(true);
 
lay->addWidget(list);
lay->addWidget(list1);
setLayout(lay);
list->setStyleSheet("* { background-color:rgb(150,147,88);color:rgb(255,255,255); padding: 7px}}");
list1->setStyleSheet("* { background-color:rgb(150,140,70);color:rgb(255,255,255); padding: 7px}}");
resize(200,200);
}
 
drag::~drag()
{
 
// No need to delete any object that got a parent that is properly deleted.
 
}

ScreenShot

Drag1.JPG

Draging

Draglist001.jpg

Internal Moves

Draglist002.jpg trac

This page was last modified on 11 October 2012, at 04:16.
106 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×