PDA

View Full Version : drag&drop from QGraphicsScene to QListWidget



hakiim35
7th July 2010, 09:37
Hi, I am developing a desktop applications which is inherited from QMainWindow and contains several widgets such as QDockWidgets, QListWidgets, QPushButtons, etc.
I have a QGraphicsView, which includes a QGraphicsScene containing a graphics item which inherits QGraphicsPixmapItem. The scene centers the main window. I want to drag and drop its content into a QListWidget. When dropped, I want to add the graphic item class' text member to the QListWidget. I have the following nonworking piece of code:
This first part is from my graphics item class:


void myGraphicsItem::mousePressEvent(QGraphicsSceneMous eEvent *event)
{
if (event->button() != Qt::LeftButton) {
event->ignore();
return;
}

setCursor(Qt::ClosedHandCursor);

update();

}

void myGraphicsItem::mouseMoveEvent(QGraphicsSceneMouse Event *event)
{
if (QLineF(event->screenPos(), event->buttonDownScreenPos(Qt::LeftButton))
.length() < QApplication::startDragDistance()) {
return;
}

QDrag* drag = new QDrag(event->widget());

QMimeData* mime = new QMimeData;

mime->setImageData(this->getImage());
mime->setText(this->path);

drag->setMimeData(mime);

drag->setPixmap(this->getPixmap().scaled(30, 40));

drag->exec(Qt::CopyAction);

setCursor(Qt::OpenHandCursor);

update();
}


void myGraphicsItem::mouseReleaseEvent(QGraphicsSceneMo useEvent *event)
{
setCursor(Qt::OpenHandCursor);

update();
}

The second part is from my widget class, which I have created from its ui file. The ui contains a QListWidget and two QPushButton.


class myWidget : public QWidget, public Ui::myWidgetClass
{
Q_OBJECT

public:
myWidget (QWidget *parent = 0);
~myWidget ();

protected:
void dragEnterEvent(QDragEnterEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
void dropEvent(QDropEvent *event);


myWidget ::myWidget (QWidget *parent)
: QWidget(parent)
{
setupUi(this);
setAcceptDrops(true);
}

void myWidget ::dropEvent( QDropEvent *event )
{
if (event->mimeData()->hasImage() && event->mimeData()->hasText())
{
event->acceptProposedAction();

QString absolutePath = ;
QListWidgetItem item(event->mimeData()->text());
myListWidget->addItem(&item);
}
else
event->ignore();
}
void ProjectWidget::dragEnterEvent( QDragEnterEvent *event )
{
event->setDropAction(Qt::CopyAction);
event->accept();
}

void ProjectWidget::dragMoveEvent( QDragMoveEvent *event )
{
event->setDropAction(Qt::CopyAction);
event->accept();
}

The problem is, the drag&drop operation never starts. When I double left click(no response when 1 left click) on the graphics item, the cursor changes to Qt::ClosedHandCursor state and keeps the same state while the mouse cursor is on the graphics item(say it is a 400x300 region).
The list widget item, which is the target widget for the drop action, is on the left most part of the app gui.
So the drag&drop always fails. The mouse is in Qt::ClosedHandCursor state when I double click on the image and drag anywhere outside while keeping the left button pressed, but never drops the item on the list widget when I release the button.
What can be the problem?
Thanks in advance.

aamer4yu
7th July 2010, 11:19
Does drag reach your list widget ?
Also you are creating QLIstWidgetItem on stack. So the item will be deleted as soon as you step out of dropEvent function.
Try creating on heap and check if it works.

hakiim35
7th July 2010, 12:02
Ok, I changed the item to pointer.
I think the drag doesnot work, item does not move at all.
The cursor keeps being in Qt::ClosedHandCursor state whenever I move the cursor into the scene, even if I dont click.

aamer4yu
7th July 2010, 12:20
Whats your drag mode in QGraphicsView class ?
seems you have set it to QGraphicsView::ScrollHandDrag

Also does your widget receive drag events ?

hakiim35
7th July 2010, 12:29
hmm I haven't set any drag mode in my GraphicsView class, I just worked on my graphics item class. What can I do in the graphics view class?

Which widget do you mean to receive drag events? graphics item or the target list widget class? how can I set to receive drag events? The only pieces of code is above as given..

Thanks.