Qt Slots and Signals Help please?

Hi I'm new to c++ and qt and I'm really sorry for posting all this code, but I really need help with the project I'm doing now and I'm really desperate. My program was suppose to check the source code of a website and compare it to a later version to see if anything changed. I tried to make a GUI for it using Qt and here's what I get. Basically its a dialog box with a start button. After clicking start it continues to check the website every few minutes. What I'm trying to do is make it so that when an update happens a dialog box pops up and says something like "Update" and then you can close the box; that's what I tried to do by making addDialog.cpp and .h. Except I'm really having trouble with the slots and signals because I don't really understand it. I already read the tutorial at http://doc.qt.nokia.com/4.7/signalsandslots.html and I'm still confused.

Does anyone know how I could make it so that in the hasUpdate() function of Notifier.cpp when it has an update it emits a signal that runs the addEntry() function making a pop up window? Thanks for any help, I really appreciate it and sorry for all the trouble...

adddialog.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
#ifndef ADDDIALOG_H
#define ADDDIALOG_H

#include <QDialog>

QT_BEGIN_NAMESPACE
class QLabel;
class QPushButton;
class QTextEdit;
class QLineEdit;
QT_END_NAMESPACE

//! [0]
class AddDialog : public QDialog
{
    Q_OBJECT

public:
    AddDialog(QWidget *parent=0);

private:
    QLabel *nameLabel;
};
//! [0]

#endif 


mainwindow.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H 


notifier.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
28
#ifndef NOTIFIER_H
#define NOTIFIER_H
#include <QDialog>
#include <qwidget.h>
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;
class QMessageBox;

class Notifier: public QDialog
{
    Q_OBJECT
public:
    Notifier(QWidget *parent = 0);
private slots:
    void cycle();
    void addEntry();
signals:
    void hasUpdate();
private:
    QPushButton *startButton;
    QPushButton *window;

};


#endif 

addDialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <QtGui>
#include "adddialog.h"
// the dialog that pops up when you presss begin

AddDialog::AddDialog(QWidget *parent)
    : QDialog(parent)
{
    nameLabel = new QLabel("   Update"); //label of name box


    QGridLayout *gLayout = new QGridLayout;//sets up object for layout of the screen
    gLayout->setColumnStretch(1, 2);//column size
    gLayout->addWidget(nameLabel, 0, 0);//shows the name label next to the text box, which tells u what to add


    QVBoxLayout *mainLayout = new QVBoxLayout; //i duno what this is for maybe the entire layout?
    mainLayout->addLayout(gLayout);
    setLayout(mainLayout);


    setWindowTitle(tr("Notification")); //names entire window
}
//! [0] 


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QApplication>
#include <QLabel>
#include <QHBoxLayout>
#include <QSlider>
#include <QSpinBox>
#include <QPushButton>
#include "notifier.h"
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    Notifier *something = new Notifier;
    something->show();

    return app.exec();
}

mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


notifier.cpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <QtGui>
#include "notifier.h"
#include <stdio.h>
#include <iostream>
#include <string>
#include <time.h>
#include <vector>
#include <fstream>
#include <ctime>
#include "windows.h"
#include <qmessagebox.h>
#include "adddialog.h"
using namespace std;
Notifier::Notifier(QWidget *parent)
    :QDialog(parent)
{
    startButton = new QPushButton(tr("&Start"));
   
    connect(startButton, SIGNAL(clicked()),
            this, SLOT(cycle()));
   
    QVBoxLayout *rightLayout = new QVBoxLayout;
    rightLayout ->addWidget(startButton);
    //rightLayout ->addWidget(window);
    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->addLayout(rightLayout);
    setLayout(mainLayout);
    setWindowTitle(tr("Notifier"));
    setFixedHeight(sizeHint().height());
}

vector<string> getInfo2();
void overWrite()
{
        ofstream newfile ("C:\\Users\\Alec Cheung\\Notifierv2-build-desktop\\Temp.txt");
        int stop = getInfo2().size();
        int count = 0;
        if (newfile.is_open())
        {
                while (  count <stop)
                {
                        newfile << getInfo2().at(count)<< endl;
                        count++;
                }
                newfile.close();
        }
}
vector<string> getInfo1()
{
        string tempLine;
        vector <string> archiveT;

        ifstream tempFile ("C:\\Users\\Alec Cheung\\Notifierv2-build-desktop\\Temp.txt");
        if (tempFile.is_open())
        {
                while (getline (tempFile, tempLine))
                {
                        archiveT.push_back(tempLine);
                }

        }
        return archiveT;

}
vector<string> getInfo2()
{
        vector <string> archiveC;
        string tempLine;
        ifstream curFile ("C:\\Users\\Alec Cheung\\Notifierv2-build-desktop\\Current.txt");
        if (curFile.is_open())
        {

                while (getline (curFile, tempLine))
                {
                        archiveC.push_back(tempLine);

                }
        }
        return archiveC;
}

bool compare(vector<string> original1 , vector<string> latest1)
{
        int x = 0;
        while (x<original1.size())
        {
                if (original1[x].compare(latest1[x])!= 0)
                {
                        return true;
                }
                x++;
        }
        return false;
}

void hasUpdate()
{
     /*AddDialog note;
     connect (&note,SIGNAL(hasUpdate()), this, SLOT(addEntry()));*/
        time_t now;
        if (compare(getInfo1(), getInfo2()))
        {
                time (&now);
                ctime(&now);
                 
                //  connect(addButton, SIGNAL(clicked()), this, SLOT(addEntry()));
                overWrite();
               emit hasUpdate();
        }
        else
        {
            QLabel *s = new QLabel("No Update");
            s->show();
        }
}
void Notifier::cycle()
{
    int repeat = 86400;
    int x = 0;
    char* thing = "curl http://www.google.com >Temp.txt";
    system(thing); //dumps output from curl into a text file
    while (x<repeat)
    {
                //Sleep(7500);
        Sleep(2000);
        system("curl http://www.google.com > Current.txt");
        hasUpdate();
        x++;
    }

}
void Notifier::addEntry()
{
    AddDialog aDialog;

    aDialog.exec();


}




Last edited on
You'll need a function that checks when the page has an update. If there was an update just do emit hasUpdate(); I don't think signals can be used as functions like in ur notifier.cpp. :P. Just make another function and emit the signal from there. Then u gotta connect the signal & slot preferably in the constructor. connect(this, SIGNAL(hasUpdate()), this, SLOT(addEntry()));
Thanks for the advice, but now I have another question. Even without the signals and slots, I tried to make it so that it did qDebug()<< "Update"; or qDebug()<< "No update" for the hasUpdate() function in the if statement to see if my program actually works. Now the problem comes up, it doesn't work... Or at least it doesn't show any sign of working when I start the program and press that start button which should make the program get the source code from a website and put it into a text file, which it does. But then it seems like it just skips the hasUpdate() function altogether or something is wrong with the following functions because there is no output. But I don't think that's the case because I tried it without the GUI in Microsoft Visual C++ 2010 and it works perfectly fine.

Any ideas on whats going on?
Last edited on
I also just tried to do what you suggested, I made signal called "void update()" but for some reason it says "update has not been declared in this scope" even though I made the signal in the header file...

notifier.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
28
29
30
31
32
#ifndef NOTIFIER_H
#define NOTIFIER_H
#include <QDialog>
#include <qwidget.h>
#include <QString>
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;
class QMessageBox;

class Notifier: public QDialog
{
    Q_OBJECT
public:
    Notifier(QWidget *parent = 0);
signals:
    void updated();
private slots:
    void cycle();
    void addEntry();

private:
    QPushButton *startButton;
    QPushButton *window;
    QLabel *label;
    QLineEdit *lineEdit;

};


#endif 



notifier.cpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <QtGui>
#include "notifier.h"
#include <stdio.h>
#include <iostream>
#include <string>
#include <time.h>
#include <vector>
#include <fstream>
#include <ctime>
#include "windows.h"
#include <qmessagebox.h>
#include "adddialog.h"
#include <QString>
using namespace std;
Notifier::Notifier(QWidget *parent)
    :QDialog(parent)
{
    label = new QLabel(tr("URL: "));
    lineEdit = new QLineEdit;
    label->setBuddy(lineEdit);
    startButton = new QPushButton(tr("&Start"));

   // connect(linEdit, SIGNAL(textChanged(const QString &)),
  //          this, SLOT());
    connect(this, SIGNAL(updated()), this, SLOT(addEntry()));
    connect(startButton, SIGNAL(clicked()),
            this, SLOT(cycle()));

    QVBoxLayout *rightLayout = new QVBoxLayout;
    rightLayout ->addWidget(startButton);
    rightLayout ->addWidget(label);
    rightLayout ->addWidget(lineEdit);
    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->addLayout(rightLayout);
    setLayout(mainLayout);
    setWindowTitle(tr("Notifier"));
    setFixedHeight(sizeHint().height());
}

vector<string> getInfo2();
void overWrite()
{
        ofstream newfile ("C:\\Users\\Alec Cheung\\Notifierv2-build-desktop\\Temp.txt");
        int stop = getInfo2().size();
        int count = 0;
        if (newfile.is_open())
        {
                while (  count <stop)
                {
                        newfile << getInfo2().at(count)<< endl;
                        count++;
                }
                newfile.close();
        }
}
vector<string> getInfo1()
{
        string tempLine;
        vector <string> archiveT;

        ifstream tempFile ("C:\\Users\\Alec Cheung\\Notifierv2-build-desktop\\Temp.txt");
        if (tempFile.is_open())
        {
                while (getline (tempFile, tempLine))
                {
                        archiveT.push_back(tempLine);
                }

        }
        return archiveT;

}
vector<string> getInfo2()
{
        vector <string> archiveC;
        string tempLine;
        ifstream curFile ("C:\\Users\\Alec Cheung\\Notifierv2-build-desktop\\Current.txt");
        if (curFile.is_open())
        {

                while (getline (curFile, tempLine))
                {
                        archiveC.push_back(tempLine);

                }
        }
        return archiveC;
}

bool compare(vector<string> original1 , vector<string> latest1)
{
        int x = 0;
        while (x<original1.size())
        {
                if (original1[x].compare(latest1[x])!= 0)
                {
                        return true;
                }
                x++;
        }
        return false;
}

void hasUpdate()
{
    // AddDialog note;
     //connect (note,SIGNAL(note.hasUpdate()), this, SLOT(addEntry()));
        time_t now;
        if (compare(getInfo1(), getInfo2()))
        {
                time (&now);
               // ctime(&now);

                emit updated();
                //connect(addButton, SIGNAL(clicked()), this, SLOT(addEntry()));
        }
}
void Notifier::cycle()
{
    int repeat = 86400;
    int x = 0;
    char* thing = "curl http://www.mangareader.net >Temp.txt";
    system(thing); //dumps output from curl into a text file
   // while (x<repeat)
   // {
                //Sleep(7500);
        Sleep(2000);
        system("curl http://www.mangareader.net > Current.txt");
        hasUpdate();
        x++;
   // }

}
void Notifier::addEntry()
{
    AddDialog aDialog;

    aDialog.exec();


}


Topic archived. No new replies allowed.