Results 1 to 6 of 6

Thread: Understanding signals/slots - small chat example

  1. #1
    Join Date
    Jan 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Understanding signals/slots - small chat example

    I've written a network chat client in C and (for fun) I'm learning Qt with the eventual goal of writing a GUI for the chat program.

    I'm sort of loosely following the structure of a GUI chat program, but I'm not worrying about connecting up the network code yet - just trying to understand signals and slots.

    So I've (tried) to write a program with an output window, an input window, and a submit button. ALL I want the program to do is echo anything written in the input window to the display window when the submit button is pressed. I think it's clear what I'm trying to do by looking at the code, I'm just doing it wrong obviously.

    Qt Code:
    1. #include <QApplication>
    2. #include <QPushButton>
    3. #include <QLineEdit>
    4. #include <QTextEdit>
    5. #include <QVBoxLayout>
    6.  
    7. class MyWidget : public QWidget
    8. {
    9. public:
    10. MyWidget(QWidget *parent = 0);
    11. };
    12.  
    13. MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
    14. {
    15.  
    16. QPushButton *quit = new QPushButton("Quit", this);
    17. QPushButton *submit = new QPushButton("Submit", this);
    18. QLineEdit *input = new QLineEdit;
    19. QTextEdit *output = new QTextEdit;
    20.  
    21. output->setReadOnly(true);
    22.  
    23. connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
    24. connect(submit, SIGNAL(clicked()), output, SLOT(append(input->text())));
    25.  
    26. QVBoxLayout *layout = new QVBoxLayout;
    27. layout->addWidget(quit);
    28. layout->addWidget(output);
    29. layout->addWidget(input);
    30. layout->addWidget(submit);
    31. setLayout(layout);
    32.  
    33. }
    34.  
    35. int main(int argc, char **argv)
    36. {
    37. QApplication app(argc, argv);
    38. MyWidget widget;
    39. widget.show();
    40. return app.exec();
    41. }
    To copy to clipboard, switch view to plain text mode 

    It compiles cleanly, but when it runs I get the error

    Qt Code:
    1. Object::connect: No such slot QTextEdit::append(input->text()) in main2.cpp:24
    To copy to clipboard, switch view to plain text mode 

    EDIT: Here is the offending line from what I have been told

    Qt Code:
    1. connect(submit, SIGNAL(clicked()), output, SLOT(append(input->text())));
    To copy to clipboard, switch view to plain text mode 

    What gives? According to here, append *is* a slot. Why can't I use it? What am I missing?

    Thanks, Benjamin
    Last edited by caesius; 12th January 2010 at 02:42.

  2. #2
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    3

    Default Re: Understanding signals/slots - small chat example

    Two mistakes here:
    1. When connecting to a slot, you cannot decide what should be passed. (like you input->text()), the parameter is always using the one passed from the signal. You can only specify a "prototype" here, e.g. SLOT(append(QString))
    2. When connecting a signal to a slot, the parameters should be match, while they don't here. So you should connect the clicked signal to some use defined function in your class (e.g. could be named as submitClicked) and call input->appned() from that function.

    Hope it helps.
    1. Users don't have the manual, and if they did, they wouldn't read it.
    2. In fact, users can't read anything, and if they could, they wouldn't want to.

  3. #3
    Join Date
    Jan 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Understanding signals/slots - small chat example

    Quote Originally Posted by bood View Post
    Two mistakes here:
    1. When connecting to a slot, you cannot decide what should be passed. (like you input->text()), the parameter is always using the one passed from the signal. You can only specify a "prototype" here, e.g. SLOT(append(QString))
    2. When connecting a signal to a slot, the parameters should be match, while they don't here. So you should connect the clicked signal to some use defined function in your class (e.g. could be named as submitClicked) and call input->appned() from that function.

    Hope it helps.
    Thanks for the reply. I don't understand (2) however, if I wrote a function like what I've got below (which is what you're suggesting?), how would this function know that the varibles input and output exist?

    Qt Code:
    1. MyWidget::submitClicked(void) {
    2. output->append(input->text());
    3. }
    To copy to clipboard, switch view to plain text mode 

    Wouldn't I have to pass the addresses of input and output along with this function?

  4. #4
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    3

    Default Re: Understanding signals/slots - small chat example

    Of course you don't have to.
    Usually, these variables are defined as member variables of your widget class, so that you can reference them in any member functions as you want.
    1. Users don't have the manual, and if they did, they wouldn't read it.
    2. In fact, users can't read anything, and if they could, they wouldn't want to.

  5. #5
    Join Date
    Jan 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Understanding signals/slots - small chat example

    Thanks for clearing that up, I've re-written the code based on what I've understood you to be saying.

    Qt Code:
    1. #include <QApplication>
    2. #include <QPushButton>
    3. #include <QLineEdit>
    4. #include <QTextEdit>
    5. #include <QVBoxLayout>
    6.  
    7. class MyWidget : public QWidget
    8. {
    9. public:
    10. MyWidget(QWidget *parent = 0);
    11. QLineEdit *input;
    12. QTextEdit *output;
    13.  
    14. public slots:
    15. void putMessage(void);
    16.  
    17. private:
    18.  
    19. };
    20.  
    21. MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
    22. {
    23.  
    24. QPushButton *quit = new QPushButton("Quit", this);
    25. QPushButton *submit = new QPushButton("Submit", this);
    26. input = new QLineEdit;
    27. output = new QTextEdit;
    28.  
    29. output->setReadOnly(true);
    30.  
    31. connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
    32. connect(submit, SIGNAL(clicked()), this, SLOT(putMessage()));
    33.  
    34. QVBoxLayout *layout = new QVBoxLayout;
    35. layout->addWidget(quit);
    36. layout->addWidget(output);
    37. layout->addWidget(input);
    38. layout->addWidget(submit);
    39. setLayout(layout);
    40.  
    41. }
    42.  
    43. void MyWidget::putMessage(void)
    44. {
    45. output->append(input->text());
    46. }
    47.  
    48. int main(int argc, char **argv)
    49. {
    50. QApplication app(argc, argv);
    51. MyWidget widget;
    52. widget.show();
    53. return app.exec();
    54. }
    To copy to clipboard, switch view to plain text mode 

    I'm still not doing something right because I still get
    Qt Code:
    1. Object::connect: No such slot QWidget::putMessage() in main2.cpp:32
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    3

    Default Re: Understanding signals/slots - small chat example

    Try adding a "Q_OBJECT" notation at the beginning of your class.
    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. Q_OBJECT
    4. //blah blah blah
    5. };
    To copy to clipboard, switch view to plain text mode 
    1. Users don't have the manual, and if they did, they wouldn't read it.
    2. In fact, users can't read anything, and if they could, they wouldn't want to.

Similar Threads

  1. about signals and slots
    By Sandip in forum Qt Programming
    Replies: 9
    Last Post: 15th July 2008, 16:02
  2. Signals and Slots
    By 83.manish in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2008, 10:31
  3. regarding signals/slots
    By jjbabu in forum Qt Programming
    Replies: 2
    Last Post: 4th October 2007, 09:32
  4. Signals and Slots
    By merry in forum Qt Programming
    Replies: 4
    Last Post: 22nd February 2007, 08:11
  5. Signals and Slots in dll
    By ankurjain in forum Qt Programming
    Replies: 8
    Last Post: 29th March 2006, 08:12

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
  •  
Qt is a trademark of The Qt Company.