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. Expose a C++ typedef to QML
Forum Updated to NodeBB v4.3 + New Features

Expose a C++ typedef to QML

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 3.3k Views 2 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
    m_andrej
    wrote on last edited by
    #1

    How can I make QML engine to recognize C++ typedef? I tried to define my type both by using and typedef:
    Header file:

    using primary_key = qint64;
    // or typedef qint64 primary_key;
    Q_DECLARE_METATYPE(primary_key)
    

    Source file:

    void init_db()
    {
        qRegisterMetaType<primary_key>();
        ...
    }
    

    But when I call a Q_INVOKABLE function which returns primary_key, I get "Unknown method return type" error at runtime.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      m_andrej
      wrote on last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • T Offline
        T Offline
        ttuna
        wrote on last edited by
        #3

        First of all a line from the docs:

        To use the type T in queued signal and slot connections, qRegisterMetaType<T>() must be called before the first connection is established.

        Maybe you could try to move qRegisterMetaType<primary_key>(); into the constructor of the class.

        M 1 Reply Last reply
        0
        • T ttuna

          First of all a line from the docs:

          To use the type T in queued signal and slot connections, qRegisterMetaType<T>() must be called before the first connection is established.

          Maybe you could try to move qRegisterMetaType<primary_key>(); into the constructor of the class.

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

          @ttuna Thanks, but I'm not using the type in signal and slot connections.

          T 1 Reply Last reply
          0
          • M m_andrej

            @ttuna Thanks, but I'm not using the type in signal and slot connections.

            T Offline
            T Offline
            ttuna
            wrote on last edited by
            #5

            @m_andrej said:

            @ttuna Thanks, but I'm not using the type in signal and slot connections.

            Not having signals emitted by your own does not mean that Qt doesn't in the background.
            If QmlEngine and your QObject are living in different threads i think they'll communicate over signal / slot.
            I ran into a similar problems and because the situation in your code example isn't clear i thought it's worth a shot.

            M 1 Reply Last reply
            0
            • T ttuna

              @m_andrej said:

              @ttuna Thanks, but I'm not using the type in signal and slot connections.

              Not having signals emitted by your own does not mean that Qt doesn't in the background.
              If QmlEngine and your QObject are living in different threads i think they'll communicate over signal / slot.
              I ran into a similar problems and because the situation in your code example isn't clear i thought it's worth a shot.

              M Offline
              M Offline
              m_andrej
              wrote on last edited by
              #6

              @ttuna OK, I see. But I'm calling qRegisterMetaType<primary_key>(); before QmlEngine is even created. Sorry for not mentioning this at the first time.

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

                Maybe you can post a more detailed code example because it's difficult to take all possible scenarios into account (e.g. different namespaces, ...)

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  m_andrej
                  wrote on last edited by
                  #8

                  Here is the complete example:

                  TestClass.h (doesn't have a cpp file):

                  #pragma once
                  #include <QObject>
                  
                  using primary_key = qint64;
                  
                  class TestClass : public QObject
                  {
                      Q_OBJECT
                  public:
                      explicit TestClass(QObject *parent = 0) : QObject(parent) {}
                      Q_INVOKABLE primary_key lastInsertRowid() { return 0; }
                  };
                  
                  Q_DECLARE_METATYPE(primary_key)
                  

                  main.cpp:

                  #include <QGuiApplication>
                  #include <QQmlApplicationEngine>
                  #include <QtQml>
                  #include "TestClass.h"
                  
                  int main(int argc, char *argv[])
                  {
                      QGuiApplication app(argc, argv);
                  
                      qRegisterMetaType<primary_key>();
                      qmlRegisterType<TestClass>("pokus", 1, 0, "TestClass");
                  
                      QQmlApplicationEngine engine;
                      engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
                  
                      return app.exec();
                  }
                  

                  main.qml:

                  import QtQuick 2.4
                  import QtQuick.Controls 1.1
                  import QtQuick.Window 2.2
                  import pokus 1.0
                  
                  Window {
                      visible: true
                      Button {
                          anchors.centerIn: parent
                          text: "Klikni"
                          onClicked:
                              console.log(testObj.lastInsertRowid())
                      }
                      TestClass {
                          id: testObj
                      }
                  }
                  

                  When I run the program and click the button, I get qrc:/main.qml:14: Error: Unknown method return type: primary_key.

                  1 Reply Last reply
                  1

                  • Login

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