Dynamically set imageSource in ImageView Blackberry 10

跟風遠走 提交于 2019-12-01 11:15:06

So you've defined your slot with QVariant parameter however trying to emit it with QByteArray type which is returned by ImageConverter::encode() call.

Try to change that part of your code to this and give it a go:

QByteArray byteArray = bb::utility::ImageConverter::encode(QUrl(QDir::currentPath() + "/shared/camera/img.png"), imageData, 75);
QVariant image(byteArray);
emit imageLoaded(image);

Also, double check that everywhere where you declare/define this pair of signal/slot you're specified exactly the same parameter notation (ie const QVariant& in case of imageLoaded() signal and so on)

Try this sample code and implement by re-altering in your project

1.QML FILE

import bb.cascades 1.0

Page {
    content: Container {
        ListView {
            dataModel: _app.model

            function itemType (data, indexPath) {
                return data["type"];
            }
            listItemComponents: [

            ]
        }
    }
}

2.HPP FILE

#ifndef APP_HPP
#define APP_HPP

#include <QtCore/QObject>
#include <QtNetwork/QNetworkAccessManager>

#include <bb/cascades/QListDataModel>

class App: public QObject
{
    Q_OBJECT

    Q_PROPERTY(bb::cascades::DataModel *model READ model NOTIFY modelChanged)

public:
    App();
    bb::cascades::DataModel * model() const;

Q_SIGNALS:

    void modelChanged();

private Q_SLOTS:

    void handleNetworkData(QNetworkReply *reply);

private:

    mutable bb::cascades::QMapListDataModel *dataModel;


    QNetworkAccessManager networkManager;

};

#endif // APP_HPP

3.CPP FILE

#include <bb/cascades/Page>
#include <bb/cascades/QmlDocument>
#include <bb/data/JsonDataAccess>

#include "App.hpp"

using namespace bb::cascades;
using namespace bb::data;

App::App() :
{

    // Load up the QML for our view
    QmlDocument *qml = QmlDocument::create("view.qml");

    // Provide a reference to this class
    qml->setContextProperty("_app", this);

    // Create the root node of this view
    Page *view = qml->createRootNode<Page>();

    dataModel = new QMapListDataModel();

    // Hook this signal so we can respond to network replies
    connect(&networkManager, SIGNAL(finished(QNetworkReply *)), this,
            SLOT(handleNetworkData(QNetworkReply *)));

    // Do the request
    QUrl url = "http://jsonservice.com/news/headlines/";
    networkManager.get(QNetworkRequest(url));

}

DataModel * App::model() const
{
    return dataModel;
}

void App::handleNetworkData(QNetworkReply *reply)
{

    if (!reply->error()) {

        const QByteArray response(reply->readAll());

        JsonDataAccess jda;
        QVariantMap results = jda.loadFromBuffer(response).toMap();

        // Get the relevant parts we want from the JSON
        QVariantList posts = results["data"].toMap()["children"].toList();

        Q_FOREACH(QVariant post, posts) {


            dataModel->append(post);

        }

    }

    // Cleanup
    reply->deleteLater();

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!