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. C++ template member method callable from QML
Forum Update on Monday, May 27th 2025

C++ template member method callable from QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 1.8k 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.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by
    #1

    Hi,
    I need a function that can take an int,float,double or string

    class PlcCom : public QObject
    {
        Q_OBJECT
    public:
        explicit PlcCom(QObject *parent = nullptr);
    
        template<typename T>
        void setValue(T v){
            QString input = typeid(v).name();
    
        }
    }
    

    but i can't call this method from QML..

    If i mark this method as public slot i will get

    Error: Template function as signal or slot

    If i mark it Q_INVOKABKE i get :

    moc_plccom.cpp:180: erreur : expected type-specifier before 'T'
    case 18: _t->setValue((reinterpret_cast< T()>(_a[1]))); break;
    moc_plccom.cpp:180: erreur : expected '>' before 'T'
    moc_plccom.cpp:180: erreur : expected '>' before 'T'
    moc_plccom.cpp:180: erreur : expected primary-expression before ')' token
    case 18: _t->setValue((reinterpret_cast< T()>(_a[1]))); break;
    moc_plccom.cpp:180: erreur : 'T' was not declared in this scope
    case 18: _t->setValue((reinterpret_cast< T()>(_a[1]))); break;

    How to call a tamplate member function from QML please ?

    KroMignonK 1 Reply Last reply
    0
    • ODБOïO ODБOï

      Hi,
      I need a function that can take an int,float,double or string

      class PlcCom : public QObject
      {
          Q_OBJECT
      public:
          explicit PlcCom(QObject *parent = nullptr);
      
          template<typename T>
          void setValue(T v){
              QString input = typeid(v).name();
      
          }
      }
      

      but i can't call this method from QML..

      If i mark this method as public slot i will get

      Error: Template function as signal or slot

      If i mark it Q_INVOKABKE i get :

      moc_plccom.cpp:180: erreur : expected type-specifier before 'T'
      case 18: _t->setValue((reinterpret_cast< T()>(_a[1]))); break;
      moc_plccom.cpp:180: erreur : expected '>' before 'T'
      moc_plccom.cpp:180: erreur : expected '>' before 'T'
      moc_plccom.cpp:180: erreur : expected primary-expression before ')' token
      case 18: _t->setValue((reinterpret_cast< T()>(_a[1]))); break;
      moc_plccom.cpp:180: erreur : 'T' was not declared in this scope
      case 18: _t->setValue((reinterpret_cast< T()>(_a[1]))); break;

      How to call a tamplate member function from QML please ?

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #2

      @LeLev said in C++ template member method callable from QML:

      How to call a tamplate member function from QML please ?

      I do not think this is possible.
      I think the best way is to used QVariant as parameter, because QML needs to be able to convert it own type into matching C++ type.

      Something like this:

      class PlcCom : public QObject
      {
          Q_OBJECT
      public:
          explicit PlcCom(QObject *parent = nullptr);
      
          Q_INVOKABLE void setValue(const QVariant& v){
              if(v.isValid()) {
                  qDebug() << "Received" << v.typeName() << "value";
              }
              else {
                  qDebug() << "Invalid value!";
              }
          }
      }
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      ODБOïO 1 Reply Last reply
      2
      • KroMignonK KroMignon

        @LeLev said in C++ template member method callable from QML:

        How to call a tamplate member function from QML please ?

        I do not think this is possible.
        I think the best way is to used QVariant as parameter, because QML needs to be able to convert it own type into matching C++ type.

        Something like this:

        class PlcCom : public QObject
        {
            Q_OBJECT
        public:
            explicit PlcCom(QObject *parent = nullptr);
        
            Q_INVOKABLE void setValue(const QVariant& v){
                if(v.isValid()) {
                    qDebug() << "Received" << v.typeName() << "value";
                }
                else {
                    qDebug() << "Invalid value!";
                }
            }
        }
        
        ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by
        #3

        hi @KroMignon
        thak you very much for the quick answer

        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