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. SetContextProperty multiple files
QtWS25 Last Chance

SetContextProperty multiple files

Scheduled Pinned Locked Moved QML and Qt Quick
qmlreference errormultiplefiles
2 Posts 2 Posters 1.7k 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.
  • N Offline
    N Offline
    neovius
    wrote on 6 Oct 2015, 08:35 last edited by
    #1

    Hello,

    I have a ReferenceError in my qml file : ReferenceError: userInterface is not defined. The error is in this line:

    userInterface.backlightChanged(backlightslider.value)

    but the qml link is working properly. below my code

    main.cpp

     QGuiApplication app(argc,argv);
        QQmlEngine engine;
        QQmlComponent component(&engine,QUrl("qrc:/QML/Qml/newMain.qml"));
        QObject *object = component.create();
        if (component.isError())
        {
            qDebug() << component.errors();
        }
        UserInterface userInterface(*object);
        engine.rootContext()->setContextProperty("userInterface",&userInterface);
        return app.exec();
    

    newMain.qml

    ApplicationWindow {
        id: mainWindow
        width: 1024
        height: 768
        visible: true
    
        SettingsMenu{ id: settingsMenu; objectName: "SettingsMenu"}
    
    ...some more code
    

    settingsMenu

    Window {
        id: settingsMenu
        width: 1024
        height: 768
        color: "#d1d3d4"
        visible: false
    
    Slider {
            property bool stateVisible: false
            id: backlightslider
            x: 885
            y: 525
            width: 50
            height: 227
            onValueChanged: {
                userInterface.backlightChanged(backlightslider.value)
            }
    
    

    userinterface.cpp

    UserInterface::UserInterface(QObject &windowInstance)
        :m_object(&windowInstance)
    {
        QQuickWindow *item = qobject_cast<QQuickWindow*>(m_object);
        m_playButton = (item->findChild<QObject *>("PlayButton"));
    }
    
    void UserInterface::backlightChanged(const int &backlightLevel)
    {
        QFile backlight("/sys/class/backlight/backlight_lvds.29/brightness");
        backlight.open(QIODevice::WriteOnly | QIODevice::Truncate);
        QTextStream out(&backlight);
        out << backlightLevel;
        backlight.close();
    
    }
    

    When i change the backlight slider, the signal is emitted, and received. but i still get the warning when starting the application :

    ReferenceError: userInterface is not defined

    What am i doing wrong?

    P 1 Reply Last reply 6 Oct 2015, 11:19
    0
    • N neovius
      6 Oct 2015, 08:35

      Hello,

      I have a ReferenceError in my qml file : ReferenceError: userInterface is not defined. The error is in this line:

      userInterface.backlightChanged(backlightslider.value)

      but the qml link is working properly. below my code

      main.cpp

       QGuiApplication app(argc,argv);
          QQmlEngine engine;
          QQmlComponent component(&engine,QUrl("qrc:/QML/Qml/newMain.qml"));
          QObject *object = component.create();
          if (component.isError())
          {
              qDebug() << component.errors();
          }
          UserInterface userInterface(*object);
          engine.rootContext()->setContextProperty("userInterface",&userInterface);
          return app.exec();
      

      newMain.qml

      ApplicationWindow {
          id: mainWindow
          width: 1024
          height: 768
          visible: true
      
          SettingsMenu{ id: settingsMenu; objectName: "SettingsMenu"}
      
      ...some more code
      

      settingsMenu

      Window {
          id: settingsMenu
          width: 1024
          height: 768
          color: "#d1d3d4"
          visible: false
      
      Slider {
              property bool stateVisible: false
              id: backlightslider
              x: 885
              y: 525
              width: 50
              height: 227
              onValueChanged: {
                  userInterface.backlightChanged(backlightslider.value)
              }
      
      

      userinterface.cpp

      UserInterface::UserInterface(QObject &windowInstance)
          :m_object(&windowInstance)
      {
          QQuickWindow *item = qobject_cast<QQuickWindow*>(m_object);
          m_playButton = (item->findChild<QObject *>("PlayButton"));
      }
      
      void UserInterface::backlightChanged(const int &backlightLevel)
      {
          QFile backlight("/sys/class/backlight/backlight_lvds.29/brightness");
          backlight.open(QIODevice::WriteOnly | QIODevice::Truncate);
          QTextStream out(&backlight);
          out << backlightLevel;
          backlight.close();
      
      }
      

      When i change the backlight slider, the signal is emitted, and received. but i still get the warning when starting the application :

      ReferenceError: userInterface is not defined

      What am i doing wrong?

      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 6 Oct 2015, 11:19 last edited by
      #2

      Hi @neovius and Welcome,

      When i change the backlight slider, the signal is emitted, and received. but i still get the warning when starting the application :

      That is because you are creating QML component before setting the context property and thus naturally it wont be able to access that method at very first time.

      http://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html#exposing-properties
      http://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html#exposing-methods-including-qt-slots

      157

      1 Reply Last reply
      0

      1/2

      6 Oct 2015, 08:35

      • Login

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