Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

C++ and QML: finding children



  • @ //main.qml
    #include <QtGui/QApplication>
    #include "qmlapplicationviewer.h"
    #include "funzioni.h"
    #include <QDeclarativeContext>
    #include <QGraphicsObject>
    #include <QDeclarativeEngine>

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

    Funzioni funz;
    QDeclarativeView view;
    view.rootContext()->setContextProperty("myObject", &funz);
    view.setSource(QUrl::fromLocalFile&#40;"qml/fileTrenner2/main.qml"&#41;&#41;;
    
    view.setWindowFlags(Qt::FramelessWindowHint);
    view.setWindowTitle("File Trenner");
    view.move(600,20);
    QObject::connect(view.engine(),SIGNAL(quit()),QCoreApplication::instance(),SLOT(quit()));
    
    view.show();
    
    return app.exec&#40;&#41;;
    

    }
    @

    @ //funzioni.cpp
    ...
    QDeclarativeView view;
    view.setSource(QUrl::fromLocalFile("qml/fileTrenner2/main.qml"));
    QObject *finestraPrincipale = view.rootObject();
    QObject input = finestraPrincipale->findChild<QObject>("componenteInput");
    ...
    @
    @ //main.qml
    Retangle {
    width: 500; height: 500
    id: finestraPrincipale
    ...
    Text {
    y: 79; x: -20
    rotation: -90
    text: "Nome del file di input"
    }
    Text {
    x: 18
    y: -10
    z: 1
    color: "orange"
    id: input
    objectName: "componenteInput"
    text: "input"
    rotation: -90
    }
    Text {
    y: 75; x:0
    rotation: -90
    text: "Nome del file di output"
    }
    Text {
    x: 38; y: -15; z: 1
    id: output
    objectName: "componenteOutput"
    color: "blue"
    rotation: -90
    text: "output"
    }
    ...
    }
    @

    where is the problem? i can not set the properties to the my rootObject's child... why???? naturally, finestraPrincipale in main.qml has other children



  • This should work. Did you try to debug or are you getting any compile time errors.

    finestraPrincipale => does this get valid pointer?? after the statement
    @QObject *finestraPrincipale = view.rootObject();@



  • boh! how can i verify it?



  • [quote author="Vijay Bhaska Reddy" date="1308470508"]This should work. Did you try to debug or are you getting any compile time errors.

    finestraPrincipale => does this get valid pointer?? after the statement
    @QObject *finestraPrincipale = view.rootObject();@

    [/quote]
    If it wasn't a valid pointer (i.e. 0) he'd get a SIGSEV anyway on the findChild method.

    @spode: You could try to use the objectName() method to find out whether you actually have the right object


Log in to reply