PDA

View Full Version : to use drag & drop on QListWidget



giorgik
9th October 2012, 19:22
Hello everyone,
I'm using Qt 4.8.3 and Qt Creator 2.5.2 (maybe there is a bug because I do not compile in the resource file *. QRC, I have to do it manually from the command line dos) in Windows XP.
I created a class ListaElementi by subclassing QListWidget in which the figures are listed in svg format. An example to understand is the following code:


QListWidgetItem *item;
item = new QListWidgetItem(QIcon("ostacolo.svg"), "Ostacolo tipo 1", this);
insertItem(1, item);

So far so good, I show you a list of figures. The project continues dragging and dropping onto my scene class (derived from QScene) one of the figures taken from ListaElementi. Even so good.
My problem is to make sure that when I select the figure from ListaElementi and drag near to the mouse cursor would like to see the figure selected in such a way so that I can see her standing before release (drop) on the scene.
Can you tell me how to write the code?
To simplify things I reproduce below part of the code of the drag ListaElementi:


void ListaElementi::mousePressEvent(QMouseEvent *event)
{
// si evidenzia l'elemento selezionato
QListWidget::mousePressEvent(event);

//qDebug() << "ListaElementi::mousePressEvent";

// creo uno stream di dato contenente il nome del
// item selezionato per usarlo nel drag&drop
if(currentItem())
{
QByteArray dato;
QDataStream stream(&dato, QIODevice::WriteOnly);
stream << currentItem()->text();
QMimeData *mime = new QMimeData;
mime->setData("elementoListaSelez", dato);

QDrag *trascina = new QDrag(this);
trascina->setMimeData(mime);
trascina->setObjectName(currentItem()->text());
trascina->setHotSpot(event->pos());
trascina->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction);
}
}

void ListaElementi::dragEnterEvent(QDragEnterEvent *event)
{
//qDebug() << "ListaElementi::dragEnterEvent";
QListWidget::dragEnterEvent(event);

// controlla che il dato da passare è un "elementoListaSelez"
if(event->mimeData()->hasFormat("elementoListaSelez"))
{
// il dato non è preso dalla ListaElementi
event->acceptProposedAction();
}
else
{
// l'elemnto non è del tipo "elementoListaSelez"
event->ignore();
}
}

void ListaElementi::dragMoveEvent(QDragMoveEvent *e)
{
//qDebug() << "ListaElementi::dragMoveEvent";
QListWidget::dragMoveEvent(e);

// controlla che il dato da passare è un "elementoListaSelez"
if(e->mimeData()->hasFormat("elementoListaSelez"))
{
// il dato non è preso dalla ListaElementi
e->acceptProposedAction();
}
else
{
// l'elemnto non è del tipo "elementoListaSelez"
e->ignore();
}
}

giorgik
19th October 2012, 18:00
Ok :) all resolved, to see Qt italia forum: http://www.qt-italia.org/forum/viewtopic.php?f=6&t=829