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

A

Alex M.

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?
 
R

red floyd

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.
 
J

Jonathan Lee

    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
 
T

tni

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.
 
R

Rolf Magnus

tni said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top