PDA

View Full Version : Can"t connect signal and slot



Bennieboj
14th November 2011, 11:31
Hi everyone,

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





#ifndef DNDPIXMAP_H
#define DNDPIXMAP_H

#include <QPixmap>
#include <QObject>
#include <QGraphicsPixmapItem>
#include <QGraphicsSceneDragDropEvent>
class uitzicht;
class DnDPixmap:public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
// No Q_OBJECT macro, as QGraphicsPixmapItem is not derived from QObject!


public:
// Constructor gets a QPixmap which it will display.
DnDPixmap(const QPixmap &pixmap, uitzicht* a);
int oudeX;
int oudeY;
protected:
// Overloaded methods to enable dragging.
virtual void mousePressEvent(QGraphicsSceneMouseEvent* event);
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
// Overloaded methods for dropping.
virtual void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
virtual void dropEvent(QGraphicsSceneDragDropEvent *event);

signals:
void moveMade( int x, int y, int oudeX, int oudeY);

private:
// This member holds the copied QPixmap resulting from a drop.
QPixmap pix;
};

#endif // DNDPIXMAP_H





#include "dndpixmap.h"
#include <QMimeData>
#include <QList>
#include <QUrl>
#include <QMessageBox>

#include <QDrag>
#include <QApplication>
#include "uitzicht.h"

DnDPixmap::DnDPixmap(const QPixmap &pixmap, uitzicht* a)
: QGraphicsPixmapItem(pixmap)
{

// Make sure we allow drops.
setAcceptDrops(true);
connect(this, SIGNAL(moveMade( int, int, int, int)), a, SLOT(myslot( int, int, int, int)));

}

//more stuffs



my view:


#include "uitzicht.h"
#include "Spelbord.h"
#include <QGraphicsView>
#include <QGraphicsScene>
#include "dndpixmap.h"


void uitzicht::myslot(const int x, const int y, const int oudeX, const int oudeY){
//see if the move is possible
//Schaakstuk* a = bord->geefSchaakStuk(oudeX, oudeY);


//if yes update the model by changing the table
bord->verplaatsSchaakStuk(oudeX, oudeY, x, y);

//update the view, if needed
this->print();
}




#ifndef UITZICHT_H
#define UITZICHT_H
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QPixmap>
#include <QGraphicsItem>
#include <QPen>
#include <QBrush>
#include <QGraphicsScene>
#include "Spelbord.h"
#include "dndpixmap.h"

class uitzicht
{
public:
uitzicht(Spelbord* model, QGraphicsScene* scene);
void print();

public slots:
void myslot( int x, int y, int oudeX, int oudeY);
private:
DnDPixmap*bordview [Spelbord::MAX][Spelbord::MAX];
Spelbord* bord;
bool witAanZet;
QGraphicsScene* scene;
};

#endif // UITZICHT_H




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 (http://www.qtcentre.org/threads/9346-Signal-Slot-from-QGraphicsItem), though it's still not working

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

wysota
14th November 2011, 11:46
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.

Bennieboj
14th November 2011, 12:01
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:


class uitzicht:public QObject
{
Q_OBJECT
public:
uitzicht(Spelbord* model, QGraphicsScene* scene);
void print();


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!

Lykurg
14th November 2011, 12:24
Clean your project, rerun qmake and it should work.

Bennieboj
14th November 2011, 13:14
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?

wysota
15th November 2011, 22:47
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().

Bennieboj
16th November 2011, 09:49
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 (http://www.qtcentre.org/threads/45850-drag-and-drop), 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 :D thanks for the info!

/thread