查看完整版本: [-- 哪位大牛知道! qml里id可以做为参数传递吗? --]

QTCN开发网 -> Qt QML开发 -> 哪位大牛知道! qml里id可以做为参数传递吗? [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

qiuxx520 2017-02-09 10:41

哪位大牛知道! qml里id可以做为参数传递吗?

[attachment=16430]

如上图,动态创建按钮,每创建一次,需要给按钮分配一个唯一的ID,需要如何实现?

多肉生物 2017-02-12 12:59
你的问题等价于,如何使用随机函数修改一个QML对象的属性。

多肉生物 2017-02-12 13:02
继续回答。
//main.cpp
#include <QGuiApplication>

#include <QtQml>//想读、写qml对象的属性你就得包含它。


int main(int argc, char *argv[])
{
    //QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);

    qmlRegisterType<Myclass>("myclass.namespace", 1, 0, "Myclass");//用以在QML中实用C++类的实例。当然这不是唯一方法,推荐使用这种方法,因为它会让你的C++实例看起来像QML原生的。
    QQmlEngine engine;
    QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:///main.qml")));
    QObject *object = component.create();
   QObject *rect = object->findChild<QObject*>("your_object_name");
    if(rect)
    {   qDebug()<<"find the object";
        rect->setProperty("text", "yourid");
    }
    return app.exec();
}
//main.qml
import QtQuick 2.7
import QtQuick.Controls 2.0 //2.0 没有tableview
import QtQuick.Layouts 1.0
import QtQuick.Window 2.1
ApplicationWindow {
    visible: true
    width: 640
    height: 480
Myclass
    {
        id:myclass
        onBegin : doSomething()
    }
Rectangle
    {   //anchors.fill: parent

        width: 640
        height: 480
        id: display
        Label{objectName: "your_object_name"}

    }


查看完整版本: [-- 哪位大牛知道! qml里id可以做为参数传递吗? --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled