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. ReferenceError ... is not defined - functions tries to access a child in a style and StatusIndicator disappears after changing WidgetTab
Forum Update on Monday, May 27th 2025

ReferenceError ... is not defined - functions tries to access a child in a style and StatusIndicator disappears after changing WidgetTab

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 2 Posters 4.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.
  • R Offline
    R Offline
    RolBri
    wrote on 30 Oct 2015, 16:20 last edited by
    #1

    Hello,

    I have two problems for which I found no solution using Google.

    I would like to change the style of a StatusIndicator which is used as a foreground for a CircularGauge.
    I tried the attached code, but if I use the function setStatusM1 I get an error: "ReferenceError: m1Stat is not defined".

    Sadly I have no clue on that.
    How can I solve this?
    I tried to make an alias, but this failed too.

    import QtQuick 2.3
    import QtQuick.Controls.Styles 1.4
    import QtQuick.Extras 1.4
    
    Rectangle
    {
        function setStatusM1(value) {m1Stat.motorActive = value; }
     
        CircularGauge
        {
            id:m1
            x: -50
            y: -30
    
            style: CircularGaugeStyle
            {
                    foreground: Item
                    {
                        Rectangle
                        {
                            StatusIndicator
                            {
                                    id: m1Stat
                                    active: true
                                    property bool motorActive
                                    anchors.centerIn: parent
                                    color: motorActive ? "green" : "red"
                            }
                            anchors.centerIn: parent
                        }
                    }
            }
            value: 0
       }
    }
    

    My Second problem is that I display the file above in a QQuickWidget which is in a Tab Widget.
    If I change the Tab and change back to the Tab with the QQuickWidget the StatusIndicator is not visible anymore, whereas the circularGauge is still visible.
    Here I can also place a StatusIndicator in the same hierarchy as the circularGauge but it also disappears.
    Why is that and how can I solve this?

    Thank you very much :-)

    P 1 Reply Last reply 31 Oct 2015, 13:35
    0
    • R RolBri
      30 Oct 2015, 16:20

      Hello,

      I have two problems for which I found no solution using Google.

      I would like to change the style of a StatusIndicator which is used as a foreground for a CircularGauge.
      I tried the attached code, but if I use the function setStatusM1 I get an error: "ReferenceError: m1Stat is not defined".

      Sadly I have no clue on that.
      How can I solve this?
      I tried to make an alias, but this failed too.

      import QtQuick 2.3
      import QtQuick.Controls.Styles 1.4
      import QtQuick.Extras 1.4
      
      Rectangle
      {
          function setStatusM1(value) {m1Stat.motorActive = value; }
       
          CircularGauge
          {
              id:m1
              x: -50
              y: -30
      
              style: CircularGaugeStyle
              {
                      foreground: Item
                      {
                          Rectangle
                          {
                              StatusIndicator
                              {
                                      id: m1Stat
                                      active: true
                                      property bool motorActive
                                      anchors.centerIn: parent
                                      color: motorActive ? "green" : "red"
                              }
                              anchors.centerIn: parent
                          }
                      }
              }
              value: 0
         }
      }
      

      My Second problem is that I display the file above in a QQuickWidget which is in a Tab Widget.
      If I change the Tab and change back to the Tab with the QQuickWidget the StatusIndicator is not visible anymore, whereas the circularGauge is still visible.
      Here I can also place a StatusIndicator in the same hierarchy as the circularGauge but it also disappears.
      Why is that and how can I solve this?

      Thank you very much :-)

      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 31 Oct 2015, 13:35 last edited by
      #2

      @RolBri You cannot directly access the child items from outside. Instead you can create a property outside for eg. in root element and bind it to the property which you are interested in. Something like:

      Rectangle
      {
          property bool _motorActive
          CircularGauge
          {
              id:m1
              ...
              ...
              StatusIndicator
              {
                    id: m1Stat
                    active: true
                    property bool motorActive: _motorActive
                    ...
      

      And then change _motorActive when required.

      My Second problem is that I display the file above in a QQuickWidget which is in a Tab Widget.
      If I change the Tab and change back to the Tab with the QQuickWidget the StatusIndicator is not visible anymore, whereas the circularGauge is still visible.
      Here I can also place a StatusIndicator in the same hierarchy as the circularGauge but it also disappears.
      Why is that and how can I solve this?

      Seems strange. Can you post the code ?

      157

      1 Reply Last reply
      1
      • R Offline
        R Offline
        RolBri
        wrote on 31 Oct 2015, 21:56 last edited by
        #3

        Thank you very much for the help :-)

        I tested the disappearing StatusIndicator with another minimal project and it still disappears.
        For reproducing you can do the following:
        Create a standard widget project and insert a tabWidget in the GUI window. In the first tab insert a QQuickWidget.
        If you use the following code you can see the green rectangle and the StatusIndicator when launching the application. If you then switch to the second tab und back to the first the rectangle is still visible but the StatusIndicator is gone.

        You could also download the minimal project:
        http://www.daten-hoster.de/file/details/519290/test.zip

        .pro:

        QT       += core gui
        QT += quick quickwidgets
        CONFIG += c++11
        
        greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
        
        TARGET = test
        TEMPLATE = app
        
        
        SOURCES += main.cpp\
                mainwindow.cpp
        
        HEADERS  += mainwindow.h
        
        FORMS    += mainwindow.ui
        
        DISTFILES += \
            quick.qml
        
        RESOURCES += \
            res.qrc
        
        

        mainwindow.h:

        #ifndef MAINWINDOW_H
        #define MAINWINDOW_H
        
        #include <QMainWindow>
        #include <QQuickWidget>
        #include <QVariant>
        
        namespace Ui {
        class MainWindow;
        }
        
        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        
        public:
            explicit MainWindow(QWidget *parent = 0);
            ~MainWindow();
        
        private:
            Ui::MainWindow *ui;
        };
        
        #endif // MAINWINDOW_H
        

        mainwindow.cpp:

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        
        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
        
            ui->quickWidget->setSource(QUrl(QStringLiteral("qrc:/quick.qml")));
                ui->quickWidget->show();
                auto rootObject = ui->quickWidget->rootObject();
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        

        quick.qml:

        import QtQuick 2.3
        import QtQuick.Controls.Styles 1.4
        import QtQuick.Extras 1.4
        
        Item {
        
            Rectangle{
                StatusIndicator
                {
                      id: m1Stat
                      active: true
                      property bool motorActive
                      anchors.centerIn: parent
                      color: motorActive ? "green" : "red"
                 }
                 anchors.centerIn: parent
        
            }
            Rectangle{
                x: 100
                y: 100
                color: "green"
                height: 50
                width: 50
            }
        }
        
        P 1 Reply Last reply 1 Nov 2015, 04:57
        0
        • R RolBri
          31 Oct 2015, 21:56

          Thank you very much for the help :-)

          I tested the disappearing StatusIndicator with another minimal project and it still disappears.
          For reproducing you can do the following:
          Create a standard widget project and insert a tabWidget in the GUI window. In the first tab insert a QQuickWidget.
          If you use the following code you can see the green rectangle and the StatusIndicator when launching the application. If you then switch to the second tab und back to the first the rectangle is still visible but the StatusIndicator is gone.

          You could also download the minimal project:
          http://www.daten-hoster.de/file/details/519290/test.zip

          .pro:

          QT       += core gui
          QT += quick quickwidgets
          CONFIG += c++11
          
          greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
          
          TARGET = test
          TEMPLATE = app
          
          
          SOURCES += main.cpp\
                  mainwindow.cpp
          
          HEADERS  += mainwindow.h
          
          FORMS    += mainwindow.ui
          
          DISTFILES += \
              quick.qml
          
          RESOURCES += \
              res.qrc
          
          

          mainwindow.h:

          #ifndef MAINWINDOW_H
          #define MAINWINDOW_H
          
          #include <QMainWindow>
          #include <QQuickWidget>
          #include <QVariant>
          
          namespace Ui {
          class MainWindow;
          }
          
          class MainWindow : public QMainWindow
          {
              Q_OBJECT
          
          public:
              explicit MainWindow(QWidget *parent = 0);
              ~MainWindow();
          
          private:
              Ui::MainWindow *ui;
          };
          
          #endif // MAINWINDOW_H
          

          mainwindow.cpp:

          #include "mainwindow.h"
          #include "ui_mainwindow.h"
          
          MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
          
              ui->quickWidget->setSource(QUrl(QStringLiteral("qrc:/quick.qml")));
                  ui->quickWidget->show();
                  auto rootObject = ui->quickWidget->rootObject();
          }
          
          MainWindow::~MainWindow()
          {
              delete ui;
          }
          

          quick.qml:

          import QtQuick 2.3
          import QtQuick.Controls.Styles 1.4
          import QtQuick.Extras 1.4
          
          Item {
          
              Rectangle{
                  StatusIndicator
                  {
                        id: m1Stat
                        active: true
                        property bool motorActive
                        anchors.centerIn: parent
                        color: motorActive ? "green" : "red"
                   }
                   anchors.centerIn: parent
          
              }
              Rectangle{
                  x: 100
                  y: 100
                  color: "green"
                  height: 50
                  width: 50
              }
          }
          
          P Offline
          P Offline
          p3c0
          Moderators
          wrote on 1 Nov 2015, 04:57 last edited by
          #4

          @RolBri It seems it could possibly a bug. You can report at https://bugreports.qt.io. Post the link here for others to track it.

          157

          1 Reply Last reply
          0
          • R Offline
            R Offline
            RolBri
            wrote on 1 Nov 2015, 09:26 last edited by
            #5

            Thanks a lot :-)

            I now reported a bug:
            https://bugreports.qt.io/browse/QTBUG-49166

            1 Reply Last reply
            0

            1/5

            30 Oct 2015, 16:20

            • Login

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