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] Exporting functions and constants from C++ to QML
Forum Updated to NodeBB v4.3 + New Features

[Solved] Exporting functions and constants from C++ to QML

Scheduled Pinned Locked Moved Mobile and Embedded
17 Posts 4 Posters 11.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.
  • sierdzioS Offline
    sierdzioS Offline
    sierdzio
    Moderators
    wrote on last edited by
    #4

    Yes. Now you only need to add Q_INVOKABLE in front of your method declarations in the header file.

    (Z(:^

    1 Reply Last reply
    0
    • I Offline
      I Offline
      Ices_Eyes
      wrote on last edited by
      #5

      Ok! Thanks... And now let's do some programming :P

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

        May the code be with you.

        You'll also have to correct some errors in your c++ code, but I trust the compiler to point them out for you.

        (Z(:^

        1 Reply Last reply
        0
        • I Offline
          I Offline
          Ices_Eyes
          wrote on last edited by
          #7

          Well, yep, I've written down the code quickly ;)

          1 Reply Last reply
          0
          • I Offline
            I Offline
            Ices_Eyes
            wrote on last edited by
            #8

            Well... It worksss!!! Thaks guys ;)

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

              I'm happy to hear that :) And you even marked the thread as solved - perfection itself. Happy further coding.

              (Z(:^

              1 Reply Last reply
              0
              • I Offline
                I Offline
                Ices_Eyes
                wrote on last edited by
                #10

                Ok, I have one more question with respect to this :)
                What I have done is something like:
                @Q_INVOKABLE QString getUrl(QString urlParameter);@

                But in many tutorials I have seen now, the use
                @Q_INVOKABLE QString getNewsUrl(const QString &urlParameter);@

                Which one is more correct?

                Also. Having javascript "var" variables that contains integers, is there a way to pass this types to C++ instead of strings? Or should I convert them in C++? I mean... Using
                @Q_INVOKABLE QString getNewsUrl(int urlParameter);@
                gives me no right values in urlParameter variables...

                :)

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #11

                  [quote author="Ices_Eyes" date="1346753251"]Ok, I have one more question with respect to this :)
                  What I have done is something like:
                  @Q_INVOKABLE QString getUrl(QString urlParameter);@

                  But in many tutorials I have seen now, the use
                  @Q_INVOKABLE QString getNewsUrl(const QString &urlParameter);@

                  Which one is more correct?
                  [/quote]
                  Why would you assume the one is 'more' correct than the other. By what measure exactly?

                  Some coding guidelines tell you to use const references for function arguments that as used unchanged by the function, and in that context using the const QString& might be more correct. But not everybody uses this guideline. For efficieny, there is very little gain here, as QString is implicitly shared and thus very fast to copy as a value.

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

                    [quote author="Ices_Eyes" date="1346753251"]
                    Also. Having javascript "var" variables that contains integers, is there a way to pass this types to C++ instead of strings? Or should I convert them in C++? I mean... Using
                    @Q_INVOKABLE QString getNewsUrl(int urlParameter);@
                    gives me no right values in urlParameter variables...
                    :)[/quote]

                    As Andre already covered the top part, I'll take on the bottom one. QML automatically translates JS values to C++ ones. So, for int you get c++ int, for real you get Qt's qreal, for string it's QString, for arrays is complicated (as you can store anything there. But basically QObjectList tends to work in most cases, or QVariantList etc.). There is no need for conversion of those basic types.

                    (Z(:^

                    1 Reply Last reply
                    0
                    • I Offline
                      I Offline
                      Ices_Eyes
                      wrote on last edited by
                      #13

                      So, I need to understand why
                      @
                      var parameter = 1;
                      ...
                      myClass.myFunc(parameter)
                      @
                      works if myFunct is defined with QString and does not work if defined with int... ;)

                      Thanks guys!!! :)

                      1 Reply Last reply
                      0
                      • I Offline
                        I Offline
                        Ices_Eyes
                        wrote on last edited by
                        #14

                        Damn!!! My bad... It was a conversion problem in C++ of integer to string!!! Damn, too much codin in Java and Javascript, I have completly forgotten C!!! :(

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

                          This is indeed strange. Be sure to make a full app rebuild (clean, run qmake, build) just to make sure that MOC updates the definitions. It quite definitely works for me. Alternatively, you can try using QVariant in c++ declaration, and then calling ::toInt() on the value - but that should not be necessary.

                          (Z(:^

                          1 Reply Last reply
                          0
                          • I Offline
                            I Offline
                            Ices_Eyes
                            wrote on last edited by
                            #16

                            Solved with a QString::number(parameter)...

                            Damn me... :P

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

                              Happens to everybody :) That took me by surprise a few times, too. I tend to forget about ::number() especially when I'm tired and after prolonged QML-only phases :D

                              (Z(:^

                              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