Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to access properties of another file from main.qml. Qt- QML
Forum Update on Monday, May 27th 2025

How to access properties of another file from main.qml. Qt- QML

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 2.5k Views
  • 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.
  • H Offline
    H Offline
    Homer JS
    wrote on 18 Dec 2014, 20:49 last edited by
    #1

    Hi guys,

    I'm having problems with QML and C++. If someone could take a look at this piece of code and tell me what I am doing wrong. I 'm just trying to print "msg.author" on a window (main window) but everytime I try to access it from main.qml there's an error saying "msg is not defined". Thank you for your time.

    @MyItem.qml
    import QtQuick 2.0
    import QtQuick.Window 2.0

    Text {
    id: text1
    visible: true

    //property string autori: msg.author
    
    width: 100; height: 100
    text: msg.author    // invokes Message::author() to get this value
    

    // anchors.centerIn: why
    color: "green"
    font.pixelSize: 20

    Component.onCompleted: {
        msg.author = "Jonah"  // invokes Message::setAuthor()
    }  @
    

    main.qml
    @
    import QtQuick 2.2
    import QtQuick.Window 2.1

    MyItem{

    Window {

    property string name: ;
    
    
    
    visible: true
    id: main_window
    width: 500; height: width
    color:"black"
    

    }

     }   @
    

    main.cpp
    @
    #include <QGuiApplication>
    #include <QQmlApplicationEngine>

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlEngine>
    #include <QQmlContext>
    #include <QQmlComponent>
    #include <QDebug>
    #include "message.h"

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

    QQmlApplicationEngine engine_e;
    engine_e.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
    QQmlEngine engine;
    Message msg;
    engine.rootContext()->setContextProperty("msg", &msg);
    QQmlComponent component(&engine, QUrl::fromLocalFile&#40;"main.qml"&#41;);
    
    
    if(component.status()!=component.Ready){
        if(component.Error){
    
        qDebug()<<"Error: "<<component.errorString();
    }
    

    }

    return app.exec();
    

    }
    @
    message.h
    @
    #ifndef MESSAGE
    #define MESSAGE

    #include <QObject>
    #include <QString>

    class Message : public QObject
    {
    Q_OBJECT
    Q_PROPERTY(QString author READ author WRITE setAuthor NOTIFY authorChanged)
    public:
    void setAuthor(const QString &a) {
    if (a != m_author) {
    m_author = a;
    emit authorChanged();
    }
    }
    QString author() const {
    return m_author;
    }
    signals:
    void authorChanged();
    private:
    QString m_author;
    };

    #endif // MESSAGE
    @

    1 Reply Last reply
    0
    • P Offline
      P Offline
      p3c0
      Moderators
      wrote on 21 Dec 2014, 05:58 last edited by
      #2

      Hi,

      First why are you loading the main.qml twice ?
      The following way should work:
      @
      Message msg;
      QQmlApplicationEngine engine_e;
      engine_e.rootContext()->setContextProperty("msg", &msg);
      engine_e.load(QUrl(QStringLiteral("qrc:/main.qml")));
      @

      Also if you want to use QQmlComponent to load the file make sure you "create":http://doc.qt.io/qt-5/qqmlcomponent.html#create it.

      157

      1 Reply Last reply
      0
      • H Offline
        H Offline
        Homer JS
        wrote on 21 Dec 2014, 10:48 last edited by
        #3

        [quote author="p3c0" date="1419141491"]Hi,

        First why are you loading the main.qml twice ? [/quote]

        Because I'm a noob (and a little bit dumb as it seems) plus I feel like there's no learning resources on Qt5 (especially Qt 5.4). No books, no explained examples(the ones from the doc. are vague and not explained at all. Imo of course).
        I resolved this propblem with the help of some guy from another website where things get overflown (overflowed?).
        Thanks for the answer anyway. I really appreciate it.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          p3c0
          Moderators
          wrote on 21 Dec 2014, 13:15 last edited by
          #4

          That's fine. Everyone was newb at some point of time :)

          This forum has quite good documentation for QML like:
          "http://doc.qt.io/qt-5/gettingstartedqml.html":http://doc.qt.io/qt-5/gettingstartedqml.html
          "http://doc.qt.io/qt-5/qmlapplications.html":http://doc.qt.io/qt-5/qmlapplications.html
          "http://doc.qt.io/qt-5/qtqml-index.html":http://doc.qt.io/qt-5/qtqml-index.html

          If you have Qt installed you can find some good examples in quick folder.
          Apart from these there are also some external resources for eg.
          "qmlbook.org":http://qmlbook.org

          Hope this helps you a bit ...

          157

          1 Reply Last reply
          0
          • T Offline
            T Offline
            Torgeir
            wrote on 30 Dec 2014, 11:59 last edited by
            #5

            You have this nesting in main.qml:

            @MyItem {
            Window {
            }
            }@

            When loaded by QQmlApplicationEngine, only the innermost Window will become a window on your screen.

            Try nesting the opposite way if you intended MyItem to be displayed inside the Window:

            @Window {
            MyItem {}
            }@

            Also agree with p3c0 that you want to set the context property before loading the qml file and only load the qml file once.

            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