Signals and slots in QT


Hi I just started using QT but I have much problem with signals and slots. It seams quite easy but I can't grasp it for quite a time.. I've build such a testing program for this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//main.cpp
#include <QApplication>
#include "test.h"
#include <QtGui>
int main(int argv, char **args){
    QApplication app(argv,args);
    test work;
    work.show();
    return app.exec();
}


//test.h
#ifndef TEST_H
#define TEST_H
#include <QtGUi>
class test : public QWidget
{
    Q_OBJECT
public:
    explicit test(QWidget *parent = 0);
    
signals:
    void send(QString a);
public slots:
    void recive(QString b);
};
#endif // TEST_H


//test.cpp
#include "test.h"
#include <QtGui>
test::test(QWidget *parent) :
    QWidget(parent)
{
    QLineEdit *name = new QLineEdit;
    QLineEdit *surname = new QLineEdit;
    QGridLayout * layout = new QGridLayout;
    layout->addWidget(name,0,0);
    layout->addWidget(surname,1,0);
    name->setText("Dave");
    connect(name,SIGNAL(send(QString)),surname,SLOT(recive(QString)));
    setLayout(layout);
}
void test::recive(QString b){
    if(b=="Bob"){
        emit send(b);
    }
}


I'm trying simply if in first QlineEdit I enter Bob it should write the same in second QlineEdit but It doesn’t work and I quite don’t get it. Would appreciate any help to make it works or other sites where it is explained(other than documentation or youtube) Thanks in advice.
connect(name,SIGNAL(send(QString)),surname,SLOT(recive(QString)));

That isn't right - name and surname are QLineEdit - the send signal and recive slot you are not part of a QLineEdit class

EDIT: To show this example
In this example - we create a 3 way connection - the name editbox object textChanged signal, to the work window recive slot - which automatically emits the send signal which we have connected to the surname editbox object.
(the work window sends the string "Bob" if you enter Bob in the name box, otherwise it send an empty string)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//test.h
#ifndef TEST_H
#define TEST_H

#include <QtGUi>

class test: public QWidget
{
    Q_OBJECT
public:
    explicit test(QWidget *parent = 0);
    
signals:
    void send(QString a);
public slots:
    void recive(QString b);
    
private:
    QLineEdit *name;
    QLineEdit *surname;
    QGridLayout * layout;
    
};

#endif // TEST_H


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//test.cpp
//test.cpp
#include <QtGui>
#include "test.h"

test::test(QWidget *parent ) : QWidget(parent)
{
    name = new QLineEdit;
    surname = new QLineEdit;
    layout = new QGridLayout;
    layout->addWidget(name,0,0);
    layout->addWidget(surname,1,0);
    name->setText("Dave");
    setLayout(layout);

    connect(name,SIGNAL(textChanged(QString)), this,SLOT(recive(QString)));
    connect(this,SIGNAL(send(QString)),surname,SLOT(setText(QString)) );
}

void test::recive(QString b){
    if(b=="Bob")
    {
        emit send(b);
    }
    else
        emit send(""); //empty string
}


1
2
3
4
5
6
7
8
9
10
11
12
//main.cpp
#include <QApplication>
#include "test.h"
#include <QtGui>

int main(int argv, char **args)
{
    QApplication app(argv,args);
    test work;
    work.show();
    return app.exec();
}
Last edited on
Topic archived. No new replies allowed.