Results 1 to 5 of 5

Thread: QObject::connect: No such slot

  1. #1
    Join Date
    Apr 2021
    Posts
    6
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default QObject::connect: No such slot

    Hello,

    I creating small app for my university and I stucked at one problem - no such slot.

    Maybe now I put some code and explain it later.



    Game. h

    Qt Code:
    1. class Game: public QGraphicsView{
    2. Q_OBJECT
    3. public slots:
    4. void displayMainMenu();
    5. void showHelp(Button &start,Button &score,Button &help,Button &quit);
    6. };
    To copy to clipboard, switch view to plain text mode 



    Game.cpp

    Qt Code:
    1. void Game::displayMainMenu() {
    2. Button* playButton = new Button(QString("../Sources/Pictures/Menu/start-inactive.png"),
    3. QString("../Sources/Pictures/Menu/start-active.png"));
    4. connect(playButton,SIGNAL(clicked()), this, SLOT(start()));
    5.  
    6.  
    7. Button* scoresButton = new Button(QString("../Sources/Pictures/Menu/scores-inactive.png"),
    8. QString("../Sources/Pictures/Menu/scores-active.png"));
    9. connect(scoresButton,SIGNAL(clicked()), this, SLOT(showScores()));
    10.  
    11.  
    12. Button* helpButton = new Button(QString("../Sources/Pictures/Menu/help-inactive.png"),
    13. QString("../Sources/Pictures/Menu/help-active.png"));
    14. connect(helpButton,SIGNAL(clicked()), this, SLOT(showHelp(playButton, scoresButton, helpButton, quitButton)));
    15.  
    16.  
    17. Button* quitButton = new Button(QString("../Sources/Pictures/Menu/quit-inactive.png"),
    18. QString("../Sources/Pictures/Menu/quit-active.png"));
    19. connect(quitButton,SIGNAL(clicked()), this, SLOT(close()));
    20.  
    21. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void Game::showHelp(Button &play, Button &scores, Button &help, Button &quit) {
    2. /* some stuff here with these buttons */
    3. }
    To copy to clipboard, switch view to plain text mode 


    Above I show my code and now I will explain you what I want to do, so I want send these buttons in parameters and use them by my idea, but when I compile my program I getting this error message:
    QObject::connect: No such slot Game::showHelp(playButton, scoresButton, helpButton, quitButton)
    Button class is made by myself and with it class isn't any problem I guess.

    I have tried all things which I found on the internet.
    Of course I clean/rebuild my project before. I had Q_OBJECT macro in game.h too.

    PS. I delete some unnecessary code but if you want I can update this any time

    Any help would be appreciated

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,968
    Thanks
    277
    Thanked 808 Times in 797 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QObject::connect: No such slot

    Look again at the documentation and examples for QObject::connect().

    - First, you do not pass actual arguments to the SIGNAL or SLOT macros when you call connect(), you simply provide the declaration or signature of the signal or slot. So if your SLOT declaration was valid (and it isn't - see the next point) the connect() statement would be:

    Qt Code:
    1. connect(helpButton,SIGNAL(clicked()), this, SLOT(showHelp(Button &, Button &, Button &, Button &)));
    To copy to clipboard, switch view to plain text mode 

    But the above code is STILL WRONG, because

    - Second, you cannot add more parameters to a slot definition than there are in the signal connected to it. So a signal "clicked()" with no parameters can only be connected to a slot with no parameters.

    Qt Code:
    1. connect(helpButton,SIGNAL(clicked()), this, SLOT(someSlot()));
    To copy to clipboard, switch view to plain text mode 

    So, your "showHelp()" slot has to be defined with no parameters if you want to connect it to a clicked() signal with no parameters.

    - Third, your displayMainMenu() slot looks as if you intend to call it every time the main menu is to be displayed. If that is your intention, then not only is it misnamed based on the code it contains, but I hope you realize that each time it is called it will create a complete new set of buttons and a complete new set of signal / slot connections for those buttons. And because you haven't saved those Button instances as member variables in your Game class, those pointers become zombies, inaccessible from the rest of your program. So there won't be any "Help" button visible to click, whether it is connected to a slot or not.

    This type of setup code should be executed once, usually in a class constructor. If you intend for the Button instances to be part of the UI for your Game class, then they need to either be assigned as member variables of your class or created as children of your Game class instance and placed into a layout managed by the class.

    - And finally, even if your use of SLOT() had been allowed, your declaration of the slot:
    Qt Code:
    1. void showHelp(Button &start,Button &score,Button &help,Button &quit);
    To copy to clipboard, switch view to plain text mode 

    does not match your use of it in the connect() statement:

    Qt Code:
    1. connect(helpButton,SIGNAL(clicked()), this, SLOT(showHelp(playButton, scoresButton, helpButton, quitButton)));
    To copy to clipboard, switch view to plain text mode 

    since "playButton", "scoresButton", "helpButton", and "quitButton" are Button * pointers, not Button & references.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    nemaider (20th April 2021)

  4. #3
    Join Date
    Apr 2021
    Posts
    6
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QObject::connect: No such slot

    After your post I realized what mistakes I did. At first I will try assign as member variables these buttons and later remake showHelp function.

    Thanks for your reply.

    When I fix all errors in my project I will put solution here.

  5. #4
    Join Date
    Apr 2021
    Posts
    6
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QObject::connect: No such slot

    Okey, now I've fixed all my problems which I came in this thread. Thank you d_stranz for your suggestions about make these buttons as member variables in Game class and connecting signals with slots. It work well.

    For future users I can give one advice - don't connect no arguments signals with no argument slots. It was my main problem in this example.

  6. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,968
    Thanks
    277
    Thanked 808 Times in 797 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QObject::connect: No such slot

    For future users I can give one advice - don't connect no arguments signals with no argument slots. It was my main problem in this example.
    This is not good advice at all. It is always appropriate to connect a signal with no arguments to a slot with no arguments. In fact, it is the only way such a signal can be connected. QPushButton::clicked() and QAction::triggered() are two very, very common cases of such signals*, and connecting them to no-argument slots is the only way to handle those signals.

    I think you still have a basic misunderstanding of signals and slots, otherwise you would not make such a statement.

    *Edit: QAbstractButton::clicked() and QAction::triggered() are actually overloaded methods - they come in two forms, one with a bool "checked" argument and another with the bool argument defaulted to false. The signals are usually connected to no-argument slots, which means the bool argument is ignored.
    Last edited by d_stranz; 21st April 2021 at 16:29.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 6
    Last Post: 11th January 2021, 00:17
  2. Replies: 0
    Last Post: 9th January 2021, 13:07
  3. Problem with the Slot in QObject::connect(...)
    By d'Matthias in forum Qt Programming
    Replies: 14
    Last Post: 4th June 2011, 07:46
  4. QObject::connect: No such slot !?!
    By Mystical Groovy in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 18:31
  5. Replies: 2
    Last Post: 24th March 2008, 16:59

Tags for this Thread

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.