I've run into some trouble with signals and slots. I've successfully used them in my application so far but when I wanted to call a slot function with an enum things seemed to get a lot harder. After reading around the net trying to understand what difference an enum parameter make I came up with the following code I thought was gonna work.
I created a new header file just for my enum:
Arrows.h
#ifndef ARROWS_H
#define ARROWS_H
#include <QtCore>
enum Arrow
{
NO_ARROW,
DIAG_DOWN_RIGHT,
DIAG_DOWN_LEFT,
DIAG_UP_RIGHT
};
Q_DECLARE_METATYPE(Arrow)
#endif // ARROWS_H
#ifndef ARROWS_H
#define ARROWS_H
#include <QtCore>
enum Arrow
{
NO_ARROW,
DIAG_DOWN_RIGHT,
DIAG_DOWN_LEFT,
DIAG_UP_RIGHT
};
Q_DECLARE_METATYPE(Arrow)
#endif // ARROWS_H
To copy to clipboard, switch view to plain text mode
The header file which declares the slot function looks like this:
CrosswordArea.h
#ifndef CROSSWORDAREA_H
#define CROSSWORDAREA_H
#include "Arrows.h"
(...)
class CrosswordArea
: public QWidget{
Q_OBJECT
Q_ENUMS(Arrow)
(...)
public slots:
(...)
void setArrow(Arrow a);
(...)
};
#endif // CROSSWORDAREA_H
#ifndef CROSSWORDAREA_H
#define CROSSWORDAREA_H
#include "Arrows.h"
(...)
class CrosswordArea : public QWidget
{
Q_OBJECT
Q_ENUMS(Arrow)
(...)
public slots:
(...)
void setArrow(Arrow a);
(...)
};
#endif // CROSSWORDAREA_H
To copy to clipboard, switch view to plain text mode
Slot implementation (I'm not sure if I need qRegisterMetaType here)
CrosswordArea.cpp
#include "Arrows.h"
#include "CrosswordArea.h"
(...)
CrosswordArea
::CrosswordArea(QString str
){
qRegisterMetaType<Arrow>("Arrow");
(...)
}
(...)
void CrosswordArea::setArrow(Arrow a)
{
marked->setArrow(a);
}
#include "Arrows.h"
#include "CrosswordArea.h"
(...)
CrosswordArea::CrosswordArea(QString str)
{
qRegisterMetaType<Arrow>("Arrow");
(...)
}
(...)
void CrosswordArea::setArrow(Arrow a)
{
marked->setArrow(a);
}
To copy to clipboard, switch view to plain text mode
The slot is connected to a couple of buttons.
ButtonArea.cpp
#include "Arrows.h"
#include "CrosswordArea.h"
(...)
ButtonArea::ButtonArea(CrosswordArea *cwArea)
{
qRegisterMetaType<Arrow>("Arrow");
(...)
connect(letter2clue, SIGNAL(clicked()), cwArea, SLOT(setClue())); //works
connect(clue2letter, SIGNAL(clicked()), cwArea, SLOT(setLetter())); //works
connect(merge, SIGNAL(clicked()), cwArea, SLOT(merge())); //works
connect(yellowMark, SIGNAL(clicked()), cwArea, SLOT(yellowMark())); //works
connect(diagDRArrow, SIGNAL(clicked()), cwArea, SLOT(setArrow(Arrow))); //doesn't work
connect(diagDLArrow, SIGNAL(clicked()), cwArea, SLOT(setArrow(Arrow))); //doesn't work
connect(diagURArrow, SIGNAL(clicked()), cwArea, SLOT(setArrow(Arrow))); //doesn't work
(...)
}
#include "Arrows.h"
#include "CrosswordArea.h"
(...)
ButtonArea::ButtonArea(CrosswordArea *cwArea)
{
qRegisterMetaType<Arrow>("Arrow");
(...)
connect(letter2clue, SIGNAL(clicked()), cwArea, SLOT(setClue())); //works
connect(clue2letter, SIGNAL(clicked()), cwArea, SLOT(setLetter())); //works
connect(merge, SIGNAL(clicked()), cwArea, SLOT(merge())); //works
connect(yellowMark, SIGNAL(clicked()), cwArea, SLOT(yellowMark())); //works
connect(diagDRArrow, SIGNAL(clicked()), cwArea, SLOT(setArrow(Arrow))); //doesn't work
connect(diagDLArrow, SIGNAL(clicked()), cwArea, SLOT(setArrow(Arrow))); //doesn't work
connect(diagURArrow, SIGNAL(clicked()), cwArea, SLOT(setArrow(Arrow))); //doesn't work
(...)
}
To copy to clipboard, switch view to plain text mode
Running this doesn't cause an error but rather a warning.
QObject::connect: Incompatible sender
/receiver arguments
QPushButton::clicked() --> CrosswordArea
::setArrow(Arrow
) QObject::connect: Incompatible sender
/receiver arguments
QPushButton::clicked() --> CrosswordArea
::setArrow(Arrow
) QObject::connect: Incompatible sender
/receiver arguments
QPushButton::clicked() --> CrosswordArea
::setArrow(Arrow
)
QObject::connect: Incompatible sender/receiver arguments
QPushButton::clicked() --> CrosswordArea::setArrow(Arrow)
QObject::connect: Incompatible sender/receiver arguments
QPushButton::clicked() --> CrosswordArea::setArrow(Arrow)
QObject::connect: Incompatible sender/receiver arguments
QPushButton::clicked() --> CrosswordArea::setArrow(Arrow)
To copy to clipboard, switch view to plain text mode
I'm doing something terribly wrong. Before I start to guess solutions, messing up my code in hope to get lucky I thought I'd ask you guys first.
What have I done wrong and how can I make my slot function call correct?
Thanks!
Bookmarks