Qt Signal not Emitting on Second Time
-
Hi I need to implement a mail sending application when some event occurred, the background thread should sent the mail, I am referring the implementation here https://github.com/xcoder123/SimpleSmtp_SSL_QT5/tree/master/smtp_attachements.
And from the answer here http://stackoverflow.com/questions/31090619/qt-cuncurrent-with-mail-client it's almost working, means the at the first time the mail get sent, where as the second time the signal not get emiited.
Here is my source
mainWindows.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "smtp.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->sendBtn, SIGNAL(clicked()),this, SLOT(sendMailButtonClicked()));
connect(ui->exitBtn, SIGNAL(clicked()),this, SLOT(close()));
connect(ui->browseBtn, SIGNAL(clicked()), this, SLOT(browse()));
smtp = new Smtp(ui->uname->text(), ui->paswd->text(), ui->server->text(), ui->port->text().toInt());
smtp->moveToThread(&workerThread);
connect(this, SIGNAL(sendMail(QString,QString,QString,QString,QStringList)), smtp, SLOT(sendMail(QString,QString,QString,QString,QStringList)));
connect(smtp, SIGNAL(status(QString)), this, SLOT(mailSent(QString)));
workerThread.start();
}void MainWindow::browse()
{
files.clear();QFileDialog dialog(this);
dialog.setDirectory(QDir::homePath());
dialog.setFileMode(QFileDialog::ExistingFiles);
if (dialog.exec())
files = dialog.selectedFiles();QString fileListString;
foreach(QString file, files)
fileListString.append( """ + QFileInfo(file).fileName() + "" " );
ui->file->setText( fileListString );
}void MainWindow::mailSent(QString status)
{
if(status == "Message sent")
QMessageBox::warning( 0, tr( "Qt Simple SMTP client" ), tr( "Message sent!\n\n" ) );
else
QMessageBox::warning( 0, tr( "Qt Simple SMTP client" ), tr( "Failed!\n\n" ) );qDebug()<<"Sent successfully........!";
}
MainWindow::~MainWindow()
{
workerThread.quit();
workerThread.wait();
delete ui;
}void MainWindow::sendMailButtonClicked()
{
emit sendMail(ui->uname->text(), ui->rcpt->text() , ui->subject->text(),ui->msg->toPlainText(), files );
}MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QtWidgets/QMessageBox>
#include <QFileDialog>
#include <QThread>namespace Ui {
class MainWindow;
}class Smtp;
class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private slots:
void sendMailButtonClicked();
void mailSent(QString);
void browse();private:
Ui::MainWindow ui;
QStringList files;
Smtp smtp;
QThread workerThread;
signals:
void sendMail(const QString &from, const QString &to, const QString &subject, const QString &body, QStringList files);
};#endif // MAINWINDOW_H
in in smtp.h: set sendMail to changed to
public slots:
void sendMail( const QString &from, const QString &to,
const QString &subject, const QString &body,
QStringList files = QStringList());Any idea why the signal not emitted on second time.
-
Hi,
Which signal is not emitted on second time ?
-
Are you sure the signal isn't getting emitted or is it a getting emitted but the sendmail object isn't picking it up? My guess is your send mail object is doing something (locked up and never going back to the event loop). So it is never receiving that second message since it never gets back to the event loop.
Don't know for sure without seeing the code for it, but just something I've noticed in thread programming with Qt.
You could gdb to break into the application and see what your thread is doing. If it isn't in processEvents() that is probably the issue.
Oh and one more thing, you can easily test the emitter by create a fake sendmail object with that same slot, then just have it write to the console when it sees that signal. That way you know if is doing nothing but writing to the console and exiting the slot so there won't be any hang ups. My guess is it will work and that your problem lies in the sendmail object.
[edit]: added more ideas :)