Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
pepe_ugalde
Posts: 64
Registered: ‎09-10-2012
My Device: Developer
Accepted Solution

Change ImageView source in C++

Hi guys

 

I'm trying to change an ImageView's source in C++

 

First I bind QML and C++, then I change the source as in the Images Reference

 

toplogo = root->findChild<ImageView*>("toplogo");

toplogo->setImageSource(QUrl("assets/images/logo.png"));


The image does exist, however the program crashes and the log says that there is no source.

I have tried logging QUrl errors for different URL combinations of the same image:

 

qDebug() << "ERROR URL:" << QUrl("asset:///images/logoideas.png").errorString();
qDebug() << "ERROR URL:" << QUrl("asset:///logoideas.png").errorString();
qDebug() << "ERROR URL:" << QUrl("assets/images/logoideas.png").errorString();
qDebug() << "ERROR URL:" << QUrl("assets/logoideas.png").errorString();
qDebug() << "ERROR URL:" << QUrl("images/logoideas.png").errorString();
qDebug() << "ERROR URL:" << QUrl("logoideas.png").errorString();
qDebug() << "ERROR URL:" << QUrl("assets:///images/logoideas.png").errorString();
qDebug() << "ERROR URL:" << QUrl("asset:///main.qml").errorString();

 

But the log always prints for all the attempts:

ERROR URL: "Invalid URL "asset:///images/logoideas.png": "


Any ideas would be appreciated.

 

Pepe

 

Developer
igosoft
Posts: 71
Registered: ‎09-19-2012
My Device: Z10 very soon

Re: Change ImageView source in C++

Hi

 

Please provide qml and more c++ code.

 

Did you try this

 

Container {
objectName: "mainContainer"

 

  ImageView{

    objectName: "imageView"

  }

 

}

 

 

QmlDocument* qml = QmlDocument::create("asset:///some.qml").parent(this);
Container* mMainContainer = qml->createRootObject<Container>();

ImageView *imageView= mMainContainer->findChild<ImageView*>("imageView")

 

if (imageView){

  imageView->setImage(Image("asset:///images/logo.png"));
}
Developer
pepe_ugalde
Posts: 64
Registered: ‎09-10-2012
My Device: Developer

Re: Change ImageView source in C++

Thanks for helping.

 

This is part of the QML:

 

Page {
    id: pgMain
    
    content: Container {
        id: mainContainer
        
        //////Content
        Container {
            id: containerContent
            ...
            //////Title Logo
            Container {
                id: titleContainer
                ...
                
                ImageView {
                    id: toplogo
                    scalingMethod: ScalingMethod.AspectFit
                    imageSource: "asset:///images/logo1.png"
                }
            }
        ...
}

 

The Image view's id is toplogo

The image source is set and the image "logo1.png" does load and is displayed correctly

 

- - - - -
This is the C++ code:

 

IdeasContainer::IdeasContainer(bb::cascades::Application *app)
: QObject(app)
{
    //check SIM
    int simState = SimCardInfo().state();

    if (simState == SimCardState::ReadError || simState == SimCardState::NotDetected) {
        showNoSIMPermissionAlert();
    } else if (simState != SimCardState::Ready) {
        showNoSIMAlert();
    } else {

        QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
        qml->setContextProperty("app", this);
        AbstractPane *root = qml->createRootObject<AbstractPane>();
        app->setScene(root);
        {

            toplogo = root->findChild<ImageView*>("toplogo");

            QString ciso = getCountryISO();

            //change logo if SIM country is Brazil
            if (ciso == "BR") {
                toplogo->setImageSource(QUrl("asset:///images/logoideas.png"));  <- APP CRASHES HERE
            }
            ...
        }
    }
}

 

The problem is the QUrl. No matter what url I give it, it says there is no source.

Developer
igosoft
Posts: 71
Registered: ‎09-19-2012
My Device: Z10 very soon

Re: Change ImageView source in C++

[ Edited ]

 

 

You should use objectName property (not id property)> Please see my post above :smileyhappy:

 

in QML

 

 ImageView {
                    objectName: "toplogo"

}

 

 toplogo->setImageSource(QUrl("asset:///images/logoideas.png"));  <- APP CRASHES HERE

 

 

It crashes here probably topLogo is NULL

 

if (topLogo){

 toplogo->setImageSource(QUrl("asset:///images/logoideas.png"));  

}

Developer
pepe_ugalde
Posts: 64
Registered: ‎09-10-2012
My Device: Developer

Re: Change ImageView source in C++

Well... this is weird. If I run this:

 

qDebug() << "ERROR URL:" << QUrl("assets/images/logoideias.png").errorString();
if (toplogo) {
    toplogo->setImageSource(QUrl("assets/images/logoideias.png"));
}

 

the log prints:

 

ERROR URL: "Invalid URL "assets/images/logoideias.png": "

 

but now it does change the  image, so the image exists but the URL format is invalid.


I think you were right about the objectName because I haven't changed anything else.

The log error that said there was no source was misleading, it made me think there was no image.

 

Thanks!

 

Pepe

Developer
igosoft
Posts: 71
Registered: ‎09-19-2012
My Device: Z10 very soon

Re: Change ImageView source in C++

[ Edited ]

 

In my opinion below is wrong

 

toplogo->setImageSource(QUrl("assets/images/logoideias.png"));

 

Please check also following

 

  toplogo->setImageSource(QUrl("asset:///images/logoideias.png"));

or

  toplogo->setImageSource(QUrl("../assets/images/logoideias.png"));

 

This should work as well

 

if (imageView){

  imageView->setImage(Image(QUrl("asset:///images/logoideias.png")));
}

 

Developer
martin78
Posts: 27
Registered: ‎05-15-2012
My Device: Dev Alpha B

Re: Change ImageView source in C++

Yeah seems your error appears before your call to toplogo->setImageSource;

 

let me know what you find

 

Martin

------------------------------------------
Macadamian Technologies Inc.
http://www.macadamian.com/
Developer
pepe_ugalde
Posts: 64
Registered: ‎09-10-2012
My Device: Developer

Re: Change ImageView source in C++

The ones that work are:


toplogo->setImageSource(QUrl("assets/images/logoideias.png"));

and

toplogo->setImageSource(QUrl("asset:///images/logoideias.png"));

 

This one doesn't work:

toplogo->setImageSource(QUrl("../assets/images/logoideias.png"));