QT未来观察员与邮件客户端 [英] QT Future Watcher With mail Client

查看:168
本文介绍了QT未来观察员与邮件客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这里的代码 https://github.com/xcoder123/ SimpleSmtp_SSL_QT5 / tree / master / smtp_attachements 从我的qt应用程序发送邮件,它的工作正常,但是当我使用未来的游艇在另一个线程中运行邮件发件人它不工作

  QFuture< void> f4 = QtConcurrent :: run(this,& MainWindow :: sendMail); //这不工作
// sendMail(); //这个工作

这是mainWindow.cpp


$ b $
#includeQSettings
#includeQFuture
#includeQtConcurrent / QtConcurrent

MainWindow :: MainWindow(QWidget * parent):
QMainWindow(parent),
ui(new Ui :: MainWindow)
{
ui-> setupUi(this);
connect(ui-> sendBtn,SIGNAL(clicked()),this,SLOT(test()));
connect(ui-> exitBtn,SIGNAL(clicked()),this,SLOT(close()));
connect(ui-> browseBtn,SIGNAL(clicked()),这个,SLOT(browse()));
}

void MainWindow :: browse()
{
files.clear();

QFileDialog对话框(这);
dialog.setDirectory(QDir :: homePath());
dialog.setFileMode(QFileDialog :: ExistingFiles);

if(dialog.exec())
files = dialog.selectedFiles();

QString fileListString;
foreach(QString文件,文件)
fileListString.append(\+ QFileInfo(file).fileName()+\);

ui-> file-> setText(fileListString);

}
void MainWindow :: test(){

QFuture< void> f4 = QtConcurrent :: run(this,& MainWindow :: sendMail);
// sendMail();
}



void MainWindow :: sendMail()
{
Smtp * smtp = new Smtp(ui-> uname- > text(),ui-> paswd-> text(),ui-> server-> text(),ui-> port-> text()toInt());
connect(smtp,SIGNAL(status(QString)),这个,SLOT(mailSent(QString)));

if(!files.isEmpty())
smtp-> sendMail(ui-> uname-> text(),ui-> rcpt-> text() ,ui-> subject-> text(),ui-> msg-> toPlainText(),文件);
else
smtp-> sendMail(ui-> uname-> text(),ui-> rcpt-> text(),ui-> subject-> text ,UI-> MSG-> toPlainText());
}

void MainWindow :: mailSent(QString status)
{
if(status ==Message sent)
QMessageBox :: warning 0,tr(Qt Simple SMTP client),tr(Message sent!\\\
\\\
));
}

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

和mainWindow.h

  #ifndef MAINWINDOW_H 
#define MAINWINDOW_H

#include< QMainWindow>
#includesmtp.h
#include&QTWidgets / QMessageBox>
#include< QFileDialog>

命名空间Ui {
class MainWindow;
}

class MainWindow:public QMainWindow
{
Q_OBJECT

public:
显式MainWindow(QWidget * parent = 0 );
〜MainWindow();

私人插槽:
void sendMail();
void mailSent(QString);
void browse();
void test();

private:
Ui :: MainWindow * ui;
QStringList文件;

};

#endif // MAINWINDOW_H

smtp类如下所示,我没有触摸任何东西



https://github.com/xcoder123/SimpleSmtp_SSL_QT5/blob/master/smtp_attachements/smtp.h
https://github.com/xcoder123/SimpleSmtp_SSL_QT5/blob/master/smtp_attachements/smtp.cpp



任何人知道为什么在使用未来观察者的线程中运行时不起作用。



感谢
Haris

解决方案

问题是 QtConcurrent :: run 只运行一个函数,在你的情况下 sendMail ,在一个单独的线程中。一旦该函数返回,线程不再有效 Smtp 中的插槽将不会被调用。



将函数 sendMail 更改为返回,直到邮件发送或使用 QThread 并移动 Smtp * stmp 使用 QObject :: moveToThread


Hi I am using the code from here https://github.com/xcoder123/SimpleSmtp_SSL_QT5/tree/master/smtp_attachements to send mail from my qt application and it's work fine, but when I use future wacht to run the mail sender in another thread it does'nt work

QFuture<void> f4 = QtConcurrent::run(this,&MainWindow::sendMail); // this doesnot work
//sendMail(); //this work 

Here is the mainWindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QSettings"
#include "QFuture"
#include "QtConcurrent/QtConcurrent"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->sendBtn, SIGNAL(clicked()),this, SLOT(test()));
    connect(ui->exitBtn, SIGNAL(clicked()),this, SLOT(close()));
    connect(ui->browseBtn, SIGNAL(clicked()), this, SLOT(browse()));
}

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::test(){

    QFuture<void> f4 = QtConcurrent::run(this,&MainWindow::sendMail);
    //sendMail();
}



void MainWindow::sendMail()
{
    Smtp* smtp = new Smtp(ui->uname->text(), ui->paswd->text(), ui->server->text(), ui->port->text().toInt());
    connect(smtp, SIGNAL(status(QString)), this, SLOT(mailSent(QString)));

    if( !files.isEmpty() )
        smtp->sendMail(ui->uname->text(), ui->rcpt->text() , ui->subject->text(),ui->msg->toPlainText(), files );
    else
        smtp->sendMail(ui->uname->text(), ui->rcpt->text() , ui->subject->text(),ui->msg->toPlainText());
}

void MainWindow::mailSent(QString status)
{
    if(status == "Message sent")
        QMessageBox::warning( 0, tr( "Qt Simple SMTP client" ), tr( "Message sent!\n\n" ) );
}

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

and mainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "smtp.h"
#include <QtWidgets/QMessageBox>
#include <QFileDialog>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:
    void sendMail();
    void mailSent(QString);
    void browse();
    void test();

private:
    Ui::MainWindow *ui;
    QStringList files;

};

#endif // MAINWINDOW_H

The smtp class is as shown here, I haven't touch anything

https://github.com/xcoder123/SimpleSmtp_SSL_QT5/blob/master/smtp_attachements/smtp.h https://github.com/xcoder123/SimpleSmtp_SSL_QT5/blob/master/smtp_attachements/smtp.cpp

Any one know why it's not working when running in a thread using future watcher.

Thanks Haris

解决方案

The problem is that QtConcurrent::run just runs a single function, in your case sendMail, in a separate thread. As soon as this function returns, the thread is not active anymore and the slots in Smtp will not be called.

Either change your function sendMail to not return until the mail is sent or use a QThread and move Smtp* stmp to it using QObject::moveToThread.

这篇关于QT未来观察员与邮件客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆