Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > A problem with slots and signals in a simple widget in Qt

Reply
Thread Tools

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

 
 
Alex M.
Guest
Posts: n/a
 
      08-01-2010
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?
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      08-01-2010
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.

 
Reply With Quote
 
 
 
 
Jonathan Lee
Guest
Posts: n/a
 
      08-01-2010
On Aug 1, 11:09*am, "Alex M." <(E-Mail Removed)> 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
 
Reply With Quote
 
tni
Guest
Posts: n/a
 
      08-01-2010
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.

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      08-02-2010
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Github (was Re: Free lightweight C++ signals and slots library) Jorgen Grahn C++ 11 08-21-2012 08:08 PM
(mandriva,eclipse,Qt) elementary problem : signals and slots lolveley Ruby 6 05-13-2009 09:47 AM
slots? SLOTS? tin gherdanarra Python 2 10-13-2005 12:31 AM
Problem with QT / Threads / Signals / Slots Frank Bossy C++ 1 07-09-2003 03:20 PM



Advertisments