如何在单例中创建 QML 对象? [英] How to create QML object within a singleton?

查看:61
本文介绍了如何在单例中创建 QML 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 MyQMLObject.qml 下定义了一个 QML 对象.这个 QML 文件看起来像这样:

I have defined a QML object under MyQMLObject.qml. This QML file looks like this:

import QtQuick 2.4

Item {
  id: rootItem
  implicitWidth: LayoutUtils.maxImplicitWidth(children)
  implicitHeight: LayoutUtils.maxImplicitHeight(children)

  Text {
    id: text1
  }
  Text {
    id: text2
  }
  // ...
  Text {
    id: textN
  }
}

文本在应用程序启动时动态添加.对于每种语言,添加了不同的文本,rootItem 的宽度因所选语言而异.我想以某种方式在应用程序启动时只创建一次 MyQMLObject 甚至没有将其可视化并将其实际宽度保存在一个单例中,例如这样我就可以在整个代码中重用该值而无需创建 MyQMLObject 不止一次.我怎么能做到这一点?

The text is added dynamically when the application starts. For each language different text is added, there for the width of the rootItem varies by the chosen language. I would like to somehow create MyQMLObject only once at application startup without even visualizing it and save its actual width in a singleton for example so I can reuse that value throughout my code without creating MyQMLObject more then once. How could I achieve this?

现在我有一个单例 QML 文件,其中包含一个 QtObject,其中包含一些常量值.我可以以某种方式在这个单例 QtObject 中创建一个 MyQMLObject 的实例吗?

Right now I have a singleton QML file, which holds a QtObject which contains some constant values. Can I somehow create an instance of MyQMLObject within this singleton QtObject?

我的单例 Style.qml 看起来像这样:

My singleton Style.qml looks like this:

pragma Singleton

import QtQuick 2.4

QtObject {
  readonly property int maxWidth: 400
  // ...
}

推荐答案

首先,如果可能,您可以使用 Column 而不是手动计算最大宽度:

Firstly, if possible, you could use a Column instead of manually calculating the maximum width:

MyQMLObject.qml

import QtQuick 2.4

Column {
    Text {
        text: "blah"
    }
    Text {
        text: "blahblah"
    }
}

您可以使用动态对象创建来创建临时对象项:

You can use dynamic object creation to create the temporary Column item:

Style.qml

pragma Singleton

import QtQuick 2.4

QtObject {
    readonly property int maxWidth: {
        var component = Qt.createComponent("qrc:/MyQMLObject.qml");
        if (component.status === Component.Error) {
            console.error(component.errorString());
            return 0;
        }

        return component.createObject().width;
    }
}

ma​​in.qml

import QtQuick 2.5
import QtQuick.Window 2.2

import App 1.0

Window {
    visible: true

    Component.onCompleted: print(Style.maxWidth)
}

然后,注册单身人士:

ma​​in.cpp

#include <QtGui>
#include <QtQml>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    qmlRegisterSingletonType(QUrl("qrc:///Style.qml"), "App", 1, 0, "Style");

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

<小时>

但是,请注意,可以通过从 C++ 计算最大宽度来改进此方法,从而无需构建项目只是为了将其丢弃.处理这个例子:

#include <QtGui>
#include <QtQml>

class Style : public QObject
{
    Q_OBJECT
    Q_PROPERTY(int maxWidth READ maxWidth CONSTANT)

public:
    Style(QObject* parent = 0) :
        QObject(parent),
        mMaxWidth(0)
    {
        QFontMetrics fontMetrics(qApp->font());
        // Here is where you'd fetch the text...
        QStringList dummyText;
        dummyText << "blah" << "blahblah";
        foreach (const QString &string, dummyText) {
            const int width = fontMetrics.boundingRect(string).width();
            if (width > mMaxWidth)
                mMaxWidth = width;
        }
    }

    int maxWidth() const
    {
        return mMaxWidth;
    }

private:
    int mMaxWidth;
};

static QObject *singletonTypeProvider(QQmlEngine *, QJSEngine *)
{

    Style *style = new Style();
    return style;
}

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    qmlRegisterSingletonType<Style>("App", 1, 0, "Style", singletonTypeProvider);

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

#include "main.moc"

它使用 QFontMetrics 来计算 width.

main.qml 保持不变.

这篇关于如何在单例中创建 QML 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆