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. Problem with ApplicationWindow
Forum Updated to NodeBB v4.3 + New Features

Problem with ApplicationWindow

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 358 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.
  • M Offline
    M Offline
    MScottM
    wrote on last edited by
    #1

    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!)?

    JoeCFDJ 1 Reply Last reply
    0
    • M MScottM

      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!)?

      JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by
      #2

      @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!

      M 1 Reply Last reply
      0
      • JoeCFDJ JoeCFD

        @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!

        M Offline
        M Offline
        MScottM
        wrote on last edited by
        #3

        @JoeCFD

        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!

        M 1 Reply Last reply
        0
        • M MScottM

          @JoeCFD

          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!

          M Offline
          M Offline
          MScottM
          wrote on last edited by
          #4

          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?

          JoeCFDJ 1 Reply Last reply
          0
          • M MScottM

            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?

            JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by
            #5

            @MScottM Mixing QML and C++ is ok and you have to do that. But it is different from mixing two main windows of QML and QWidget. QWidgets != C++. I would recommend you to use QWidgets or QML only, better not both.

            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