Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. C++ and QML: finding children
Qt 6.11 is out! See what's new in the release blog

C++ and QML: finding children

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 7.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    spode
    wrote on last edited by
    #1

    @ //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

    1 Reply Last reply
    0
    • T Offline
      T Offline
      thisisbhaskar
      wrote on last edited by
      #2

      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();@

      1 Reply Last reply
      0
      • S Offline
        S Offline
        spode
        wrote on last edited by
        #3

        boh! how can i verify it?

        1 Reply Last reply
        0
        • L Offline
          L Offline
          loladiro
          wrote on last edited by
          #4

          [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

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved