qtqtnetworkqiodevice

"QIODevice device not open" only occurs when build in Release


I am using QNetworkAccessManager to transfer file. My program runs without any problem when it was built in Debug. But there is a strange problem with the problem when built in Release: QIODevice::read (QFile, "my file path"): device not open

What is wrong in my code? Thanks for your helping!

QHttpMultiPart* getPacket(QString imgPath)
{
    QHttpMultiPart *pMultiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);

    QFile *imgFile = new QFile(imgPath);
    imgFile->setParent(pMultiPart);
    Q_ASSERT(imgFile->open(QIODevice::ReadWrite));

    QHttpPart imagePart;
    imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
    imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"img\"; filename=\"target.jpg\""));
    imagePart.setBodyDevice(imgFile);
    pMultiPart->append(imagePart);

    return pMultiPart;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    JTest testObj;
    QNetworkAccessManager manager;

    QHttpMultiPart *pHMP = getPacket("F:/Temp/1.jpg");
    QNetworkReply *pNR = manager.put(QNetworkRequest(QUrl("http://192.168.11.102:8080/Test")), pHMP);
    QObject::connect( pNR, SIGNAL(finished()), &testObj, SLOT(handleTransmissionFinished()) );

    return a.exec();
}

Solution

  • Q_ASSERT() is useful for testing pre- and post-conditions during development. It does nothing if QT_NO_DEBUG was defined during compilation.

    Q_ASSERT will not be evaluated in Release, so your file will not be opened.