[Qt-interest] drag&drop from QGraphicsScene to QListWidget

hakiim the dream hakiim35 at hotmail.com
Wed Jul 7 09:41:19 CEST 2010


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 graphics item which inherits QGraphicsPixmapItem. The item 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(QGraphicsSceneMouseEvent *event){	if (event->button() != Qt::LeftButton) {		event->ignore();		return;	}
	setCursor(Qt::ClosedHandCursor);
        update();   }
void myGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *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(QGraphicsSceneMouseEvent *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);[/CODE]
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. 		 	   		  
_________________________________________________________________
Windows Live Hotmail: Arkadaşlarınız Facebook'taki güncellemelerinizi doğrudan Hotmail®'den alır.
http://www.microsoft.com/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:tr-tr:SI_SB_4:092009
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100707/44f7dd8b/attachment.html 


More information about the Qt-interest-old mailing list