05-15-2012 05:01 PM
I'm new to Blackberry 10 development and I'm trying to load an image from the web into a ImageView control on a ListView.
My QML for the ListView looks like this:
ListView {
id: titlesListView
objectName: "titlesListView"
listItemComponents: [
ListItemComponent {
type: "item"
Container {
layout: StackLayout {
layoutDirection: LayoutDirection.LeftToRight
}
ImageView {
imageSource: ListItemData.boxArt
}
Label {
text: ListItemData.title
textStyle {
base: SystemDefaults.TextStyles.TitleText
fontWeight: FontWeight.Normal
}
}
}
}
]
}Then I use a GroupDataModel class to provide the data:
GroupDataModel *model = new GroupDataModel(QStringList() << "title" << "boxArt"); QVariantMap map; map["title"] = "Test Title"; map["boxArt"] = "http://www.foo.com/bar.jpg"; model->insert(map); ListView *listView = findControl<ListView>("titlesListView"); listView->setDataModel(model);
At runtime I am getting this error in the logs:
Unsupported scheme (http) used in url
Does this really mean that it's not possible to load an image from a URL or am I going about it in the wrong way?
Solved! Go to Solution.
05-16-2012 11:20 PM
05-17-2012 06:24 AM
from my discussions at BB10Jam I know that this is work-in-progress and a sample app will follow soon.
It's important for me, too to get Images from URL. In my case a JSON of Conference-Speakers contains the Image URL's of the Speakers I want to display in a List.
05-21-2012 08:22 AM
Ekke is right, Cascades currently does not support loading images from URL. It's a very reasonable feature but we just didn't have time to put it in yet.
It should be pretty simple to build your own class to do this for now, like Peter9477 suggests.
Cheers!
Anders
05-21-2012 10:18 AM
With standard qml you can use Qt.resolvedUrl ( ) http://doc-snapshot.qt-project.org/4.8/qml-qt.html
And in Qt c++ use QUrl http://qt-project.org/doc/qt-4.8/QUrl.html
Good Luck!
Jon
05-22-2012 03:17 AM
There is some nice code here that shows you how to download data from a URL and load it into a QPixmap. It should be easy enough to wrap it up in a class to do what you want.
05-22-2012 05:03 PM
OK I was finally able to get this working.
Here's some sample code to show how it was done:
App::App()
{
//QNetworkAccessManager m_manager;
connect(&m_manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));
}
void App::setImage(const QString &url)
{
m_manager.get(QNetworkRequest(QUrl(url)));
}
void App::replyFinished(QNetworkReply* reply)
{
QByteArray data = reply->readAll();
QImage qimage;
qimage.loadFromData(data);
int width = qimage.width();
int height = qimage.height();
PixelBufferData pixelBuffer = PixelBufferData(PixelBufferData::RGBX, width, height, width, qimage.bits());
Image image = Image(pixelBuffer);
m_imageView->setImage(image);
}
06-08-2012 02:03 PM
How did you bind the image to the ListItemComponent?
07-03-2012 05:35 AM - edited 07-03-2012 05:36 AM
Hi,
I used same code, it can load image successful, but some surprised happened, when i load the below image by the follow code, it display the image which change blue to red, and the background change the black but actually it is black. please see the attches.
void CascadeImage::handleNetworkData(QNetworkReply *reply)
{
if (!reply->error()) {
// Let's get ALL the data
const QByteArray response(reply->readAll());
QImage QImg;
QImg.loadFromData(response);
if (!QImg.isNull())
{
PixelBufferData pbd(PixelBufferData::RGBX, QImg.width(), QImg.height(), QImg.width(), QImg.bits());
imageView->setImage(Image(pbd));
}
}
// Cleanup
reply->deleteLater();
}
Here is the image url:
http://img.sc115.com/uploads/png/110125/2011012512
07-05-2012 11:15 AM
I'd love to see how this is done. I'm new to C++/QML so at the moment I don't quite understand how retrieving the image in C++ and binding to the datamodel/QML works.
It would be a very good candidate for a sample application, because it is such a common requirement.