简体   繁体   English

将TextStyle从c ++传递到QML

[英]Pass TextStyle from c++ to QML

I am having problems setting the TextStyle of a Label when using a c++ method, it always causes the app to crash. 使用c ++方法时,我在设置标签的TextStyle时遇到问题,它总是导致应用程序崩溃。

I have a c++ getStyle() method. 我有一个c ++ getStyle()方法。 I want to be able to call it like so (parameters removed to simplify code example): 我希望能够这样称呼(删除参数以简化代码示例):

Label {
    id: myLabel                        
    text: "test with style"
    textStyle.base: _App.getStyle();
}

The following does NOT work: 以下内容不起作用:

TextStyle ApplicationUI::getStyle() {
    TextStyle *blueStyle = new TextStyle(bb::cascades::SystemDefaults::TextStyles::smallText());
    blueStyle->setColor(Color::Blue);
    return *blueStyle;
}

Currently the only way I have been able to get it working is to pass the entire Label object into a method and set the style with c++. 目前,我能够使其工作的唯一方法是将整个Label对象传递给一个方法,并使用c ++设置样式。 This however makes the QML code more verbose leading to this: 但是,这使QML代码更加冗长,导致以下情况:

Label {
    id: myLabel                        
    text: "test with style"
    onCreationCompleted: {
        _App.setStyle(myLabel);
    }
}

C++ (Works) C ++(Works)

void ApplicationUI::setStyle(AbstractTextControl* label) {
    TextStyle *blueStyle = new TextStyle(bb::cascades::SystemDefaults::TextStyles::smallText());
    blueStyle->setColor(Color::Blue);

    label->textStyle()->setBase(*blueStyle);
}

Is there any way to pass the TextStyle directly to the QML Label without having to pass the Label object into the method? 有什么方法可以将TextStyle直接传递给QML Label,而不必将Label对象传递给方法吗?

After reading the documentation on text styles I would suggest you try something like this: 阅读有关文本样式文档后,建议您尝试如下操作:

Page {
    content: Container {
        attachedObjects: [
            TextStyleDefinition {
                id: myStyle
                base: _App.getStyle()
            }
        ]

        Label {
            text: "A label with some text."

            textStyle {
                base: myStyle.style
            }
        }
    } // end of Container
} // end of Page

I managed to solve the problem after some browsing of the various .h files and experimenting. 在浏览了各种.h文件并进行了尝试之后,我设法解决了该问题。

QML: QML:

Label {
    id: myLabel                        
    text: "test with style"
    textStyle.base: _App.getStyle();
}

C++: C ++:

QVariant ApplicationUI::getStyle() {
    TextStyleDefinition *textStyle = new TextStyleDefinition();
    textStyle->setColor(Color::Blue);

    QVariant style = textStyle->property("style");
    return style;
}

The trick is that when setting a Label's Style.base property from c++ it requires a TextStyle object, however when you set it from QML it expects a QVariant. 诀窍是,从c ++设置Label的Style.base属性时,它需要一个TextStyle对象,但是从QML设置时,它需要一个QVariant。 Using ->property("style") we can get the QVariant which QML expects. 使用->property("style")我们可以获得QML期望的QVariant。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM