ReferenceError ... is not defined - functions tries to access a child in a style and StatusIndicator disappears after changing WidgetTab
-
wrote on 30 Oct 2015, 16:20 last edited by
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 functionsetStatusM1
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 :-)
-
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 functionsetStatusM1
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 :-)
@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 ?
-
wrote on 31 Oct 2015, 21:56 last edited by
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 atabWidget
in the GUI window. In the first tab insert aQQuickWidget
.
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 } }
-
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 atabWidget
in the GUI window. In the first tab insert aQQuickWidget
.
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 } }
@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.
-
wrote on 1 Nov 2015, 09:26 last edited by
Thanks a lot :-)
I now reported a bug:
https://bugreports.qt.io/browse/QTBUG-49166
1/5