PDA

View Full Version : Drag and Drop



allensr
11th December 2006, 20:34
I have a treeWidget that I am populating with positional data (3 columns x, y, z) that has several thousand rows. I am able to select each individual column and perform a drag and drop over to a table widget. This method works, but all of the data being exposed in the treeWidget is cumbersome. Now I would like to replace all of that data in my treeWidget with just having text that says "Position". I would then like the user to be able to select the "Position" text and drag and drop it over to the tableWidget with all of the data getting populated in the treeWidget. Does anyone know how to do this?? I think dragEnterEvent and dropEvent (maybe others?) would need reimplemented, but I'm not sure how to get the positional data to "attach" to the drag and drop event. Hope this is clear.

Thanks.

Qt 4.1.4 - MSVC 2005

thomaspu
11th December 2006, 20:50
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:
void mousePressEvent(QMouseEvent *);
void mouseMoveEvent(QMouseEvent *);

private:
void startDrag(Qt::DropActions);
QPoint startPos;
Then you'll need to implement the 3 functions:

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.....
}
}


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