如何将imageSource
上的ImageView
设置为网址?
ImageView {
imageSource: "http://myrrix.com/wp-content/uploads/2012/06/stackoverflow.png"
}
这不起作用,我只能在ImageView上将图像设置为“asset:enter code here
/// images / myimage.png”。如何将来源设置为来自URL?
答案 0 :(得分:4)
您无法直接从网络加载图片。您需要使用QNetworkRequest,QNetworkAccessManager和QNetworkReply类来生成网络请求。获取回复加载ImageView中的QByteArray。
QNetworkAccessManager* netManager = new QNetworkAccessManager();
if (netManager) {
QUrl url(ImageUrl);
QNetworkRequest networkRequest(url);
QNetworkReply* networkReply = netManager->get(networkRequest);
connect(networkReply, SIGNAL(finished()), this, SLOT(onReply()));
}
&安培;在onReply()插槽中,您可以加载图像:
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);
}