Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. [SOLVED] Qt Quick (QML) and C/C++ example
Forum Update on Tuesday, May 27th 2025

[SOLVED] Qt Quick (QML) and C/C++ example

Scheduled Pinned Locked Moved Mobile and Embedded
9 Posts 6 Posters 7.7k Views 1 Watching
  • 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
    marcin100
    wrote on 30 May 2013, 04:16 last edited by
    #1

    Ok, I did some searching but could not find any good examples with short descriptions. Most of pages go into a lot of detail and require a significant object oriented programming background.

    I have a decent C programming background but not much C++ or Java and I quickly get lost with these C++ examples. I am used to the Visual C way of programming where gui object's action such as BUTTON_CLICK would be directly linked to the C/C++ code. I was hoping that Qt/QtQuick provides similar solution, but I failed to located a simple example.

    My question is simple:
    Is it possible to do a QtQuick(QML) and C combo on embedded device? Prefer QtQuick due to the graphic flexibility and easy animations/transitions.

    Project goal: use QtQuick for a GUI and C for the "code behind".
    The "code behind" would control the following:

    • I2C bus
    • change the state of discrete outputs
    • generate data to be sent to QML for the display
      I can handle the C code and embedded side of things, but I cannot figure out how to execute the C code after a QML defined button is clicked.

    Thx!

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bjanuario
      wrote on 31 May 2013, 23:41 last edited by
      #2

      Did u try the "hello world" that cames from Qt Installation examples ?
      I think you will get it there .. is simple

      1 Reply Last reply
      0
      • M Offline
        M Offline
        marcin100
        wrote on 1 Jun 2013, 03:58 last edited by
        #3

        Are you sure that "hello world" example goes into mixing C++ and QML? I would appreciate the exact path to the example you have mentioned.

        I am using qt 4.8.4 and I was unable to locate any "hello world" code in the "examples" folder which would go over the usage of C/C++ with QML.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          portoist
          wrote on 5 Jun 2013, 13:00 last edited by
          #4

          There are two ways to connect you C++ code with QML. One is to add instance of your class to QML context, second is to register your class as QML type.
          Lets have class like this:
          @
          class MyClass : public QObject {
          Q_OBJECT
          public slots:
          void doSomething();
          }
          @
          Now to add this class to QML context you have to do something like this in you main method (or anywhere else, where you have access to QmlApplicationViewer instance):
          @
          QmlApplicationViewer viewer;
          ...
          viewer.rootContext()->setContextProperty("MyObject", new MyClass());
          @
          In QML you can do:
          @
          Item{
          Component.onCompleted: MyObject.doSomething()
          }
          @
          To register class to QML you need to do something like this in your main method (or somewhere else):
          @
          qmlRegisterType<MyClass>("my.utils",1,0,"MyClass");
          @
          And in QML:
          @
          import my.utils 1.0
          Item{
          Component.onCompleted: myObject.doSomething()
          MyClass{
          id: myObject
          }
          }
          @

          1 Reply Last reply
          0
          • M Offline
            M Offline
            marcin100
            wrote on 12 Jun 2013, 23:31 last edited by
            #5

            Very good post portoist! Thanks!

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jediengineer
              wrote on 3 Feb 2014, 21:08 last edited by
              #6

              Does this work with existing Qt classes? Say the QUdpSocket class? Is it possible that someone could give an example of how to bind that class into QML? Most of my experience is with embedded processors and C, so this is a little abstract to me... including the tutorials.

              basically is it as easy as this?

              @ import QUdpSocket @

              and then, to call the writeDatagram function from a button, I just simply need the following (using general function description)?

              @Button {
              id: autoupdate
              x: 340
              y: 56
              text: "AutoUpdate"
              signal clicked
              onClicked: QUdpSocket.writeDatagram()
              }@

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kangqf
                wrote on 4 Feb 2014, 01:59 last edited by
                #7

                learn a lot

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  JKSH
                  Moderators
                  wrote on 4 Feb 2014, 04:24 last edited by
                  #8

                  [quote author="jediengineer" date="1391461698"]Does this work with existing Qt classes? Say the QUdpSocket class? Is it possible that someone could give an example of how to bind that class into QML? Most of my experience is with embedded processors and C, so this is a little abstract to me... including the tutorials.

                  basically is it as easy as this?

                  @ import QUdpSocket @

                  and then, to call the writeDatagram function from a button, I just simply need the following (using general function description)?

                  @Button {
                  id: autoupdate
                  x: 340
                  y: 56
                  text: "AutoUpdate"
                  signal clicked
                  onClicked: QUdpSocket.writeDatagram()
                  }@[/quote]Hi Jediengineer, please don't post the same thing twice. I've replied to your original post: http://qt-project.org/forums/viewthread/37907/

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    jediengineer
                    wrote on 4 Feb 2014, 14:25 last edited by
                    #9

                    JKSH, sorry, I didn't see your reply when I posted this. I'll keep it in one posting from here on out. Thanks!

                    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