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. Calling C++ functions from QML doesn't work

Calling C++ functions from QML doesn't work

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 816 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.
  • S Offline
    S Offline
    Slei
    wrote on last edited by
    #1

    Hey I've tried to call a c++ function from QML.

    let say i have following class

    ```
    
    class ProxySettings : public QObject
    	{
    	public:
    		explicit ProxySettings(QObject* parent = nullptr);
    		virtual ~ProxySettings();
    
    		Q_INVOKABLE QString getHttp()
    		{
    			return "Test Ogden port";
    		}
    
    public slots:
    		void help()
    		{
    			std::cout << "put slei abc" << std::endl;
    		}
    	};
    
    
    
    which i register the following way:
    	qmlRegisterType<ProxySettings>("remotedui.qt.proxysettings", 1, 0, "ProxySettingsNative");
    
    
    
    
    and I try to use it in QML:
    
    import QtQuick 2.9
    import QtQuick.Window 2.0
    import QtQuick.Controls 2.0
    import remotedui.qt.proxysettings 1.0
    
    Window
    {
    	property alias qProxySettings : cpp_qProxySettings
    	
    	ProxySettingsNative
    	{
    		id: cpp_qProxySettings
    	}
    
    	property var http: getHttp();
    
    
    	function reset()
    	{
    		cpp_qProxySettings.help();
    		http = cpp_qProxySettings.getHttp()
    	}
    
    }
    
    

    But I always geht following error:
    qrc:/ui/ProxySettings.qml:31: TypeError: Property 'help' of object QObject(0x180550d8) is not a function

    same with getHttp.

    The ProxySettings get created correctly(when trying to debug through the cpp class) and is the QObject pointer from the error log is the same.

    am i missing something?

    J.HilkJ 1 Reply Last reply
    0
    • S Slei

      Hey I've tried to call a c++ function from QML.

      let say i have following class

      ```
      
      class ProxySettings : public QObject
      	{
      	public:
      		explicit ProxySettings(QObject* parent = nullptr);
      		virtual ~ProxySettings();
      
      		Q_INVOKABLE QString getHttp()
      		{
      			return "Test Ogden port";
      		}
      
      public slots:
      		void help()
      		{
      			std::cout << "put slei abc" << std::endl;
      		}
      	};
      
      
      
      which i register the following way:
      	qmlRegisterType<ProxySettings>("remotedui.qt.proxysettings", 1, 0, "ProxySettingsNative");
      
      
      
      
      and I try to use it in QML:
      
      import QtQuick 2.9
      import QtQuick.Window 2.0
      import QtQuick.Controls 2.0
      import remotedui.qt.proxysettings 1.0
      
      Window
      {
      	property alias qProxySettings : cpp_qProxySettings
      	
      	ProxySettingsNative
      	{
      		id: cpp_qProxySettings
      	}
      
      	property var http: getHttp();
      
      
      	function reset()
      	{
      		cpp_qProxySettings.help();
      		http = cpp_qProxySettings.getHttp()
      	}
      
      }
      
      

      But I always geht following error:
      qrc:/ui/ProxySettings.qml:31: TypeError: Property 'help' of object QObject(0x180550d8) is not a function

      same with getHttp.

      The ProxySettings get created correctly(when trying to debug through the cpp class) and is the QObject pointer from the error log is the same.

      am i missing something?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @Slei
      two things come to my mind,

      add the Q_Object macro, its missing in your code example
      and the help function is missing the Q_INVOKABLE macro


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      3
      • S Offline
        S Offline
        Slei
        wrote on last edited by Slei
        #3

        uff, i did indeed miss the Q_OBJECT macro and didn't see it ._. adding the missing Q_OBJECt did the job 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