Important: Please read the Qt Code of Conduct -

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

    And from the answer here 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


    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "smtp.h"

    MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)
    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());
    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)));

    void MainWindow::browse()

    QFileDialog dialog(this);
    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" ) );
    QMessageBox::warning( 0, tr( "Qt Simple SMTP client" ), tr( "Failed!\n\n" ) );

    qDebug()<<"Sent successfully........!";


    delete ui;

    void MainWindow::sendMailButtonClicked()
    emit sendMail(ui->uname->text(), ui->rcpt->text() , ui->subject->text(),ui->msg->toPlainText(), files );


    #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

    explicit MainWindow(QWidget *parent = 0);

    private slots:
    void sendMailButtonClicked();
    void mailSent(QString);
    void browse();

    Ui::MainWindow ui;
    QStringList files;
    QThread workerThread;
    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.

  • Lifetime Qt Champion


    Which signal is not emitted on second time ?

  • @haris123 said:

    The Signal

    sendMail(ui->uname->text(), ui->rcpt->text() , ui->subject->text(),ui->msg->toPlainText(), files );

  • Moderators

    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 :)

Log in to reply