A problem with slots and signals in a simple widget in Qt

Discussion in 'C++' started by Alex M., Aug 1, 2010.

  1. Alex M.

    Alex M. Guest

    Hi, here's my code, it's a simple widget with Qt. :)

    [main.cpp]
    _________
    #include <QtGui/QApplication>
    #include "widget.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    Widget W(250,200);

    return a.exec();
    }
    _________

    [widget.h]
    _________
    #ifndef WIDGET_H
    #define WIDGET_H

    #include <QWidget>
    #include <QPushButton>
    #include <QLabel>
    #include <QTextEdit>

    class Widget : public QWidget{
    private:
    QTextEdit *t;

    public slots:
    void clearText(QTextEdit *t);

    public:
    Widget(int, int);
    };

    class widget : public QWidget{
    public:
    QPushButton *b;
    QLabel *l;
    widget(Widget *parent);
    };

    #endif // WIDGET_H
    _________

    [widget.cpp]
    _________
    #include <widget.h>
    #include <QHBoxLayout>
    #include <QVBoxLayout>

    Widget::Widget(int a, int b){

    widget *w = new widget(this);

    t = new QTextEdit(this);
    QVBoxLayout *qv = new QVBoxLayout;

    qv->addWidget(t);
    qv->addWidget(w);

    connect(w->b, SIGNAL(clicked()), t, SLOT(clearText(t))); //
    clears the content of t
    connect(w->b, SIGNAL(clicked()), t, SLOT(setFocus())); //sets
    focus to t

    setLayout(qv);
    setFixedSize(a, b);
    setWindowTitle("QuickTweet");
    show();
    };

    void Widget::clearText(QTextEdit *t){
    t->clear();
    }

    widget::widget(Widget *parent){
    b = new QPushButton(tr("Clear"), this);
    l = new QLabel(tr("160 characters remaining"));

    QHBoxLayout *ql = new QHBoxLayout;
    ql->addWidget(l);
    ql->addWidget(b);

    setLayout(ql);
    show();
    };
    _________

    When I build this and press the "Clear" button the t [QTextEdit] won't
    clear. :) What have I done wrong?
    Alex M., Aug 1, 2010
    #1
    1. Advertising

  2. Alex M.

    red floyd Guest

    On 8/1/2010 8:09 AM, Alex M. wrote:
    > Hi, here's my code, it's a simple widget with Qt. :)
    >
    >[redacted\
    >
    > When I build this and press the "Clear" button the t [QTextEdit] won't
    > clear. :) What have I done wrong?


    You asked in a newsgroup where QT is not on topic. Try a QT forum.
    red floyd, Aug 1, 2010
    #2
    1. Advertising

  3. Alex M.

    Jonathan Lee Guest

    On Aug 1, 11:09 am, "Alex M." <> wrote:
    >     connect(w->b, SIGNAL(clicked()), t, SLOT(clearText(t)));   //
    > clears the content of t

    [snip]
    > When I build this and press the "Clear" button the t [QTextEdit] won't
    > clear. :) What have I done wrong?


    First of all, Red Floyd is right: you should hit up Trolltech
    newsgroups and ask your question there.

    But I happen to know the answer: clearText is not a member of
    QTextEdit. It's a member of your Widget class. When connect
    goes to make the connection with t, it won't find the slot.
    *It should print this error on the command line.* Change
    the slot to SLOT(clear()) which _is_ a member of QTextEdit.

    If that's not clear for you, please follow up in one of the
    Qt newsgroups: http://lists.trolltech.com/

    --Jonathan
    Jonathan Lee, Aug 1, 2010
    #3
  4. Alex M.

    tni Guest

    On 2010-08-01 17:09, Alex M. wrote:

    > t = new QTextEdit(this);
    > QVBoxLayout *qv = new QVBoxLayout;
    >
    > qv->addWidget(t);
    > qv->addWidget(w);
    >
    > connect(w->b, SIGNAL(clicked()), t, SLOT(clearText(t))); //
    > clears the content of t


    t of type QTextEdit doesn't have a slot 'clearText'. Even if it did,
    'SLOT(clearText(t))' is garbage, you can only forward parameters from
    signals (and you can only specify a type, not a variable in the 'SLOT()'
    macro).

    The console output should show an error that the connect failed.

    That being said, signals and slots aren't standard C++, so the Qt
    mailing lists are a better place to ask.
    tni, Aug 1, 2010
    #4
  5. Alex M.

    Rolf Magnus Guest

    tni wrote:

    > On 2010-08-01 17:09, Alex M. wrote:
    >
    >> t = new QTextEdit(this);
    >> QVBoxLayout *qv = new QVBoxLayout;
    >>
    >> qv->addWidget(t);
    >> qv->addWidget(w);
    >>
    >> connect(w->b, SIGNAL(clicked()), t, SLOT(clearText(t))); //
    >> clears the content of t

    >
    > t of type QTextEdit doesn't have a slot 'clearText'. Even if it did,
    > 'SLOT(clearText(t))' is garbage, you can only forward parameters from
    > signals (and you can only specify a type, not a variable in the 'SLOT()'
    > macro).


    Alex also forgot to use the Q_OBJECT macro.
    Rolf Magnus, Aug 2, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frank Bossy
    Replies:
    1
    Views:
    457
    Victor Bazarov
    Jul 9, 2003
  2. tin gherdanarra

    slots? SLOTS?

    tin gherdanarra, Oct 12, 2005, in forum: Python
    Replies:
    2
    Views:
    2,297
    Peter Hansen
    Oct 13, 2005
  3. lolveley
    Replies:
    6
    Views:
    173
    Stefano Crocco
    May 13, 2009
  4. Jorgen Grahn
    Replies:
    11
    Views:
    634
    Jorgen Grahn
    Aug 21, 2012
  5. Rui Maciel
    Replies:
    0
    Views:
    360
    Rui Maciel
    Aug 11, 2012
Loading...

Share This Page