Qt 5.6 retrieving QObject sender



  • My project works fine with Qt 5.5.1 msvc 2013 problems occured when I switched
    to Qt 5.6.
    Here is simplified code (only not working part) for downloading file from ftp:

    void fun()
    {
    QNetworkAccessManager qnam; // in project it is class member
    QNetworkRequest request; // also class member
    request.setAttribute(QNetworkRequest::User, reqIndex); // lets say reqIndex = 0
    QNetworkReply* pReply = qnam.get(request); // also class member
    connect(pReply, SIGNAL(finished()), this, SLOT(onFinished()));
    connect(pReply, SIGNAL(downloadProgress(qint64,qint64)), this,
    SLOT(onDownloadProgress(qint64, qint64)));
    }
    
    void onFinished()
    {
    QNetworkReply* pCallerReply =
    dynamic_cast<QNetworkReply*>(QObject::sender());
    // pCallerReply is not nullptr
    int itemIndex = pCallerReply->request().attribute(QNetworkRequest::User, QVariant(-1)).toInt(&convertStatus;);
    // in Qt 5.6 itemIndex is always default QVariant(-1), convertStatus is true
    }
    
    // here is the same as onFinished()
    void onDownloadProgress(qint64, qint64)
    {
    QNetworkReply* pCallerReply =
    dynamic_cast<QNetworkReply*>(QObject::sender());
    // pCallerReply is not nullptr
    int itemIndex = pCallerReply->request().attribute(QNetworkRequest::User, QVariant(-1)).toInt(&convertStatus;);
    // in Qt 5.6 itemIndex is always default QVariant(-1), convertStatus is true
    }
    

    What could be a reason of this behavior? What can I do with it?


  • Lifetime Qt Champion

    Hi,

    The fact that you get -1 and convertStatus true just indicates that you are likely getting the default value.

    Can you create a minimal compilable example that shows that behavior ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.