Results 1 to 3 of 3

Thread: Connecting slot for drop event in QGraphicsView

  1. #1
    Join Date
    Sep 2009
    Posts
    41
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Connecting slot for drop event in QGraphicsView

    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.

    Qt Code:
    1. QGraphicsScene *scene = new QGraphicsScene(this);
    2. QGraphicsView *view = new QGraphicsView(scene);
    3.  
    4. view->setAcceptDrops(true);
    5. connect(view, SIGNAL(dropEvent()), this, SLOT(myDropEvent()));
    6.  
    7. void MainWindow::myDropEvent()
    8. {
    9. QDebug("here");
    10. }
    To copy to clipboard, switch view to plain text mode 

    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Connecting slot for drop event in QGraphicsView

    QGraphicsScene::dropEvent() is not a signal! You have to reimplement:
    Qt Code:
    1. class YourClass
    2. {
    3. //...
    4. dropEvent ( QDropEvent * event );
    5. }
    6.  
    7.  
    8. YourClass::dropEvent ( QDropEvent * event )
    9. {
    10. QDebug("here");
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Lykurg for this useful post:

    Ishmael (22nd October 2009)

  4. #3
    Join Date
    Sep 2009
    Posts
    41
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Connecting slot for drop event in QGraphicsView

    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.)


    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QMainWindow>
    3. #include <QGraphicsView>
    4. #include <QDropEvent>
    5.  
    6. class MyGraphicsView : public QGraphicsView
    7. {
    8. //Q_OBJECT // Do I need Q_OBJECT?
    9.  
    10. public:
    11. MyGraphicsView(QGraphicsScene *scene);
    12.  
    13. protected:
    14. //void dropEvent(QGraphicsSceneDragDropEvent *event);
    15. void dropEvent(QDropEvent *event);
    16. };
    17.  
    18.  
    19. MyGraphicsView::MyGraphicsView(QGraphicsScene *scene)
    20. {
    21. this->setScene(scene);
    22. }
    23.  
    24.  
    25. void MyGraphicsView::dropEvent(QDropEvent *event)
    26. {
    27. qDebug("here");
    28. }
    29.  
    30.  
    31. int main(int argc, char *argv[])
    32. {
    33. QApplication app(argc, argv);
    34. QMainWindow window;
    35.  
    36. MyGraphicsView *view = new MyGraphicsView(scene);
    37. view->setAcceptDrops(true);
    38.  
    39. window.setCentralWidget(view);
    40. window.resize(640, 480);
    41. window.setAcceptDrops(true);
    42. window.show();
    43.  
    44. return app.exec();
    45. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Ishmael; 25th October 2009 at 01:40.

Similar Threads

  1. Connecting signal to custom slot?
    By dbrmik in forum Qt Tools
    Replies: 2
    Last Post: 30th April 2009, 09:28
  2. Replies: 4
    Last Post: 19th February 2009, 11:10
  3. Connecting to a slot not within an Obejct
    By Matze-o in forum Qt Programming
    Replies: 2
    Last Post: 10th November 2008, 14:02
  4. Replies: 2
    Last Post: 24th March 2008, 16:59
  5. signal and slot across threads having event loop
    By travis in forum Qt Programming
    Replies: 6
    Last Post: 5th November 2007, 23:56

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.