Set Image Source on ImageView BlackBerry

2019-09-06 10:56发布

How to set imageSource on ImageView as url?

   ImageView {
               imageSource: "http://myrrix.com/wp-content/uploads/2012/06/stackoverflow.png"
             }

This is not working, I can only set the image as "asset:enter code here///images/myimage.png" on the ImageView. How to set the source to be from URL?

1条回答
在下西门庆
2楼-- · 2019-09-06 11:47

You can not directly load image from web. You need to make a network request using QNetworkRequest, QNetworkAccessManager, and QNetworkReply classes & on getting reply load that QByteArray in ImageView.

QNetworkAccessManager* netManager = new QNetworkAccessManager();
if (netManager) {
    QUrl url(ImageUrl);
    QNetworkRequest networkRequest(url);
    QNetworkReply* networkReply = netManager->get(networkRequest);
    connect(networkReply, SIGNAL(finished()), this, SLOT(onReply()));
}

& in onReply() slot you can load image:

void App::onReply(QNetworkReply* reply) {
    if (reply->error() != QNetworkReply::NoError) {
        qDebug() << "Image not available or any error";
        return;
    }
    Image image = Image(reply->readAll());
    imageView->setImage(image);
}    
查看更多
登录 后发表回答