• 3393阅读
  • 2回复

[讨论]哪位大牛知道! qml里id可以做为参数传递吗? [复制链接]

上一主题 下一主题
离线qiuxx520
 

只看楼主 倒序阅读 楼主  发表于: 2017-02-09


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

只看该作者 1楼 发表于: 2017-02-12
你的问题等价于,如何使用随机函数修改一个QML对象的属性。
离线多肉生物

只看该作者 2楼 发表于: 2017-02-12
继续回答。
//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"}

    }
快速回复
限100 字节
 
上一个 下一个