PDA

View Full Version : how to use dragMove()..



sar_van81
17th September 2007, 12:07
hi,

i had written an application in which i use drag and drop concept.in that use dragCopy for dragging the items. but i need move the item itself instead of copying. i tried using dragMove() function instead of dragCopy(). but i could not move the item. the following is my code:



class drag : public QLabel
{
public:
drag(QWidget* parent=0, const char* name=0);
protected:
void mousePressEvent( QMouseEvent * );
};

drag::drag(QWidget * parent,const char * name):
QLabel(parent,name)
{

}

void drag::mousePressEvent( QMouseEvent * a )
{
QDragObject *drobj;
if ( pixmap() )
drobj = new QImageDrag( pixmap()->convertToImage(), this );
drobj->dragMove();
}


is this correct ? if no means can anyone correct me ?

thanks in advance,

rajesh
17th September 2007, 12:41
try this:

void drag::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
QMimeData *mimeData = new QMimeData;
mimeData->setText("myDrageLabel");
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
Qt::DropAction dropAction = drag->start(Qt::CopyAction | Qt::MoveAction);
if (dropAction == Qt::MoveAction)
{
close();
update();
}
}
}
in your base class, in which you are using drag object (say myWidget)
myWidget::myWidget(QWidget *parent) :
QWidget(parent)
{
setAcceptDrops(true);
...
}
void myWidget::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasText())
{
event->acceptProposedAction();
}
else
{
event->ignore();
}
}
void myWidget::dropEvent(QDropEvent *event)
{
if (event->mimeData()->hasText())
{
//QString plainText = event->mimeData()->text();
QPoint dropPos = event->pos();
QDragObject *drobj;
if ( pixmap() )
drobj = new QImageDrag( pixmap()->convertToImage(), this );
drobj->move(dropPos);

drobj->show();

if (children().contains(event->source()))
{
event->setDropAction(Qt::MoveAction);
event->accept();
}
else
{
event->acceptProposedAction();
}
}
else
{
event->ignore();
}
}

sar_van81
18th September 2007, 05:28
Hi Rajesh,

thanks for your code. but unfortunately i'm using older version of qt(qt-3.3.5). in that i dont have QMimeData etc. but let me go through the code and try to adapt with teh functions in qt-3.3.5. but i think the problem in my code was that i used QPixmap::load() function to display the image. and then drag this image to the drop widget. this is my code:



static void addStuff( QWidget * parent, bool image, bool secret = FALSE )
{
QVBoxLayout * tll = new QVBoxLayout( parent);
if(image)
{
drag *drg = new drag(parent);
drag *drg1 = new drag(parent);
tll->addWidget( drg );
tll->addWidget( drg1 );
QPixmap stuff;
QPixmap stuff1;
if(stuff.load( "trolltech.bmp" )); ----i think this caused my image not to be moved---
itemname[0]=1;
stuff1.load( "butterfly.png" );
drg->setPixmap( stuff );
drg1->setPixmap( stuff1 );
}
else
{
drop *drp= new drop(parent);
drop *drp1= new drop(parent);
tll->addWidget( drp );
tll->addWidget( drp1 );
drp->setText("Drop the items");
drp->setFont(QFont("Helvetica",12));
drp1->setText("Drag and Drop");
drp1->setFont(QFont("Helvetica",12));
}
}
.

thinking of using QIconview. any suggestions ?

rajesh
18th September 2007, 07:28
I dont know about Qpixmap::load. but in your previous code you creating a new object and not deleteing old object. so it will create a copy of new widget.
mainly whenever you create a new widget then delete or close old widget.