Results 1 to 7 of 7

Thread: Can"t connect signal and slot

  1. #1
    Join Date
    Nov 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Can"t connect signal and slot

    Hi everyone,

    I have a problem connecting a signal to a slot.
    I'm making a chessgame using QGraphicsView and an array of DnDPixmaps:


    Qt Code:
    1. #ifndef DNDPIXMAP_H
    2. #define DNDPIXMAP_H
    3.  
    4. #include <QPixmap>
    5. #include <QObject>
    6. #include <QGraphicsPixmapItem>
    7. #include <QGraphicsSceneDragDropEvent>
    8. class uitzicht;
    9. class DnDPixmap:public QObject, public QGraphicsPixmapItem
    10. {
    11. Q_OBJECT
    12. // No Q_OBJECT macro, as QGraphicsPixmapItem is not derived from QObject!
    13.  
    14.  
    15. public:
    16. // Constructor gets a QPixmap which it will display.
    17. DnDPixmap(const QPixmap &pixmap, uitzicht* a);
    18. int oudeX;
    19. int oudeY;
    20. protected:
    21. // Overloaded methods to enable dragging.
    22. virtual void mousePressEvent(QGraphicsSceneMouseEvent* event);
    23. virtual void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
    24. // Overloaded methods for dropping.
    25. virtual void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
    26. virtual void dropEvent(QGraphicsSceneDragDropEvent *event);
    27.  
    28. signals:
    29. void moveMade( int x, int y, int oudeX, int oudeY);
    30.  
    31. private:
    32. // This member holds the copied QPixmap resulting from a drop.
    33. QPixmap pix;
    34. };
    35.  
    36. #endif // DNDPIXMAP_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "dndpixmap.h"
    2. #include <QMimeData>
    3. #include <QList>
    4. #include <QUrl>
    5. #include <QMessageBox>
    6.  
    7. #include <QDrag>
    8. #include <QApplication>
    9. #include "uitzicht.h"
    10.  
    11. DnDPixmap::DnDPixmap(const QPixmap &pixmap, uitzicht* a)
    12. {
    13.  
    14. // Make sure we allow drops.
    15. setAcceptDrops(true);
    16. connect(this, SIGNAL(moveMade( int, int, int, int)), a, SLOT(myslot( int, int, int, int)));
    17.  
    18. }
    19.  
    20. //more stuffs
    To copy to clipboard, switch view to plain text mode 

    my view:
    Qt Code:
    1. #include "uitzicht.h"
    2. #include "Spelbord.h"
    3. #include <QGraphicsView>
    4. #include <QGraphicsScene>
    5. #include "dndpixmap.h"
    6.  
    7.  
    8. void uitzicht::myslot(const int x, const int y, const int oudeX, const int oudeY){
    9. //see if the move is possible
    10. //Schaakstuk* a = bord->geefSchaakStuk(oudeX, oudeY);
    11.  
    12.  
    13. //if yes update the model by changing the table
    14. bord->verplaatsSchaakStuk(oudeX, oudeY, x, y);
    15.  
    16. //update the view, if needed
    17. this->print();
    18. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef UITZICHT_H
    2. #define UITZICHT_H
    3. #include <QGraphicsView>
    4. #include <QGraphicsScene>
    5. #include <QPixmap>
    6. #include <QGraphicsItem>
    7. #include <QPen>
    8. #include <QBrush>
    9. #include <QGraphicsScene>
    10. #include "Spelbord.h"
    11. #include "dndpixmap.h"
    12.  
    13. class uitzicht
    14. {
    15. public:
    16. uitzicht(Spelbord* model, QGraphicsScene* scene);
    17. void print();
    18.  
    19. public slots:
    20. void myslot( int x, int y, int oudeX, int oudeY);
    21. private:
    22. DnDPixmap*bordview [Spelbord::MAX][Spelbord::MAX];
    23. Spelbord* bord;
    24. bool witAanZet;
    25. };
    26.  
    27. #endif // UITZICHT_H
    To copy to clipboard, switch view to plain text mode 

    In the constructor of DnDPixmap, I want to connect the signal 'moveMade' to the slot of the view 'mySlot', but I get this error:
    dndpixmap.cpp:17: error: no matching function for call to 'DnDPixmap::connect(DnDPixmap* const, const char*, uitzicht*&, const char*)'

    I already derived DnDPixmap from QObject as I learned from another thread outhere, though it's still not working

    Can someone help me with this please?
    Thanks in advance!
    Bennieboj

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can"t connect signal and slot

    Slots can only be in classes inheriting QObject. Your uitzicht class doesn't inherit QObject and it doesn't have the Q_OBJECT macro required for slots to work.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can"t connect signal and slot

    Quote Originally Posted by wysota View Post
    Slots can only be in classes inheriting QObject. Your uitzicht class doesn't inherit QObject and it doesn't have the Q_OBJECT macro required for slots to work.
    Ok, I inherited 'uitzicht' from QObject, now I have this code:
    Qt Code:
    1. class uitzicht:public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. uitzicht(Spelbord* model, QGraphicsScene* scene);
    6. void print();
    To copy to clipboard, switch view to plain text mode 

    Thanks for your time
    but now I get these errors:
    dndpixmap.cpp:12: error: undefined reference to `vtable for DnDPixmap' (4 times),
    still the error from my first post and the same error as above but then for uitzicht.cpp (about undefined reference)

    How do I fix this please?
    Thanks in advance!

  4. #4
    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: Can"t connect signal and slot

    Clean your project, rerun qmake and it should work.

  5. #5
    Join Date
    Nov 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can"t connect signal and slot

    It works, Thanks

    May I ask another question?
    I now have my chesspieces, on my view and in the array of dndpixmaps.
    But if I drag a chesspiece, how do I get the mosttopleft position of the pixmap
    I know my chesspieces are 50 high and wide, so to get the position in the table I just have to divide the position onscreen by 50.
    I simply can't get the position...
    Can you help me with this please?

    EDIT: nevermind, I already got it, I used: event->lastScenePos().rx()/50;
    The problem is now, I can get those x and y values, (I store them in the two ints in the DnDPixmap.)

    I still have those values in the mouseMoveEvent and the dragEnterEvent, but in the dropEvent function they're gone...
    Can someone explain this to me please?
    Last edited by Bennieboj; 14th November 2011 at 14:04.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can"t connect signal and slot

    Are you sure you should be using dragEnterEvent and dropEvent? If you just want to move items around, do it with mouseMoveEvent and mouseReleaseEvent or even set the item flag ItemIsMovable and you won't have to do anything, maybe apart controling the item position with QGraphicsItem::itemChange().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Nov 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can"t connect signal and slot

    Quote Originally Posted by wysota View Post
    Are you sure you should be using dragEnterEvent and dropEvent? If you just want to move items around, do it with mouseMoveEvent and mouseReleaseEvent or even set the item flag ItemIsMovable and you won't have to do anything, maybe apart controling the item position with QGraphicsItem::itemChange().
    I made a new topic, just so you know it's the same problem.
    Thanks for the hint, I'll let you know if there are any problems =)

    EDIT: I did it thanks for the info!

    /thread
    Last edited by Bennieboj; 16th November 2011 at 10:24.

Similar Threads

  1. Replies: 2
    Last Post: 15th September 2010, 00:54
  2. Can't connect a signal to a slot
    By cejohnsonsr in forum Newbie
    Replies: 5
    Last Post: 26th August 2010, 20:42
  3. debugging "signal does not reach slot" in a template class
    By Daniel Dekkers in forum Qt Programming
    Replies: 1
    Last Post: 3rd April 2010, 16:03
  4. Replies: 1
    Last Post: 23rd August 2008, 22:09
  5. QObject::connect says "no such signal"
    By MistaPain in forum Qt Programming
    Replies: 3
    Last Post: 31st October 2006, 05:40

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
  •  
Qt is a trademark of The Qt Company.