Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED] Questins about creating a QML component using c++
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Questins about creating a QML component using c++

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 3 Posters 2.9k 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.
  • T Offline
    T Offline
    terietor
    wrote on last edited by
    #1

    Hello,

    I want to make a component which requires to use c++. I have read the docs about
    creating components via c++ but i have some questions.

    @class HelloQML : public QObject {
    //foo
    bool setHelloMessage(A *a_hello, B *b_hello);//question no1
    C * helloMessage();//question no2
    //will the above work?
    Q_INVOKABLE void doHello(char a, QString b, QString *c);//question no3
    }@

    @import foo

    HelloQML {

    helloMessage: {
    //Question no1
    //what should i add here??
    }
    //Question no3
    //will this work?
    doHello('a', "aa", "aaa");
    }@

    A,B,C are my classes which doesn't derive from QObject and I haven't add to them any QML relative code.

    thanks in advance

    terietor.gr

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      in order for any functions to be available in QML, you have to use either Q_INVOKABLE or Q_PROPERTY. So, "questions" 1 and 2 will not work. You are trying to use those methods as properties, which is wrong. You'll have to declare a property, and then add getter, setter methods, and a notification signal.

      About question 3 - this will also fail. You are trying to invoke a JS/C++ method directly in QML, which is not supported. You can invoke methods in assignments (like "height: myMethod(foo)") or in slots. So, here, for it to work, you'll have to use:
      @
      // inside HelloQML
      Component.onCompleted: doHello('a', "aa", "aaa");
      @

      Also, your import statement is wrong. It has to specify version number, IIRC.

      (Z(:^

      1 Reply Last reply
      0
      • T Offline
        T Offline
        terietor
        wrote on last edited by
        #3

        [quote author="sierdzio" date="1327128947"]in order for any functions to be available in QML, you have to use either Q_INVOKABLE or Q_PROPERTY.
        [/quote]

        My mistake,I didn't mention about the existance of a Q_PROPERTY, before setHelloMessage there are

        @
        Q_OBJECT
        Q_PROPERTY(...)
        @

        [quote]
        About question 3 - this will also fail. You are trying to invoke a JS/C++ method directly in QML,[/quote]

        Correct.This was wrong

        I wanted to know if my code will work from the point of view that classes A,B,C doesn't derive from QObject.

        terietor.gr

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          [quote author="Giorgos Tsiapaliwkas" date="1327183176"]
          I wanted to know if my code will work from the point of view that classes A,B,C doesn't derive from QObject.
          [/quote]

          I don't think I've ever tried doing that, so I can't give you a definite answer. IIRC, as long as QVariant can work with these classes, you can use them in QML - C++ communication.

          (Z(:^

          1 Reply Last reply
          0
          • T Offline
            T Offline
            terietor
            wrote on last edited by
            #5

            [quote author="Giorgos Tsiapaliwkas" date="1327072363"]Hello,

            @
            bool setHelloMessage(A *a_hello, B *b_hello);//question no1
            @

            @import foo

            HelloQML {
            onSomething:{
            helloMessage: {
            //what should i add here??
            //my setter has two parameters.
            }
            }@
            [/quote]

            Does anyone know what I should add??

            Will this work?

            @import foo

            HelloQML {
            onSomething:{
            helloMessage: {
            A:.....
            B:.....
            }
            }@

            terietor.gr

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              Properties can have only one parameter. They should represent getters and setters directly. I'm afraid your code is wrong again: you open a JavaScript block after "onSomething", which would any QML assignment inside invalid (like on lines 5., 6., and 7.).

              Here's a tip that would probably help you most: go ahead and try. Think of a simple, example application and code it to get basic experience. This would make solving this problem a lot easier for you, and also will be a source of fun.

              (Z(:^

              1 Reply Last reply
              0
              • T Offline
                T Offline
                terietor
                wrote on last edited by
                #7

                thank you

                I will mark it as solved

                terietor.gr

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  Punit_Tak
                  wrote on last edited by
                  #8

                  Hello Sierdzio, your suggestions and tips helped me too. I would like learn from you if can start any forum related to Qt Quick and C++ integration. Nicely explained. Thanks a ton.

                  Hello Giorgos, Thanks for asking your doubt, it clears my doubt too.

                  Thanks and regards,
                  Punit Tak

                  Punit Tak

                  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