Problem with ApplicationWindow
-
I'm having an issue with showing a QML ApplicationWindow, or rather the items inside the window. This application WAS working (showing the items in the window and then after a minor code change which I don't think was related, stopped showing the items.
I'll try and explain the best I can the issue:
I have 'MainWindow.h' where I declare the QQmlComponent:
namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); QQmlApplicationEngine engine; class pMessageProcessor pMessageProcessor; //class moduleFinder moduleFinder; QQmlComponent *component = new QQmlComponent(&engine, (QUrl(QStringLiteral("qrc:/qml/mainAppWindow.qml"))));
and 'MainWindow.cpp' where some contexts are set:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), m_ui(new Ui::MainWindow) { engine.rootContext()->setContextProperty("boardMessages", &pMessageProcessor);
and the QML window is created on a button click:
// Create Monitor window when menu item is clicked void MainWindow::on_actionPDS_Bus_triggered() { if( component->status() != component->Ready ) { if( component->status() == component->Error ) qDebug()<< "Error: " + component->errorString(); return; } component->create(); component->setParent(this); qDebug() << "Create qml window"; }
And here is the ApplicationWindow:
ApplicationWindow { id: root visible: true color: "grey" minimumHeight: 800 minimumWidth: 1000 height: Screen.height * 0.6 width: Screen.width * 0.8 title: qsTr("Board Monitor") background: Rectangle { gradient: Gradient { GradientStop { position: 0; color: "white" } GradientStop { position: 1; color: "light grey" } } } property real sVoltage: 0.0 property real sCurrent: 0.0 property bool switchState: false property real boardStatus: 0 property bool bmsVis: false property string srcAddress: "" Connections { target: boardMessages function onBmsData(essAddress, bmsStatus, swState, stackVoltage, stackCurrent) { srcAddress = essAddress; statusMessages2.text = srcAddress; if(srcAddress === "2A") { switchState = swState; sVoltage = Math.round( stackVoltage * 100 ) / 100; sCurrent = Math.round( stackCurrent * 100 ) / 100; } console.log("received message: " + bmsStatus + " " + swState + " " + stackVoltage) } } header: ToolBar { //console.log: "context 1" //id: toolbar width: parent.width contentItem.children: rl RowLayout { id: rl width: parent.width/2 height: btnDispatchOn.height Shared.Button{ id: btnDispatchOn text: qsTr("Dispatch Mode ON ") onClicked: setDispatchModeOn() Layout.leftMargin: 20 } Shared.Button { id: btnDispatchOff text: qsTr("Dispatch Mode OFF") onClicked: setDispatchModeOff() } Item { Layout.fillWidth: true } } //RowLayout } //ToolBar
underneath the header are some Rectangles{} where I show some data in Labels, etc.
Like I said, it was working until I made a change inside the Connections{} statement, where I changed a formula. Then when I ran it, it would display the ApplicationWindow, including the Rectangle with the StatusMessages TextArea, but nothing underneath, and the console contained the message: QQmlEngine::setContextForObject(): Object already has a QQmlContext.
I don't know if this is a clue, but when I comment out the Header{}, the error message goes away, but it still doesn't display anything underneath the status message bar.
What can I do to troubleshoot this (spent hours over the weekend already!)?
-
I'm having an issue with showing a QML ApplicationWindow, or rather the items inside the window. This application WAS working (showing the items in the window and then after a minor code change which I don't think was related, stopped showing the items.
I'll try and explain the best I can the issue:
I have 'MainWindow.h' where I declare the QQmlComponent:
namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); QQmlApplicationEngine engine; class pMessageProcessor pMessageProcessor; //class moduleFinder moduleFinder; QQmlComponent *component = new QQmlComponent(&engine, (QUrl(QStringLiteral("qrc:/qml/mainAppWindow.qml"))));
and 'MainWindow.cpp' where some contexts are set:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), m_ui(new Ui::MainWindow) { engine.rootContext()->setContextProperty("boardMessages", &pMessageProcessor);
and the QML window is created on a button click:
// Create Monitor window when menu item is clicked void MainWindow::on_actionPDS_Bus_triggered() { if( component->status() != component->Ready ) { if( component->status() == component->Error ) qDebug()<< "Error: " + component->errorString(); return; } component->create(); component->setParent(this); qDebug() << "Create qml window"; }
And here is the ApplicationWindow:
ApplicationWindow { id: root visible: true color: "grey" minimumHeight: 800 minimumWidth: 1000 height: Screen.height * 0.6 width: Screen.width * 0.8 title: qsTr("Board Monitor") background: Rectangle { gradient: Gradient { GradientStop { position: 0; color: "white" } GradientStop { position: 1; color: "light grey" } } } property real sVoltage: 0.0 property real sCurrent: 0.0 property bool switchState: false property real boardStatus: 0 property bool bmsVis: false property string srcAddress: "" Connections { target: boardMessages function onBmsData(essAddress, bmsStatus, swState, stackVoltage, stackCurrent) { srcAddress = essAddress; statusMessages2.text = srcAddress; if(srcAddress === "2A") { switchState = swState; sVoltage = Math.round( stackVoltage * 100 ) / 100; sCurrent = Math.round( stackCurrent * 100 ) / 100; } console.log("received message: " + bmsStatus + " " + swState + " " + stackVoltage) } } header: ToolBar { //console.log: "context 1" //id: toolbar width: parent.width contentItem.children: rl RowLayout { id: rl width: parent.width/2 height: btnDispatchOn.height Shared.Button{ id: btnDispatchOn text: qsTr("Dispatch Mode ON ") onClicked: setDispatchModeOn() Layout.leftMargin: 20 } Shared.Button { id: btnDispatchOff text: qsTr("Dispatch Mode OFF") onClicked: setDispatchModeOff() } Item { Layout.fillWidth: true } } //RowLayout } //ToolBar
underneath the header are some Rectangles{} where I show some data in Labels, etc.
Like I said, it was working until I made a change inside the Connections{} statement, where I changed a formula. Then when I ran it, it would display the ApplicationWindow, including the Rectangle with the StatusMessages TextArea, but nothing underneath, and the console contained the message: QQmlEngine::setContextForObject(): Object already has a QQmlContext.
I don't know if this is a clue, but when I comment out the Header{}, the error message goes away, but it still doesn't display anything underneath the status message bar.
What can I do to troubleshoot this (spent hours over the weekend already!)?
@MScottM said in Problem with ApplicationWindow:
QMainWindow
Why do you mix QMainWindow with qml ApplicationWindow? I would prefer to use either QMainWindow or qml ApplicationWindow, but not both. I struggled a little to put a layered qml into a layout of qwidgets. Not a great experience!
-
@MScottM said in Problem with ApplicationWindow:
QMainWindow
Why do you mix QMainWindow with qml ApplicationWindow? I would prefer to use either QMainWindow or qml ApplicationWindow, but not both. I struggled a little to put a layered qml into a layout of qwidgets. Not a great experience!
I was hoping to get something going quickly by using a GUI that I had already build in QML and combining it with a Kvaser program that someone had put up on Github, and in this case it would suit me to keep the two GUI's separate, as they each have their own purpose.
Honestly, if I'm not running into stupid issues like this, I find it pretty rewarding to mix QML and C++, it's like the best of both worlds!
-
I was hoping to get something going quickly by using a GUI that I had already build in QML and combining it with a Kvaser program that someone had put up on Github, and in this case it would suit me to keep the two GUI's separate, as they each have their own purpose.
Honestly, if I'm not running into stupid issues like this, I find it pretty rewarding to mix QML and C++, it's like the best of both worlds!
-
It might help me to know if no one is replying because they've never seen this issue either, or because the it's so obvious that I should be able to figure it out on my own?