hakiim35
7th July 2010, 08: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.
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.