PDA

View Full Version : Drag problem to setStartDragTime



duduqq
11th September 2008, 07:32
In my program I used QApplication::setStartDragDistance(30),QApplicatio n::setStartDragTime(1000) to changed ths drag start time and the distance,but it not work ,it also drag immediately
the code is like this,can you help me.

.H

#ifndef QDRAPBUTTON_H
#define QDRAPBUTTON_H

#include <QPushButton>
#include <QApplication>

QT_BEGIN_NAMESPACE
class QDragEnterEvent;
class QDragMoveEvent;
QT_END_NAMESPACE

class QDrapButton : public QPushButton
{
public:
QDrapButton(QWidget *parent);
protected:
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event) ;
private:
};

#endif


.CPP


#include <QtGui>

#include "QDrapButton.h"
bool m_bMove ;
int m_nMoveX ;
int m_nMoveY ;
QDrapButton::QDrapButton(QWidget *parent)
: QPushButton(parent)
{
QApplication::setStartDragDistance(30) ;
QApplication::setStartDragTime(1000) ;
m_bMove = false ;
m_nMoveX = 0 ;
m_nMoveY = 0 ;
}

void QDrapButton::mousePressEvent(QMouseEvent *event)
{
QPoint hotSpot = event->pos();

QMimeData *mimeData = new QMimeData;
mimeData->setText(objectName());
mimeData->setData("application/x-hotspot",
QByteArray::number(hotSpot.x())
+ " " + QByteArray::number(hotSpot.y()));
mimeData->setImageData(icon()) ;
QPixmap pixmap(size());
render(&pixmap);

QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->setPixmap(pixmap);
drag->setHotSpot(hotSpot);

Qt::DropAction dropAction = drag->start();

if (dropAction == Qt::MoveAction)
{
close();
update();
}
return QPushButton::mousePressEvent(event) ;
}

void QDrapButton::mouseMoveEvent(QMouseEvent *event)
{
QPoint newPoint = event->pos() ;
m_nMoveX = 0 ;
m_nMoveY = 0 ;
}

aamer4yu
11th September 2008, 09:43
First, you are overriding mousePressEvent, and starting the draf urself.
How can you expect the QApplication setting to work ?

You need to check those values when starting the drag urself

duduqq
11th September 2008, 10:28
Thank you
but if I do not overriding the QPushButton's mousePressEvent,how can I make the QPushButton can drag when mouse press.And how can I make the QApplication::setStartDragTime work.

aamer4yu
11th September 2008, 12:34
You can make a check yourself like -


if ((startPos - currentPos).manhattanLength() >=QApplication::startDragDistance()
&& elapsedTime > qApp->startDragTime() )
{
startTheDrag();
}


I hope u get the idea :)

duduqq
12th September 2008, 08:09
Thank you ,I know the problem now ;