PDA

View Full Version : QMimeData problems between a QTableWidget and a QTreeWidget



andreime
20th March 2010, 16:13
Hello, i am trying to make it so when you drag from a table view to a tree view you can send some data and add an item to the tree view. I searched on the web and programmed something that i feel should work but, alas, it is not.
This is the treeview class.


Favorites::Favorites(QWidget *parent)
: QTreeWidget(parent)
{
//building the part for the favorites list
QTreeWidgetItem* favoritesItem = new QTreeWidgetItem();
favoritesItem->setText(0,"Favorites");
this->addTopLevelItem(favoritesItem);
favoritesItem->setExpanded(true);
this->setDragDropMode(QAbstractItemView::DropOnly);
this->setAcceptDrops(true);
addPathToFavorites(QString("asdsadasd"));
refreshFavorites();
}

void Favorites::dragEnterEvent(QDragEnterEvent* event){
qDebug()<< event->mimeData()->text(); // just prints out ""
if (event->mimeData()->hasText())
event->acceptProposedAction();
}

void Favorites::dropEvent(QDropEvent *event){
qDebug()<< event->mimeData()->text();
addPathToFavorites(event->mimeData()->text());
}

And this is the table


TableWidget::TableWidget(QWidget *parent)
: QTableWidget(parent)
{
//i've enabled drag in the main class
}

void TableWidget::mousePressEvent(QMouseEvent *event){
if(event->button() == Qt::LeftButton)
poss = event->pos();
else { ;}
QTableWidget::mousePressEvent(event);
}

void TableWidget::mouseMoveEvent(QMouseEvent *event){
if(!event->buttons() & Qt::LeftButton)
QTableWidget::mouseMoveEvent(event);
else
if((event->pos() - poss).manhattanLength() < QApplication::startDragDistance())
QTableWidget::mouseMoveEvent(event);
else{
QTableWidgetItem *item = this->itemAt(event->pos());
if(item){
QDrag* drag = new QDrag(this);
QMimeData* mimeData = new QMimeData;
mimeData->setText(item->text());
drag->setMimeData(mimeData);
qDebug()<<drag->mimeData()->hasText()<<drag->mimeData()->text();//print's what it should
}
QTableWidget::mouseMoveEvent(event);
}
}

What am I doing wrong? Any help will be greatly appreciated. Thank you.

JohannesMunk
21st March 2010, 22:06
Hi!

No time to look into it thoroughly. Aren't you missing drag->exec() ?

What happens when you drag some text from another source/application? That way you can separate source and destination problems..

If nothing works - Post a minimal compilable example, please!

HIH

Johannes

andreime
22nd March 2010, 14:34
I am a bit shocked I missed that. It works now.
But I have a follow-up question: just by using drag->exec(), it works, but when I am over the place I want to drop the data, it shows a blocked-like icon(i can't take a screenshot of it); any ideas why this is happening? should i pass something in the exec method call ?
Thanks again for the help.

JohannesMunk
22nd March 2010, 14:50
Thats a destination problem - should also happen, when you try tro drag text from let's say notepad..

You need to accept the drag in the dragMoveEvent, too.



void Favorites::dragMoveEvent(QDragMoveEvent* event)
{
if (event->mimeData()->hasText())
{
event->accept();
}
QTreeWidget::dragMoveEvent(event);
}


And call the inherited dragEnterEvent, also!

HIH

Johannes