You are not logged in.

1

Tuesday, January 31st 2012, 9:53pm

Object::connect No such slot...

Hi, im experiencing some problems with custom slots. I've googled the problem for a while, but I couldn't find any sufficient answer. The error I get after running the program is:

Quoted

Object::connect: No such slot hero::move(UP)
Object::connect: No such slot hero::move(DOWN)
Object::connect: No such slot hero::move(RIGHT)
Object::connect: No such slot hero::move(LEFT)


GameField.h

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef GAMEFIELD_H
#define GAMEFIELD_H

#define TILE_SIZE 18
#define TILE_BORDER_SIZE 0 //Every tile's border right and bot
#include <QWidget>
#include <vector>
using namespace std;

class GameField : public QWidget
{
	Q_OBJECT
	int mapWidth;
	int mapHeight;
	int score;
	void readMap(/*add parameter*/);
	void saveMap(/*add parameter*/);
	public:
		GameField(QWidget * parent = 0);
		~GameField();
		int getMapWidth() const { return mapWidth; };
		int getMapHeight() const { return mapHeight; };
		void incScore(int i) { score+=i; };
	protected:
		void paintEvent(QPaintEvent *event);
		vector< vector<char> > map;		
};
#endif



hero.h

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef HERO_H
#define HERO_H
#include "GameField.h"

enum direction { UP, DOWN, RIGHT, LEFT };

class hero : public GameField
{
	Q_OBJECT
	int lifeCount;
	int xPos;
	int yPos;
	QRect model;
	public:
		hero(int,int,QPainter*);
		QRect getModel() { return model; };	
		int findXPos();
		int findYPos();
	public slots:
		void move(direction);
};
#endif



Should i post more? Any clues?
PS. Honestly, I've no idea how to fix the code box.
PS2. K,nvm

This post has been edited 3 times, last edit by "yyyeey" (Jan 31st 2012, 10:02pm)


2

Tuesday, January 31st 2012, 10:38pm

you should show your connection code, but I can guess what you are doing wrong - you need to connect a SIGNATURE not a VARIABLE

e.g.
connect(ptr1, SIGNAL(func(direction)), ptr2, SLOT(func2(direction)));

NOT

connect(ptr1, SIGNAL(func(UP)), ptr2, SLOT(func2(UP)));

you tell qt to make a connection passing a 'direction' instance (ok, here it's only an enum value, not really a class instance)
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

This post has been edited 1 times, last edit by "Amleto" (Jan 31st 2012, 11:10pm)


3

Wednesday, February 1st 2012, 6:27am

Source code

1
2
3
4
	(void) new QShortcut(Qt::Key_Up, player, SLOT(move(UP)));
	(void) new QShortcut(Qt::Key_Down, player, SLOT(move(DOWN)));
	(void) new QShortcut(Qt::Key_Right, player, SLOT(move(RIGHT)));
	(void) new QShortcut(Qt::Key_Left, player, SLOT(move(LEFT)));


PS. I've changed it into no argument slots, each for every key, it doesn't throw errors anymore, but still the hero is idle. :P Anyway, its sort of a different problem, so I guess the main one is solved.

This post has been edited 1 times, last edit by "yyyeey" (Feb 1st 2012, 6:38am)


4

Wednesday, February 1st 2012, 7:26am

Ok, instead of making a new topic, I'll just post here. The new error msg:

Quoted


GameField.cpp: In member function ‘virtual void GameField::paintEvent(QPaintEvent*)’:
GameField.cpp:81:57: error: no matching function for call to ‘QShortcut::QShortcut(Qt::Key, hero*&, const char [10])’
GameField.cpp:81:57: note: candidates are:
/usr/include/qt4/QtGui/qshortcut.h:68:5: note: QShortcut::QShortcut(const QKeySequence&, QWidget*, const char*, const char*, Qt::ShortcutContext)
/usr/include/qt4/QtGui/qshortcut.h:68:5: note: no known conversion for argument 2 from ‘hero*’ to ‘QWidget*’
/usr/include/qt4/QtGui/qshortcut.h:67:14: note: QShortcut::QShortcut(QWidget*)
/usr/include/qt4/QtGui/qshortcut.h:67:14: note: candidate expects 1 argument, 3 provided
/usr/include/qt4/QtGui/qshortcut.h:57:20: note: QShortcut::QShortcut(const QShortcut&)
/usr/include/qt4/QtGui/qshortcut.h:57:20: note: candidate expects 1 argument, 3 provided


GameField.h

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#ifndef GAMEFIELD_H
#define GAMEFIELD_H

#define TILE_SIZE 18
#define TILE_BORDER_SIZE 0 //Every tile's border right and bot
#include <QWidget>
#include <vector>
#include "hero.h"
using namespace std;

class GameField : public QWidget
{
	Q_OBJECT
	int mapWidth;
	int mapHeight;
	int score;
	hero *player;
	void readMap(/*add parameter*/);
	void saveMap(/*add parameter*/);
	public:
		GameField(QWidget * parent = 0);
		~GameField();
		int getMapWidth() const { return mapWidth; };
		int getMapHeight() const { return mapHeight; };
		void incScore(int i) { score+=i; };
	protected:
		void paintEvent(QPaintEvent *event);
		vector< vector<char> > map;		
};
#endif


hero.h

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef HERO_H
#define HERO_H
#include <QWidget>
#include "GameField.h"

enum direction { UP, DOWN, RIGHT, LEFT };

class hero : public GameField
{
	Q_OBJECT
	int lifeCount;
	int xPos;
	int yPos;
	QRect model;
	public:
		hero(int,int,QPainter*);
		QRect getModel() { return model; };	
		int findXPos();
		int findYPos();
	public slots:
		void moveUp();
		void moveDown();
		void moveRight();
		void moveLeft();
};
#endif

connection part, reduced just to one key

Source code

1
	(void) new QShortcut(Qt::Key_Up, player, SLOT(moveUp()));

5

Wednesday, February 1st 2012, 10:25am

/usr/include/qt4/QtGui/qshortcut.h:68:5: note: no known conversion for argument 2 from ‘hero*’ to ‘QWidget*’

maybe you just havent included hero propery.

You have another problem, though - circular references! hero includes gamefield, gamefield includes hero. This is a problem. Gamefield should not include hero, it should only forward declare

e.g.

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
#include ...

class hero; // NOT #include hero.

class GameField : public QWidget
{
	Q_OBJECT
	int mapWidth;
	int mapHeight;
	int score;
	hero *player;
...
};


Although if hero can inherit from gamefield, which looks very bizarre just on the names, then there is NO NEED for the pointer member - you should be using virtual methods more appropriatley.

inhgeritance is normally only suitable if an 'is-a' relationship exists. can you say 'hero is-a game field' ? If not, then you probably shouldnt be using inheritance
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

6

Wednesday, February 1st 2012, 6:43pm

Well, I thought about has-a relationship, but it seems the code need some major fixes. And about multiple includings, I thought header guard would fix that, shouln't it?

7

Wednesday, February 1st 2012, 9:41pm

has-a does not imply inheritence.

header guards do not fix circular references
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

8

Thursday, February 2nd 2012, 8:25am

Ah, ok, that helped alot. Ty.

Similar threads