Results 1 to 11 of 11

Thread: No Such Slot

  1. #1
    Join Date
    Feb 2016
    Posts
    7
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default No Such Slot

    I have a dialog window with a slider. I'm trying to make it so that a QLabel will change as the slider changes, but I'm having problems with the slot.

    The error message is:
    QMetaObject::connectSlotsByName: No matching signal for on_sizeslider_valuechanged(int)
    QObject::connect: No such slot Dialog:: on_sizeslider_valuechanged(int value) in ..\GUINew\dialog.cpp:104
    QObject::connect: (receiver name: 'Dialog')
    My project is quite long, I will post the whole header file but only the relevant part of the .cpp file.

    Dialog.h
    Qt Code:
    1. #ifndef DIALOG_H
    2. #define DIALOG_H
    3.  
    4. #include <QDialog>
    5. #include <QGridLayout>
    6. #include <QHBoxLayout>
    7. #include <QLabel>
    8.  
    9. namespace Ui {
    10. class Dialog;
    11. }
    12.  
    13. class Dialog : public QDialog
    14. {
    15. Q_OBJECT
    16. QGridLayout *gridlayout;
    17. QHBoxLayout *usernamelayout;
    18. QHBoxLayout *sizelayout;
    19. QHBoxLayout *skilllayout;
    20. QHBoxLayout *texlayout;
    21.  
    22. QLabel *sizechoice;
    23.  
    24. public:
    25. explicit Dialog(QWidget *parent = 0);
    26. ~Dialog();
    27.  
    28. public slots:
    29. void on_sizeslider_valuechanged(int value);
    30.  
    31. private:
    32. Ui::Dialog *ui;
    33. };
    34.  
    35. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 

    Dialog.cpp
    Qt Code:
    1. QObject::connect(sizeslider, SIGNAL(valueChanged(int)), this, SLOT(on_sizeslider_valuechanged(int value)));
    2.  
    3. }
    4.  
    5. void Dialog::on_sizeslider_valuechanged(int value){
    6.  
    7. if (value == 1) {
    8. Dialog::sizechoice->setText("Small");
    9. }
    10. if (value == 2) {
    11. Dialog::sizechoice->setText("Medium");
    12. }
    13. if (value == 3) {
    14. Dialog::sizechoice->setText("Large");
    15. }
    16. if (value == 4) {
    17. Dialog::sizechoice->setText("XLarge");
    18. }
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 

    I've tried clean/rebuild/qmake.

    I'm not sure what's wrong with it. The function is clearly there, and it's defined under 'public slots' in the header, but the program seems to think Dialog doesn't have a slot of that name.

    Any help would be appreciated.

  2. #2
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: No Such Slot

    Hello,

    the signal from your sizeslider has the name "valueChanged(int)". Using the connection by name
    Qt Code:
    1. void on_sizeslider_valuechanged(int value);
    To copy to clipboard, switch view to plain text mode 
    Qt tries to connect the slot on_sizeslider_valuechanged(int) to a signal valuechanged(int) of sizeslider. The slider signal is valueChanged(int) (capital C), so no connection can be established. If you want to go with connection by name you should change your slot name to on_sizeslider_valueChanged(int).

    Applying this change will run you into the next issue triggered by the (changed) line
    Qt Code:
    1. QObject::connect(sizeslider, SIGNAL(valueChanged(int)), this, SLOT(on_sizeslider_valueChanged(int)));
    To copy to clipboard, switch view to plain text mode 
    Note the 2 changes in this line. First using on_sizeslider_valueChanged with capital C and second removing the argument name value from its parameter list. With this change your slot on_sizeslider_valueChanged(int) will be called twice for every change of the slider: one call for the connection by name and the other one for your explicit connection. So you should decide for a method of connection: In case of connecting by name remove your explicit connect statement. If you prefer explicit connection, rename your slot to something that is not recognized by Qt for connecting by name, e.g. sizesliderValueChanged(int).

    Personally I dislike the connection by name and I prefer explicit connections. In case you rename GUI objects the compiler will help you finding the places where you have to adjust your C++ code. Connecting by name will find faults only when you open your dialog.

    Best regards
    ars

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

    k3ro (22nd February 2016)

  4. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: No Such Slot

    ars already explained the situation and its solutions but just to be more precise: your error message are actually two error messages.

    The first line is from the "connected by name" feature and complains that there is no signal with the name "valuechanged" on object sizeslider, because it only has "valueChanged" (capitalization error).

    The second and third line are from the connect() statement, since you had "int value" in your SLOT macro, but it only expects argument types, so just "int".

    And of course I also fully agree with ars' assessment regarding using "connect by name". I.e. I would also always use and recommend explicit connect.

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    k3ro (22nd February 2016)

  6. #4
    Join Date
    Feb 2016
    Posts
    7
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: No Such Slot

    Quote Originally Posted by ars View Post
    Best regards
    ars
    Quote Originally Posted by anda_skoa View Post
    Cheers,
    _
    Thanks both for the help. I made the connections explicit and removed the int value mistake and the program now runs without errors.

    I have an additional question if you don't mind, because now the program crashes (error message is "the program has unexpectedly finished") whenever the sliders are moved. The functions are very simple (as you can see from my first post I'm just trying to change the text of a qlabel as the slider moves) and I can't figure out why they're crashing. Any suggestions would be appreciated.

    (Also, this is relatively unimportant, but any ideas why
    Qt Code:
    1. sizeslider->TicksAbove;
    To copy to clipboard, switch view to plain text mode 
    doesn't work?)

    Thanks.

    P.S. anda_skoa sorry about the dupe thread.

  7. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: No Such Slot

    I can't figure out why they're crashing.
    If you built a debug version of your program and ran it in the debugger, you would know immediately. If you don't know how to use a debugger, learn. It is an invaluable tool for programming.

    My guess is that your valueChanged() / on_valueChanged() signal and slot pair is recursive. As soon as you move a slider, it emits the valueChanged() signal. Your slot handles it and does something in the slot that results in a new valueChanged() signal, which recursively calls your slot, ad infinitum until you get a stack overflow.

  8. #6
    Join Date
    Feb 2016
    Posts
    7
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: No Such Slot

    Thanks all. The crashing issue was due to pointer errors.

  9. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: No Such Slot

    Quote Originally Posted by k3ro View Post
    I have an additional question if you don't mind, because now the program crashes (error message is "the program has unexpectedly finished") whenever the sliders are moved. The functions are very simple (as you can see from my first post I'm just trying to change the text of a qlabel as the slider moves) and I can't figure out why they're crashing. Any suggestions would be appreciated.
    Is "sizechoice" a valid pointer?

    Quote Originally Posted by k3ro View Post
    (Also, this is relatively unimportant, but any ideas why
    Qt Code:
    1. sizeslider->TicksAbove;
    To copy to clipboard, switch view to plain text mode 
    doesn't work?)
    What does "doesn't work" mean in this context?

    Enum values are more commonly accessed through qualifying the name by prefixing the class name, not the object, so the equivalent to your code would be
    Qt Code:
    1. QSlider::TicksAbove;
    To copy to clipboard, switch view to plain text mode 
    Which of course doesn't to anything by itself, so it cannot not work.
    Did you mean your code did not compile?

    Cheers,
    _

  10. #8
    Join Date
    Feb 2016
    Posts
    7
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: No Such Slot

    Quote Originally Posted by anda_skoa View Post
    x
    Yes, the crashing was due to pointer problems, it is fixed now.

    Thanks for pointing out my ticks mistake. I now realise the correct syntax is

    Qt Code:
    1. sizeslider->setTickPosition(QSlider::TicksAbove);
    To copy to clipboard, switch view to plain text mode 

    Thanks all for the help. This thread can be closed now if need be.

  11. #9
    Join Date
    Feb 2016
    Posts
    7
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: No Such Slot

    Sorry for double post, but I can't find edit button?

    I have a final question. Can someone please clarify for me. These are two slots I have, which I have explicitly connected.

    .h:
    Qt Code:
    1. public slots:
    2. void create_chara_triggered();
    3. void exit_now();
    To copy to clipboard, switch view to plain text mode 

    .cpp:
    Qt Code:
    1. QObject::connect(createbutton, SIGNAL(clicked()), this, SLOT(create_chara_triggered()));
    2. QObject::connect(exitbutton, SIGNAL(clicked()), this, SLOT(exit_now()));
    3.  
    4. void MainWindow::exit_now(){
    5. this->close();
    6. }
    7.  
    8. void MainWindow::create_chara_triggered(){
    9. Dialog mDialog;
    10. mDialog.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    They are both almost identical. The createbutton slot works, but it says there is no exit_now slot in mainwindow. I honestly don't understand why sometimes slots just don't seem to work? I've tried renaming a few times, and cleaning/rebuilding. My errors previously in the thread were due to connecting by name and trying to pass 'int value' instead of just 'int' into the slot, but this isn't relevant here. Especially when createbutton works when it's been coded in almost exactly the same way.

    Thanks again.

    edit: after some browsing I feel like the issue is with the moc files. I'm writing this code in visual studio and the moc files are supposed to be automatically generated. I'm not sure how to force them to regenerate, so I guess this is where the problem is.
    Last edited by k3ro; 22nd February 2016 at 11:48.

  12. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: No Such Slot

    Usually moc files are regenerated when the header file used as their input changes.

    Maybe the Visual Studio Addin has bad change tracking.

    In this particular case you don't even needt the slot, as close() is a slot and you can directly connect the exit button to close.

    Cheers,
    _

  13. #11
    Join Date
    Feb 2016
    Posts
    7
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: No Such Slot

    Quote Originally Posted by anda_skoa View Post
    Usually moc files are regenerated when the header file used as their input changes.

    Maybe the Visual Studio Addin has bad change tracking.

    In this particular case you don't even needt the slot, as close() is a slot and you can directly connect the exit button to close.

    Cheers,
    _
    I'm planning on adding more lines to the function once it is working, I need to set a bool when the exit button is clicked, so I can't just connect this button to exit().

    I tried deleting q_object and adding it back to force the moc files to generate again. I also tried deleting all moc/ui files to force regenerate, also didn't work.

    In the end, I had to copy all of my code into qt creator, recompile, and then import it back into VS. A lot of hassle simply to add a slot, but oh well...

Similar Threads

  1. No Such Slot
    By Geoffry31 in forum Newbie
    Replies: 9
    Last Post: 13th November 2012, 22:07
  2. Replies: 8
    Last Post: 7th November 2012, 14:10
  3. Replies: 2
    Last Post: 26th August 2011, 08:51
  4. Slot
    By mickey in forum Qt Programming
    Replies: 1
    Last Post: 22nd July 2006, 18:51
  5. signal slot conection using a string, not a SLOT
    By rianquinn in forum Qt Programming
    Replies: 6
    Last Post: 5th February 2006, 18:52

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.