Results 1 to 6 of 6

Thread: Understanding signals/slots - small chat example

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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.