Results 1 to 6 of 6

Thread: Problem using SIGNALS/SLOTS

  1. #1
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Problem using SIGNALS/SLOTS

    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.

    Qt Code:
    1. //Lobby.h
    2.  
    3. class Lobby : public QApplication
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. Lobby::Lobby(int argc, char *argv[]) : QApplication(argc, argv){}
    9. ~Lobby(){}
    10. void LoadMenus();
    11.  
    12. public slots:
    13. void StartGame(QString target);
    14.  
    15. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //Lobby.cpp
    2.  
    3. void Lobby::StartGame(QString target)
    4. {
    5. QProcess startGame;
    6. startGame.execute(target);
    7. }
    8.  
    9. void Lobby::LoadMenus()
    10. {
    11.  
    12. //iterate through button map, making the proper connections
    13. for(std::map<QString, LobbyButton *>::iterator buttonItor = buttonMap.begin(); buttonItor != buttonMap.end(); ++buttonItor)
    14. {
    15. if(buttonItor->second->GetType() == "screen")
    16. {
    17. QObject::connect(buttonItor->second, SIGNAL(clicked()), menuMap[buttonItor->second->GetTarget()], SLOT(show()));
    18. QObject::connect(buttonItor->second, SIGNAL(clicked()), menuMap[buttonItor->second->GetBoundMenu()], SLOT(hide()));
    19. }
    20. if(buttonItor->second->GetType() == "game")
    21. {
    22. QObject::connect(buttonItor->second, SIGNAL(clicked()), this, SLOT(StartGame(menuMap[buttonItor->second->GetTarget()])));
    23. QObject::connect(buttonItor->second, SIGNAL(clicked()), this, SLOT(quit()));
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem using SIGNALS/SLOTS

    , and the debug output reads "Object::connect: No such slot Lobby::StartGame(menuMap[buttonItor->second->GetTarget()])"
    Well:
    Qt Code:
    1. QObject::connect(buttonItor->second, SIGNAL(clicked()), this, SLOT(StartGame(menuMap[buttonItor->second->GetTarget()])));
    To copy to clipboard, switch view to plain text mode 
    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
    Last edited by marcel; 9th September 2007 at 22:50.

  3. #3
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Problem using SIGNALS/SLOTS

    Thanks alot for the advice! When I get a chance I'll try what you suggested.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem using SIGNALS/SLOTS

    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.

  5. #5
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Problem using SIGNALS/SLOTS

    Quote Originally Posted by momesana View Post
    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?

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem using SIGNALS/SLOTS

    Quote Originally Posted by JimDaniel View Post
    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

Similar Threads

  1. Graphics view display problem.
    By kiranraj in forum Qt Programming
    Replies: 3
    Last Post: 20th July 2007, 07:08
  2. [QMYSQL] connection problem
    By chaos_theory in forum Installation and Deployment
    Replies: 5
    Last Post: 2nd July 2007, 09:52
  3. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.