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
New Contributor
dd_123
Posts: 6
Registered: ‎03-12-2012
My Device: ndk
Accepted Solution

Cascades: textStyle.color ignored for TextArea created at run-time

I am attempting to set the text color of a TextArea created at run-time. My main.xml inserts a static MyWidget control like this:

import bb.cascades 1.0

Page {
    content: Container {
        objectName: "topLevel"
        MyWidget {}
    }
}

MyWidget.qml looks like this:

Container {
    TextArea {
        editable: false
        text: "some text"
        textStyle.color: Color.create ("#ffff0000")
    }
}

 And I dynamically add a MyWidget at run-time like this:

QDeclarativeEngine engine;
QDeclarativeComponent component(&engine, QUrl::fromLocalFile("app/native/assets/MyWidget.qml"));
QObject *object = component.create();
Container *topLevel = root->findChild<Container*>("topLevel");
topLevel->add(qobject_cast<Control *>(object));

 

So I have two MyWidget items in the app - one created in the QML file and one added dynamically. The object created in QML has the text color correctly set to red, but the one created dynamically fails to set its text color and uses the default (black). You can see how this looks in a screengrab from the simulator http://imgur.com/PrSk7

 

I get an error at run-time in the console that says:

file:///accounts/1000/appdata/[...]/app/native/assets/MyWidget.qml:21: ReferenceError: Can't find variable: Color

 

I've tried various other things like setting the color to various built-in colors ("Color.Red")  and setting the text color at run-time, none of which work.

 

So am i setting the text color incorrectly, or is this a bug in the platform?

Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: Cascades: textStyle.color ignored for TextArea created at run-time

If you look at the QML samples, they appear more like this:

        TextArea {
            textStyle {
                color: Color.create("#e0e0e0")
            }
        }

 


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
New Contributor
dd_123
Posts: 6
Registered: ‎03-12-2012
My Device: ndk

Re: Cascades: textStyle.color ignored for TextArea created at run-time

Thanks for the reply. That was one of the "other" things I'd mentioned that I'd tried. It generates the same error, "ReferenceError: Can't find variable: Color".

Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: Cascades: textStyle.color ignored for TextArea created at run-time

Have you been able to build any other Cascades apps yet, including the samples? That name should simply be available, without any special effort required. Seems to me like your Momentics install could be broken in some fashion, or the project isn't configured properly.

If you haven't already, can you try one of the samples such as StarshipSettings? It uses Color.create() the same way...

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
New Contributor
dd_123
Posts: 6
Registered: ‎03-12-2012
My Device: ndk

Re: Cascades: textStyle.color ignored for TextArea created at run-time

Yes, I just tried StarshipSettings and it works fine.

 

But StarshipSettings isn't doing the same thing as I've done. In that sample app, all of the objects are initialized from inside the QML file. That also works fine for me - the image I've posted shows one working text box (which was initialized from QML) and one broken text box (which was initialized using C++).

 

The only occurs when creating the object at run-time in C++, using QDeclarativeComponent::create() and QUrl::fromLocalFile().

Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: Cascades: textStyle.color ignored for TextArea created at run-time

Do you need to import bb.cascades inside the MyWidget.qml file? Or something similar maybe.

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
New Contributor
dd_123
Posts: 6
Registered: ‎03-12-2012
My Device: ndk

Re: Cascades: textStyle.color ignored for TextArea created at run-time

I have that in all my QML files already - I just omitted it for brevity.

BlackBerry Development Advisor (Retired)
smacmartin
Posts: 499
Registered: ‎05-07-2012
My Device: developer

Re: Cascades: textStyle.color ignored for TextArea created at run-time

I've been playing around with creating text boxes in various ways.

I'll try out yours tomorrow, but I suspect the issue is that QDeclarativeEngine is in the Qt namespace.

 

In my example, I added the text box to a container c1:

 

            QmlDocument* tbQml = QmlDocument::create("Mytextbox.qml");  // note: namespace bb::cascades
            Container* container = tbQml->createRootNode<Container>();
            if (container)
            {
                c1->add(container);
            }

 

The colour shows up as expected.

New Contributor
dd_123
Posts: 6
Registered: ‎03-12-2012
My Device: ndk

Re: Cascades: textStyle.color ignored for TextArea created at run-time

This works! Thanks very much.