PDA

View Full Version : Drag an item from QTreeWidget and drop in TableView



steg90
18th May 2007, 11:35
Hi,

I have a treewidget of which I want to be able to drag items from and drop them in a tableview, the tableview uses a model so after the drop I would need to update the data structures my model uses. Is it possible to drag an item from treewidget to tableview?

Regards,
Steve

wysota
18th May 2007, 11:37
Yes. Make sure they are aware of each others mime types (or use the same).

steg90
18th May 2007, 12:02
Thanks,

They will be using the same data ( the data is shared from another class ), the data is only text so I'm sure the mime type will be fine. Guess I start with using QDrag and capture mousePressEvent?

Regards,
Steve

wysota
18th May 2007, 12:04
The data is irrelevant. You have to program models (or items) so that they use the same MIME.

steg90
18th May 2007, 12:17
Ok, guess I need to do some reading on QMimeData...

Also, would I need to have my QTableView and QTreeWidget derived in my own classes in order to capture the mouse events, I've added a mouseEvent to my QTreeWidget form and nothing happens, never gets called...

Regards,
Steve

marcel
18th May 2007, 12:20
It is not mouseEvent but there are three mouse event handlers:
mouseMoveEvent, mouseReleaseEvent and mousePressEvent.
You should reimplement these, and it will work.

steg90
18th May 2007, 12:24
Ok,

I was just reading the documentation on Drag and Drop in the QT assistant and it shows an example using mousePressEvent which is what I was meant to say! I have this event in my tree widget, but it never gets called?

Code :



DAMessageTree::DAMessageTree(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
QStringList list;
list << "Messages";
ui.treeList->setHeaderLabels( list );

QTreeWidgetItem* pRoot = new QTreeWidgetItem((QTreeWidget*)0,QStringList(QStrin g("Can Messages")));
ui.treeList->addTopLevelItem( pRoot );
ui.treeList->expandItem( pRoot );
QList<QTreeWidgetItem *> items;

if( theApp->m_dcb.m_oMessages.count() > 0 )
{
QHashIterator<QString, QObject*> i( theApp->m_dcb.m_oMessages );
while( i.hasNext() )
{
i.next();
CDADcb::CMessage* pMess = (CDADcb::CMessage*)i.value();
if( pMess )
{
QTreeWidgetItem *newItem = new QTreeWidgetItem( pRoot );
newItem->setFlags( newItem->flags() | Qt::ItemIsUserCheckable );
//newItem->setCheckState(Qt::Unchecked);
newItem->setText( 0, pMess->name );
// store object with item for later retreivel
newItem->setData( 0, Qt::UserRole, QVariant::fromValue(pMess) );
ui.treeList->addTopLevelItem( newItem );
ui.treeList->expandItem( newItem );
for( int i = 0; i < pMess->m_oSignals.count(); i++ )
{
CDADcb::CSignal* pSig = (CDADcb::CSignal*)pMess->m_oSignals.at(i);
if( pSig )
{
QTreeWidgetItem *newSigItem = new QTreeWidgetItem( newItem );
newSigItem->setText( 0, QString( pSig->name ) );
ui.treeList->addTopLevelItem( newSigItem );
}
}

}
}
}

}


DAMessageTree::~DAMessageTree()
{

}


void DAMessageTree::mousePressEvent ( QMouseEvent * event )
{
}


Something I'm doing wrong:confused:

Thanks,
Steve

wysota
18th May 2007, 12:30
IMO you don't have to reimplement mouse handlers. Simply reimplement mimeData(), dropMimeData() and mimeTypes() in the tree widget and in the model that handles the table and make sure they are able to handle each other's data.

steg90
18th May 2007, 12:42
I'm thinking now of implementing dragEnterEvent, dragMoveEvent and dropEvent...As for mousePressEvent, this would be useful for getting the mouse position at the start of the drag for later use - I believe if the distance between the start mouse position and the position where the mouse button was pressed is larger than QApplication's recommended drag start distance we can avoid initiating a drag just because say the user's hand shakes...;)

Regards,
Steve