I actually just got done doing exactly what you need to do.
You will need to over-ride mousePressEvent and mouseMoveEvent. Mouse move event is where you will determine if you have moved the mouse far enough to begin your dragging operation (draw dragging stuff and prep data to pass).
Your class will need to have added:
protected:
private:
void startDrag(Qt::DropActions);
protected:
void mousePressEvent(QMouseEvent *);
void mouseMoveEvent(QMouseEvent *);
private:
void startDrag(Qt::DropActions);
QPoint startPos;
To copy to clipboard, switch view to plain text mode
Then you'll need to implement the 3 functions:
{
if (event->button() == Qt::LeftButton)
startPos = event->pos();
QTreeView::mousePressEvent(event
);
//let QTreeview further process event }
{
if(event->buttons() & Qt::LeftButton)
{
int distance = (event->pos() - startPos).manhattanLength();
//have we moved far enough?
startDrag(Qt::CopyAction | Qt::MoveAction | Qt::LinkAction); //start drag
}
}
void YOUR_CLASS::startDrag(Qt::DropActions supportedActions)
{
//make sure one or more items are selected, if not, return
QModelIndexList indexes = this->selectedIndexes();
if (indexes.count() > 0)
{
//Get the mimeData from your model. You will need to create
///this functionallity most likely.
QMimeData *mimeData
= model
()->mimeData
( indexes
);
drag->setMimeData( mimeData);
drag->setPixmap( SOME_SWEET_ICON);
/*I don't think you can just give it text to display.
What shows up beside the drag is always a pixmap. Qt
generates a pixmap of the selected stuff to use for the
default drag operation. So if you want some specific text,
you'll need to generate a pixmap of the text somehow
and throw in the pixmap up in that function. Thats how
you'll show text beside the cursor. */
//let Qt block until the drag operation completes
drag->start(supportedActions);
// -or-
// if(drag->start(supportedActions) == Qt::CopyAction)
// do something.....
}
}
void YOUR_CLASS::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
startPos = event->pos();
QTreeView::mousePressEvent(event); //let QTreeview further process event
}
void YOUR_CLASS::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() & Qt::LeftButton)
{
int distance = (event->pos() - startPos).manhattanLength();
//have we moved far enough?
if (distance >= QApplication::startDragDistance())
startDrag(Qt::CopyAction | Qt::MoveAction | Qt::LinkAction); //start drag
}
QTreeView::mouseMoveEvent(event);
}
void YOUR_CLASS::startDrag(Qt::DropActions supportedActions)
{
//make sure one or more items are selected, if not, return
QModelIndexList indexes = this->selectedIndexes();
if (indexes.count() > 0)
{
//Get the mimeData from your model. You will need to create
///this functionallity most likely.
QMimeData *mimeData = model()->mimeData( indexes );
QDrag *drag = new QDrag(this);
drag->setMimeData( mimeData);
drag->setPixmap( SOME_SWEET_ICON);
/*I don't think you can just give it text to display.
What shows up beside the drag is always a pixmap. Qt
generates a pixmap of the selected stuff to use for the
default drag operation. So if you want some specific text,
you'll need to generate a pixmap of the text somehow
and throw in the pixmap up in that function. Thats how
you'll show text beside the cursor. */
//let Qt block until the drag operation completes
drag->start(supportedActions);
// -or-
// if(drag->start(supportedActions) == Qt::CopyAction)
// do something.....
}
}
To copy to clipboard, switch view to plain text mode
for more help, you can reference an online Qt book here (search on "drag"):
http://ebook-4u.ifastnet.com/c.htm
Should help a little ;p
Paul
Bookmarks