PDA

View Full Version : Connecting slot for drop event in QGraphicsView



Ishmael
22nd October 2009, 03:35
How do I connect a dropEvent in a QGraphicsView widget to a slot? Here's a snippet of what I've got so far. This compiles just fine, but it doesn't recognize any drop event.


QGraphicsScene *scene = new QGraphicsScene(this);
QGraphicsView *view = new QGraphicsView(scene);

view->setAcceptDrops(true);
connect(view, SIGNAL(dropEvent()), this, SLOT(myDropEvent()));

void MainWindow::myDropEvent()
{
QDebug("here");
}

What I'm trying to do is drag and drop items from outside my application into an empty QGraphicsView (for instance, an icon from the Desktop). Can this be done?

Thanks!

Lykurg
22nd October 2009, 08:39
QGraphicsScene::dropEvent() is not a signal! You have to reimplement:

class YourClass
{
//...
dropEvent ( QDropEvent * event );
}


YourClass::dropEvent ( QDropEvent * event )
{
QDebug("here");

}

Ishmael
24th October 2009, 23:45
Thanks for your response. Unfortunately, still no luck. I thought making my own class would do the trick.

Here's a sample version of my code. For some reason, it still does not respond to Drop events. I am trying to drag an icon from my desktop onto my GraphicsView window.

Any help you can give me would be greatly appreciated!

Also, I have two questions:
1) What is the difference between QDropEvent and QGraphicsSceneDragDropEvent?
2) As I understand it, I only need to include Q_OBJECT if I want to use signals, is that correct?

(For some reason, when I uncomment Q_OBJECT in my code, I get a bunch of "undefined reference to `vtable for MyGraphicsView'" errors.)



#include <QtGui/QApplication>
#include <QMainWindow>
#include <QGraphicsView>
#include <QDropEvent>

class MyGraphicsView : public QGraphicsView
{
//Q_OBJECT // Do I need Q_OBJECT?

public:
MyGraphicsView(QGraphicsScene *scene);

protected:
//void dropEvent(QGraphicsSceneDragDropEvent *event);
void dropEvent(QDropEvent *event);
};


MyGraphicsView::MyGraphicsView(QGraphicsScene *scene)
{
this->setScene(scene);
}


void MyGraphicsView::dropEvent(QDropEvent *event)
{
qDebug("here");
}


int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow window;

QGraphicsScene *scene = new QGraphicsScene;
MyGraphicsView *view = new MyGraphicsView(scene);
view->setAcceptDrops(true);

window.setCentralWidget(view);
window.resize(640, 480);
window.setAcceptDrops(true);
window.show();

return app.exec();
}