Qt QThread中的Qt信号和插槽

时间:2012-11-03 17:43:19

标签: qt post upload qthread

我目前在Qt线程上遇到问题。

我必须上传QThread中的文件列表,但显然一个上传不起作用和/或我的插槽永远不会被调用。如果我把这些方法排除在线程之外就可以完美地运行。

这是run()方法:

void UploadThread::run()
{
    for (int i = 0; i < Window::_listUpload.size(); i++) {
        qDebug() << Window::_listUpload[i].getPath();
        this->sendFile(Window::_listUpload[i].getPath());
    }
}

这是sendFile()方法:

void UploadThread::sendFile(const QString & path)
{
    QFreeDesktopMime mime;
    QNetworkAccessManager *manager = new QNetworkAccessManager;
    QFileInfo fInfo(path);
    QNetworkRequest request(QUrl("http://my-url/"));
    QNetworkReply *reply;

    QString bound = "---------------------------723690991551375881941828858";
    QByteArray data(QString("--"+bound+"\r\n").toAscii());
    data += "Content-Disposition: form-data; name=\"action\"\r\n\r\n";
    data += "\r\n";
    data += QString("--" + bound + "\r\n").toAscii();
    data += "Content-Disposition: form-data; name=\"file\"; filename=\""+fInfo.fileName()+"\"\r\n";
    data += "Content-Type: "+mime.fromFile(path)+"\r\n\r\n";

    QFile file(fInfo.absoluteFilePath());
    file.open(QIODevice::ReadOnly);
    data += file.readAll();
    data += "\r\n";
    data += QString("--" + bound + "\r\n").toAscii();
    data += QString("--" + bound + "\r\n").toAscii();
    data += "Content-Disposition: form-data; name=\"desc\"\r\n\r\n";
    data += "Description for my image here :)\r\n";
    data += "\r\n";
    request.setRawHeader(QString("Accept-Encoding").toAscii(), QString("gzip,deflate").toAscii());
    request.setRawHeader(QString("Content-Type").toAscii(),QString("multipart/form-data; boundary=" + bound).toAscii());
    request.setRawHeader(QString("Content-Length").toAscii(), QString::number(data.length()).toAscii());

    reply = manager->post(request, data);

    QObject::connect(reply, SIGNAL(uploadProgress(qint64,qint64)), currentThread(), SLOT(receiveUploadProgress(qint64, qint64)));
    QObject::connect(manager, SIGNAL(finished(QNetworkReply*)), currentThread(), SLOT(uploadFinished(QNetworkReply*)));
}

这是我的插槽:

void UploadThread::uploadFinished(QNetworkReply *reply)
{
    _isFinished = true;
}

void UploadThread::receiveUploadProgress(qint64 bytesSent, qint64 bytesTotal)
{
    qDebug() << bytesSent << " " << bytesTotal;
}

您是否在我的代码中发现了问题? 谢谢。

2 个答案:

答案 0 :(得分:2)

好的,我明白了。

对于每次上传,我需要使用exec()启动QEventLoop,并且在上传结束时我必须使用exit()才能完成QEventLoop。它现在就像一个魅力。

答案 1 :(得分:1)

您似乎在sendFile()方法中调用了QObject::connect()

QObject::connect()使用slot方法连接信号。可以使用emit signalName()等方式触发信号。

在您的示例中,您的UploadThread标头应如下所示:

class UploadThread: public QThread {
    ...
signals:
    void uploadProgress(int sent, int total);
    void uploadFinished(QNetworkReply *reply);
}

不要实现信号,它们由Qt自动实现。

在sendFile()中,只需调用它们:

...
emit uploadProgress(sent, total);
...

在你的主线程(= gui线程)中你应该有一个具有处理信号的槽的类(你当前的uploadFinished()和recieveUploadProgress()方法)。如果要更新GUI,重要的是它们在GUI线程中执行。

最后你必须连接它们(连接并不意味着调用它们)并开始上传。

UploadThread *uploadThread = new UploadThread();
connect(uploadThread, SIGNAL(uploadProgress(...)), objectInGuiThread, SLOT(actualUploadProgressSlot(...)));

uploadThread.start()

有关如何使用它们的更多信息,请查看Qt的Signal & Slot文档。