我正在处理一个应用程序的一部分,该应用程序在图像旁边显示一个新闻片段(如果图像存在)。此新闻列表是SegmentedControl“选项卡”页面的一部分,可以根据用户的要求隐藏和重新显示。当图像通过网络加载时,它们由ImageTracker处理,并且只在完全加载后设置为属性。
我遇到的问题是,当这些图像第一次加载时,如果我导航到另一个SegmentControl“选项卡”然后导航回“新闻”选项卡,这些图像就会失败。通过将非活动容器的可见性设置为false并将活动容器的可见性设置为true来完成隐藏操作。
这是我定义数据绑定的方式: 列表显示 { id:lstShowNews objectName:“lstShowNews”
listItemComponents: [
ListItemComponent {
type: "item"
Detail_Tabs_NewsCell {
newsHeadline: ListItemData.headline
newsBlurbPreview: ListItemData.content
newsTimeAdded: ListItemData.time
newsImageVisible: ListItemData.hasImage
newsImage: ListItemData.image
}
}
]
}
自定义列表项的别名/属性:
property alias newsImageVisible: newsImage.visible
property alias newsHeadline: newsTitle.text
property alias newsImage: newsImage.image
property string newsBlurbPreview: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam condimentum tellus vel magna dapibus aliquam. Nunc iaculis metus ut arcu tempus semper."
property string newsTimeAdded: "News Source - 3 hours ago"
处理列表加载的C ++代码部分:
/*!
* Handles loading for each tab page. Enables data to only be loaded on demand.
* @param index - The new selected index on the SegmentedControl
*/
void MyClass::onSegmentChanged(int index) {
if(index == 2) {
if(!mNewsLoaded) {
// Load the news
NewsRequester* news = new NewsRequester(mId);
connect(news, SIGNAL(requestDone(QList<NewsItem*>)), this, SLOT(onNewsReceieved(QList<NewsItem*>)));
indNews->start(); // Activity indicator
news->requestNews();
}
else {
// Caching functions
}
}
else {
// Stuff for other tabs
}
}
void MyClass::onNewsReceieved(QList<NewsItem*> newsItems) {
qDebug() << "Received news reply";
indNews->stop();
indNews->setVisible(false);
mNewsItems = newsItems;
setNewsModel();
}
void MyClass::setNewsModel() {
GroupDataModel* newsModel = new GroupDataModel(QStringList() << "news");
newsModel->setGrouping(ItemGrouping::None);
// Move the list items to the data model
for(int i = 0; i < mNewsItems.size(); i++) {
mNewsItems.at(i)->loadImage();
newsModel->insert(mNewsItems.at(i));
}
ListView* lstNews = root->findChild<ListView*>("lstShowNews");
lstNews->setDataModel(newsModel);
}
用于跟踪图像并确保在传递到列表之前正确加载图像的功能:
/*!
* Tracks the status of the current local image and displays when ready
*/
void NewsItem::trackImage() {
// Load cascades image
mTracker = new ImageTracker(mLocalImage);
connect(mTracker, SIGNAL(stateChanged (bb::cascades::ResourceTracker::State)),
this, SLOT(onStateChanged (bb::cascades::ResourceTracker::State)));
}
/*!
* Passes the downloaded image's path to the list
*/
void NewsItem::onImageRetrieved(QString path) {
//qDebug() << "Received image " << path;
mLocalImage = QUrl(path);
trackImage();
}
/*!
* Display the image if it's been fully loaded in memory
*/
void NewsItem::onStateChanged(bb::cascades::ResourceTracker::State state) {
if(state == bb::cascades::ResourceTracker::Loaded) {
qDebug() << "Image loaded";
mLocalCascadesImage = mTracker->image();
emit localImageChanged();
}
}
mLocalCascadesImage的属性:
Q_PROPERTY(bb::cascades::Image image READ localImage NOTIFY localImageChanged)
为什么ListView会以这种方式行为不端?感谢。
答案 0 :(得分:0)
自己解决了。事实证明,我还需要公开newsImage.imageSource
属性并将其传递给图像的本地缓存版本(如果可用)。虽然image
属性可以方便地确保图像在下载后立即显示,但imageSource
属性需要用于本地可用文件。