PDA

View Full Version : slots and signals don't seem to be recognized



Jessehk
31st July 2007, 19:08
Hello all. :)

I'm very new to Qt but I'm extremely impressed with several aspects of it, especially the documentation.
I'm not an expert programmer (more of a hobbyist), so please feel very free to correct me.

I'm writing a tic-tac-toe game. I've got the game engine itself working, and I'm trying to wrap it up in a nice GUI.

One aspect of the game is going to be a grid of buttons representing the board. When a button is clicked, I want a signal to be emitted witht he coordinates of the button.

Here's what I've got:


#ifndef UISPOT_HPP
#define UISPOT_HPP

#include <QPushButton>
#include <QIcon>

#include "spot.hpp"

namespace ttt {
class UISpot : public QPushButton {
Q_OBJECT
private:
Spot spot_;
slots:
void buttonClicked();
signals:
void clickedAt( const Coord &coord );
public:
UISpot( const Spot &spot, QWidget *parent ) :
QPushButton( parent ),
spot_( spot ) {
connect( this, SIGNAL( clicked() ),
this, SLOT( buttonClicked() ) );
}
};
}

#endif // UISPOT_HPP


And uiSpot.cpp:



#include "uiSpot.hpp"

namespace ttt {
void UISpot::buttonClicked() {
emit clickedAt( spot_.coord() );
}
}


The Spot interface is irrelevent, so I didn't post it. Now, when I try to compile this, I get the following error messages:



$ qmake -project
$ qmake
$ make
...
<SNIP>
...
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -I. -o uiSpot.o uiSpot.cpp
uiSpot.hpp:15: error: expected primary-expression before ‘void’
uiSpot.hpp:15: error: ISO C++ forbids declaration of ‘type name’ with no type
uiSpot.hpp:15: error: expected ‘;’ before ‘void’
uiSpot.cpp:4: error: no ‘void ttt::UISpot::buttonClicked()’ member function declared in class ‘ttt::UISpot’
make: *** [uiSpot.o] Error 1


At line 15 is where I declare the slots and signals. What am I doing wrong?

Any help would be greatly appreciated. :)

Michiel
31st July 2007, 19:27
I've never seen 'slots:' in a class without a visibility specification. It might not be the error, but it is worth a try. In your case, I guess that would be 'private slots:'.

Also, you might be interested in auto-connect slots. It's a Qt slot naming convention (on_<object>_<signal>). If you call your slot 'on_this_clicked()', you don't need to explicitly connect.

Jessehk
31st July 2007, 19:50
I've never seen 'slots:' in a class without a visibility specification. It might not be the error, but it is worth a try. In your case, I guess that would be 'private slots:'.

Also, you might be interested in auto-connect slots. It's a Qt slot naming convention (on_<object>_<signal>). If you call your slot 'on_this_clicked()', you don't need to explicitly connect.

Thanks Michiel. The private slots did it. :)