Results 1 to 6 of 6

Thread: Can't connect a signal to a slot

  1. #1
    Join Date
    Aug 2010
    Location
    Joplin, Mo
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Can't connect a signal to a slot

    I'm using Qt 4.6.3 SDK on the 64 bit version Fedora 13. I created a project via Creator & the form via Designer. It's a trivial program intended only to experiment with Signals & Slots to get me started with Qt. I've read everything I can find & I've done just what the docs say is right, but it doesn't work. I'm including the code from the header & cpp as well as the output from the build. Any help in understanding why this isn't working will be greatly appreciated.

    Note: The app does run & the initial string is displayed.

    Thanx,

    Ed
    This is the header:
    Qt Code:
    1. #ifndef SANDSDEMO_H
    2. #define SANDSDEMO_H
    3.  
    4. #include <QWidget>
    5. #include <QObject>
    6. #include <QLabel>
    7.  
    8. namespace Ui {
    9. class SandSDemo;
    10. }
    11.  
    12. class SandSDemo : public QWidget
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit SandSDemo(QWidget *parent = 0);
    18. ~SandSDemo();
    19.  
    20. public slots:
    21. void cancelText();
    22.  
    23. private:
    24. Ui::SandSDemo *ui;
    25. void initialText();
    26. };
    27.  
    28. #endif // SANDSDEMO_H
    To copy to clipboard, switch view to plain text mode 

    This is the source:
    Qt Code:
    1. #include "sandsdemo.h"
    2. #include "ui_sandsdemo.h"
    3.  
    4. SandSDemo::SandSDemo(QWidget *parent) :
    5. QWidget(parent),
    6. ui(new Ui::SandSDemo)
    7. {
    8. ui->setupUi(this);
    9. connect(ui->cancelButton,SIGNAL(clicked()),ui->msgArea,SLOT(cancelText()));
    10. initialText();
    11. }
    12.  
    13. SandSDemo::~SandSDemo()
    14. {
    15. delete ui;
    16. }
    17.  
    18. void SandSDemo::initialText()
    19. {
    20. QString * initialMsg = new QString( "This is the string that will be displayed upon initialization.");
    21.  
    22. ui->msgArea->setText(*initialMsg);
    23. delete initialMsg;
    24. }
    25.  
    26. void SandSDemo::cancelText()
    27. {
    28. QString * cancelMsg = new QString("This is the string that will be displayed when the cancel button is clicked.");
    29. ui->msgArea->setText(*cancelMsg);
    30. delete cancelMsg;
    31. }
    To copy to clipboard, switch view to plain text mode 

    This is the error from the build output:

    Starting /home/ed/projects/SandSDemo-build-desktop/SandSDemo...
    Object::connect: No such slot QLabel::cancelText() in ../SandSDemo/sandsdemo.cpp:9
    Object::connect: (sender name: 'cancelButton')
    Object::connect: (receiver name: 'msgArea')
    /home/ed/projects/SandSDemo-build-desktop/SandSDemo exited with code 0

  2. #2
    Join Date
    Aug 2010
    Location
    Joplin, Mo
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can't connect a signal to a slot

    Additional infor: I originally tried to connect via designer, but my slot isn't available in the signals & slots editor.

    Thanx,

    Ed

  3. #3
    Join Date
    Jul 2010
    Posts
    21
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can't connect a signal to a slot

    Try
    Qt Code:
    1. connect(ui->cancelButton,SIGNAL(clicked()), this, SLOT(cancelText()));
    To copy to clipboard, switch view to plain text mode 
    Moreover, in your initialText() and cancelText() functions, you can set the text as
    Qt Code:
    1. ui->msgArea->setText("blaa");
    To copy to clipboard, switch view to plain text mode 
    No need to introduce a new QString in heap for temporary use.

  4. #4
    Join Date
    Aug 2010
    Location
    Joplin, Mo
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can't connect a signal to a slot

    Thank you so much. That did the trick. It's been several years since I tried to write anything in any language. This app is just a trivial attempt to refresh my memory in several areas as well as trying to become familiar with Qt. Thus the pointers & memory usage.

    I am curious to know exactly which object "this" points to in this case. And I'm confused as to why Designer wouldn't let me make the connection.

    Ed

  5. #5
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Can't connect a signal to a slot

    Welcome Ed.

    Regarding the this pointer (from here):
    this is usually an immutable reference or pointer which refers to the current object.
    So in your case it is a pointer to the current instance of SandSDemo.

    In Designer try connecting your slot like this:
    • Press F4 // puts you in signals/slots editing mode

    • Left click and hold on your pushbutton, drag to your main widget and release. // you should see something like an electrical ground symbol

    • Click on a signal in the left panel of the dialog that pops up
    • Click edit under the right panel and add your slot.

    • Close the dialog and press F3 to return to widget edit mode.


    Another way to make connections is to let the Meta-Object Compiler, or moc, automatically connect your custom slot. You have to follow the naming convention described here. If you name your slot on_cancelButton_clicked() you won't need the connect statement.

    HTH

  6. #6
    Join Date
    Aug 2010
    Location
    Joplin, Mo
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can't connect a signal to a slot

    Thank you very much. This is just the kind of answer I hoped for. I'll read naming convention info you provided & try the Designer Slot Editor again. I hadn't tried connecting to the Main Widget so the edit buttons were grayed out. This has given me plenty to play with. I'm sure I'll like one way be able to get comfortable making the connections now.

    If you're watching this forum, maybe you'll be able to provide some insight into a couple of other issues I'll be posting shortly. As I mentioned before, this is all trivial. I'm just getting my feet wet again & playing around with code is the best way to improve my skills.

    Ed

Similar Threads

  1. How to connect signal/slot in QItemEditorFactory?
    By yyalli in forum Qt Programming
    Replies: 1
    Last Post: 4th June 2010, 15:56
  2. problem connect signal - slot
    By jaca in forum Newbie
    Replies: 13
    Last Post: 9th March 2010, 20:38
  3. Replies: 8
    Last Post: 27th August 2009, 15:51
  4. A signal/slot connect isn't working.
    By Daimonie in forum Qt Programming
    Replies: 6
    Last Post: 15th February 2009, 23:55
  5. Connect signal from base to slot of sub-sub-object
    By donglebob in forum Qt Programming
    Replies: 15
    Last Post: 30th October 2008, 20:54

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.