You Are Here:

Community: Wiki

This page was last modified on 1 November 2009, at 13:28.

Drag and drop in Qt

From Forum Nokia Wiki



ID ... Creation date 15 June 2009
Platform S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition Tested on devices S60 Emulator
Category Qt for S60 Subcategory Application


Keywords (APIs, classes, methods, functions): QListWidget,QHBoxLayout

Overview

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.

This Example show,how to perform drag and drop operation in Qt.


This exmaple makes the use of following class

QListWidget-Use to list out widget

Preconditions


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

Image:Drag1.JPG

Draging

Image:Draglist001.jpg

Internal Moves

Image:Draglist002.jpg

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia