Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for MCUs
  4. Can't connect cpp to qml project
QtWS25 Last Chance

Can't connect cpp to qml project

Scheduled Pinned Locked Moved Unsolved Qt for MCUs
mcucppmainstm32f7
3 Posts 2 Posters 555 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.
  • A Offline
    A Offline
    Armha
    wrote on 18 May 2023, 14:56 last edited by
    #1

    Good day to all,

    I'm having trouble connecting C++ to QML in a Qt for MCU project. The main issue is that QML seems to not recognize my C++ classes or structures.

    Despite numerous attempts, I was unable to find and install Qt Ultralite via the online installer. I'm uncertain if this could be the root of the problem, or if it's something else entirely.

    I'm using STM32F769I-Discovery board and tried to download it's kit also.

    In my project https://github.com/Funkvay/QtMCU, I have a rather simple setup, and I've tried various approaches to make it work. For example, I have tried using a singleton structure as well as a regular Qul::Object.

    In QML, I have code like this:

    Item {
    Component.onCompleted: test1.feed(5);
    }
    

    and

    test1 {
            id: test1
            bigness: 61
            onGotFood: bigness = 99
        }
    

    These are just some attempts to see if QML would recognize my C++ objects (see the image.png in the gitub to see the other version of "test1").

    Here I get errors like

    MCU2Depricated.qml:20: error: Could not determine type for global name test1
            Component.onCompleted: test1.feed(5);
    

    or

    Invalid property name "test1"
    

    However, despite these efforts, QML seems unable to see my C++ entities. At this point, I'm unsure what could be causing this issue. Could it be something in CMake, or perhaps an incorrect declaration of the class, or could the absence of Ultralite be the problem?

    I tried to create new projects to try this out, but it didn't work.

    I'd greatly appreciate any guidance or suggestions on what to try next. If more information is needed to diagnose the issue, please let me know.

    Thank you in advance.

    S 1 Reply Last reply 19 May 2023, 05:41
    0
    • A Armha
      18 May 2023, 14:56

      Good day to all,

      I'm having trouble connecting C++ to QML in a Qt for MCU project. The main issue is that QML seems to not recognize my C++ classes or structures.

      Despite numerous attempts, I was unable to find and install Qt Ultralite via the online installer. I'm uncertain if this could be the root of the problem, or if it's something else entirely.

      I'm using STM32F769I-Discovery board and tried to download it's kit also.

      In my project https://github.com/Funkvay/QtMCU, I have a rather simple setup, and I've tried various approaches to make it work. For example, I have tried using a singleton structure as well as a regular Qul::Object.

      In QML, I have code like this:

      Item {
      Component.onCompleted: test1.feed(5);
      }
      

      and

      test1 {
              id: test1
              bigness: 61
              onGotFood: bigness = 99
          }
      

      These are just some attempts to see if QML would recognize my C++ objects (see the image.png in the gitub to see the other version of "test1").

      Here I get errors like

      MCU2Depricated.qml:20: error: Could not determine type for global name test1
              Component.onCompleted: test1.feed(5);
      

      or

      Invalid property name "test1"
      

      However, despite these efforts, QML seems unable to see my C++ entities. At this point, I'm unsure what could be causing this issue. Could it be something in CMake, or perhaps an incorrect declaration of the class, or could the absence of Ultralite be the problem?

      I tried to create new projects to try this out, but it didn't work.

      I'd greatly appreciate any guidance or suggestions on what to try next. If more information is needed to diagnose the issue, please let me know.

      Thank you in advance.

      S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 19 May 2023, 05:41 last edited by
      #2

      @Armha QML components have to start with a capital letter. So this should be:

      Test1 {
              id: test1
              bigness: 61
              onGotFood: bigness = 99
          }
      

      In order for a type to be visible in QML, you have to:

      • make sure the type is a subclass of Q_OBJECT
      • make sure it has a public constructor like MyClass(QObject*parent = nullptr);
      • register the type with QML engine https://doc.qt.io/qt-6/qqmlengine.html#qmlRegisterType
      • in QML file, import the URI you provided to qmlRegisterType

      Then it should work :-)

      A thing to remember: When you use the Test1 { somtething } syntax in QML, you are creating an instance of an object (in this case, of your C++ class). So adding a singleton of the same class to the project won't help here at all.

      (Z(:^

      A 1 Reply Last reply 22 May 2023, 07:50
      1
      • S sierdzio
        19 May 2023, 05:41

        @Armha QML components have to start with a capital letter. So this should be:

        Test1 {
                id: test1
                bigness: 61
                onGotFood: bigness = 99
            }
        

        In order for a type to be visible in QML, you have to:

        • make sure the type is a subclass of Q_OBJECT
        • make sure it has a public constructor like MyClass(QObject*parent = nullptr);
        • register the type with QML engine https://doc.qt.io/qt-6/qqmlengine.html#qmlRegisterType
        • in QML file, import the URI you provided to qmlRegisterType

        Then it should work :-)

        A thing to remember: When you use the Test1 { somtething } syntax in QML, you are creating an instance of an object (in this case, of your C++ class). So adding a singleton of the same class to the project won't help here at all.

        A Offline
        A Offline
        Armha
        wrote on 22 May 2023, 07:50 last edited by
        #3

        @sierdzio Thank you very much for the answer. I tried some tips, but unfortunately not all were applicable in my case, since let's say the same Q_OBJECT is missing in Qt microcontrollers. And in the examples that I'm looking at, it seems that there is no such thing (Q_OBJECT), which is why I ran into a problem, since everything works in the usual Qt for Desktop but not in Qt MCU

        1 Reply Last reply
        0

        1/3

        18 May 2023, 14:56

        • Login

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