PDA

View Full Version : Problem using SIGNALS/SLOTS



JimDaniel
9th September 2007, 22:16
Hi, maybe you guys can help me out.

I'm new to QT (and C++) and am trying to create a SIGNAL/SLOT event which closes the current QApp and starts another .exe based on a QPushButton click() signal. But its not working correctly. I believe I have more than one error.

With the code I put below, I get an "Access violation reading location 0x00000000 error" when compiling, and the debug output reads "Object::connect: No such slot Lobby::StartGame(menuMap[buttonItor->second->GetTarget()])"

I've put what I think is the most relevant code below. Let me know if you need more info.




//Lobby.h

class Lobby : public QApplication
{
Q_OBJECT

public:
Lobby::Lobby(int argc, char *argv[]) : QApplication(argc, argv){}
~Lobby(){}
void LoadMenus();

public slots:
void StartGame(QString target);

};




//Lobby.cpp

void Lobby::StartGame(QString target)
{
QProcess startGame;
startGame.execute(target);
}

void Lobby::LoadMenus()
{

//iterate through button map, making the proper connections
for(std::map<QString, LobbyButton *>::iterator buttonItor = buttonMap.begin(); buttonItor != buttonMap.end(); ++buttonItor)
{
if(buttonItor->second->GetType() == "screen")
{
QObject::connect(buttonItor->second, SIGNAL(clicked()), menuMap[buttonItor->second->GetTarget()], SLOT(show()));
QObject::connect(buttonItor->second, SIGNAL(clicked()), menuMap[buttonItor->second->GetBoundMenu()], SLOT(hide()));
}
if(buttonItor->second->GetType() == "game")
{
QObject::connect(buttonItor->second, SIGNAL(clicked()), this, SLOT(StartGame(menuMap[buttonItor->second->GetTarget()])));
QObject::connect(buttonItor->second, SIGNAL(clicked()), this, SLOT(quit()));
}
}


I'd appreciate any help you can give me.

EDIT: I posted this in another forum and someone mentioned that if my SLOT method takes a parameter then my SIGNAL must emit that parameter. Do you think that has anything to do with my current problem?

marcel
9th September 2007, 22:27
, and the debug output reads "Object::connect: No such slot Lobby::StartGame(menuMap[buttonItor->second->GetTarget()])"

Well:


QObject::connect(buttonItor->second, SIGNAL(clicked()), this, SLOT(StartGame(menuMap[buttonItor->second->GetTarget()])));

That code is not correct. I am assuming you want the slot to know what button was clicked.
For this purpose you can either use a QSignalMapper or, remove the parameter from the slot(also from the declaration and definition) and inside the slot look at the QObject::sender(). You will have to cast sender to object type that sends the signal and compare it too all the objects in the list( a pointer comparison is OK). If you find a match, then you got your sender.
However, QSignalMapper is the better choice.



I posted this in another forum and someone mentioned that if my SLOT method takes a parameter then my SIGNAL must emit that parameter. Do you think that has anything to do with my current problem?
Yes that is correct.
A SINGAL can have more parameters than the corresponding SLOT, but never less. If it has more, then the slot will ignore the extra parameters.

Correct these errors and see what's you get next.



With the code I put below, I get an "Access violation reading location 0x00000000 error" when compiling, and the debug output reads "Object::connect: No such slot Lobby::StartGame(menuMap[buttonItor->second->GetTarget()])"
I doubt you get an access violation when compiling... Or the connect warning. Those are run time warning/errors.

If you get the access violation while pressing the button then post the code for the corresponding slot.
Otherwise please state when exactly you get an access violation.
Anyway, make sure that you have valid LobbyButton pointers in the buttonMap.

Regards

JimDaniel
9th September 2007, 23:01
Thanks alot for the advice! When I get a chance I'll try what you suggested.

momesana
10th September 2007, 02:07
That's about the first time I see an application being derived from QApplication. I wonder if you really need to subclass QApplication for your purposes. Maybe you should consider deriving from QWidget, QMainWindow or QDialog instead.

JimDaniel
10th September 2007, 03:13
That's about the first time I see an application being derived from QApplication. I wonder if you really need to subclass QApplication for your purposes. Maybe you should consider deriving from QWidget, QMainWindow or QDialog instead.

Well, I'm not sure either. This design was suggested to me by my boss, who has much more experience than me. I think its mainly for clarity, as this is going to be a casino game with several applications starting and quitting. But I will keep your suggestion in mind...do you think it would help solve my current problem?

marcel
10th September 2007, 04:59
Well, I'm not sure either. This design was suggested to me by my boss, who has much more experience than me. I think its mainly for clarity, as this is going to be a casino game with several applications starting and quitting. But I will keep your suggestion in mind...do you think it would help solve my current problem?

No, as I told you, your problems are with the signals and slots, not with the way you implemented your application.

Anyway, momesana was right. You usually subclass QApplication of you really need to override some of its functionality.

For interfaces, main windows, widgets and/or dialogs are used.

Regards