I Have a server which is built on gsoap. Gsoap is also written on Qt. So I can use Qt classes.
From the client I send a request to the server.
The request is the following: server supplied a phone number and message which should be sent to him. This list may be 10 000 or more, this is exactly the problem! I'm going to send a message to each number with QThread. When QThread ends its work, the history of the database should be recorded.. The problem is that we can not understand when all the history is recorded in the database.
I know this was a very bad explanation for the problem! Excuse me for my English.
Now I will try to show you the problem with the program.
I have one QThread class, which send messages. One History class, which write a history in the database.
void MainWindow::on_pushButton_clicked() //Imagine that this is a server function.
{
QSet<int> st; //List to who sends a message.
st << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10;
QSet<int> clientAnswer = this->start(st);
qDebug() << "newSet: " << clientAnswer;
//return ClientAnswer list ot client.
}
QSet<int> MainWindow::start(QSet<int> st)
{
for (auto it : st) {
MyThread *thrd = new MyThread(it);
History *hist = new History();
connect(thrd, &MyThread::smsSended, hist, &History::insertHistory);
connect(hist, &History::historyAdded, this, [=](int historyUID){
qDebug() << "history inserted: " << historyUID;
this->ansSet.insert(historyUID);
if (this->ansSet.size() == st.size()) {
qDebug() << "mainwindow finished!";
emit alreadyDone();
}
});
connect(thrd, &MyThread::finished, hist, &History::deleteLater);
connect(thrd, &MyThread::finished, thrd, &MyThread::deleteLater);
thrd->start();
}
return this->ansSet;
}
MainWindow.h
private:
Ui::MainWindow *ui;
QSet<int> ansSet; //The list is to send a client.
MyThread.cpp:
class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(int tInt, QObject *parent = 0);
signals:
void smsSended(int);
public slots:
void run();
private:
int m_int;
};
MyThread.h
MyThread::MyThread(int tInt, QObject *parent) : QThread(parent)
{
this->m_int = tInt;
}
void MyThread::run()
{
qDebug() << "thread started: " << this->m_int;
emit smsSended(this->m_int * 10);
}
History.cpp
History::History(QObject *parent) : QObject(parent)
{
}
void History::insertHistory(int insertInt)
{
qDebug() << "insert History: " << insertInt;
emit historyAdded(insertInt);
}
History.h
class History : public QObject
{
Q_OBJECT
public:
explicit History(QObject *parent = 0);
signals:
void historyAdded(int hInt);
public slots:
void insertHistory(int insertInt);
};
Application Output looks like:
thread started: 5
thread started: 1
thread started: 3
thread started: 2
thread started: 4
thread started: 7
thread started: 9
thread started: 6
newSet: QSet()
thread started: 8
thread started: 10
insert History: 50
history inserted: 50
insert History: 10
history inserted: 10
insert History: 30
history inserted: 30
insert History: 20
history inserted: 20
insert History: 40
history inserted: 40
insert History: 70
history inserted: 70
insert History: 90
history inserted: 90
insert History: 60
history inserted: 60
insert History: 80
history inserted: 80
insert History: 100
history inserted: 100
mainwindow finished!
I know this output is right. But how I can do return a QSet ---> ansSet from MainWindow :: start function when happen if (this->ansSet.size() == st.size())
? Or you have a other idea?
I'm very sorry for my English