Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to judge class object is null ?



  • TileItem::TileItem(QGraphicsItem *parent) : QGraphicsPixmapItem(parent)
    {
    
    }
    
    TileItem::TileItem(QString &surl)
    {
        setPixmapFormUrl(surl);
    }
    
    void TileItem::setPixmapFormUrl(QString surl)
    {
        QNetworkAccessManager *NAM = new QNetworkAccessManager;
        QNetworkRequest request;
        request.setUrl(QUrl(surl));
        NAM->get(request);
        connect(NAM, &QNetworkAccessManager::finished, [=](QNetworkReply *reply){
            QPixmap pixmap;
            pixmap.loadFromData(reply->readAll());
            //if(TileItem) 
                setPixmap(pixmap);//if it is deleted in parent, here will crash.
            NAM->deleteLater();
        });
    }
    


  • Not sure I understand the question...


  • Lifetime Qt Champion

    @sonichy hi,

    I would say that your design has issues. Creating a QNAM for each tile when you want to a remote image is rather wasteful.

    You should rather have a dedicated class that manages the retrieval of the images and setting them on the appropriate tile. That way you can also cancel the retrieval on tile deletion.


Log in to reply