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. I can't use a function from C ++
Forum Updated to NodeBB v4.3 + New Features

I can't use a function from C ++

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 2 Posters 320 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.
  • M Offline
    M Offline
    Mikeeeeee
    wrote on 3 Apr 2019, 16:15 last edited by
    #1

    Hi.
    I can't use a function from C ++.
    Created a class:

    class DataWorking : public QObject
    {
    public:
        DataWorking();
        ~DataWorking();
        Q_INVOKABLE  int randomNumber;
        int winNumber;
        int progressNumber;
        int roundNumber;// число раундов
     
    public slots:
        Q_INVOKABLE  void setRandomNamber(int first, int last);
     
    };
    
    DataWorking::DataWorking()
    {
        winNumber = 0;
        progressNumber = 0;
        roundNumber = 20;
    }
    
    DataWorking::~DataWorking()
    {
        winNumber = 0;
        progressNumber = 0;
    }
    
    void DataWorking::setRandomNamber(int first, int last)
    {
        //randomNumber = Random::get(first, last);
        randomNumber=654;
        qDebug()<<"randomNamber = "<<randomNumber;
    }
    
    

    main.cpp:

    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
    
        DataWorking dataWorking;
    
        QQmlContext *context = engine.rootContext();   
           context->setContextProperty("dataWorking", &dataWorking);
    
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
    
        return app.exec();
    }
    

    I specify connection in main.qml:

       Connections
         {
             target: dataWorking // Указываем целевое соединение
     
         }
    

    and trying to call a class function:

       page1Button0.onClicked: //left button
        {
            dataWorking.setRandomNamber(1,55)
        }
    

    but I get the error: Property 'setRandomNamber' of object QObject (0x22fd40) is not a function.
    Tell me please, how can I use this function?

    K 1 Reply Last reply 3 Apr 2019, 16:50
    0
    • M Mikeeeeee
      3 Apr 2019, 16:15

      Hi.
      I can't use a function from C ++.
      Created a class:

      class DataWorking : public QObject
      {
      public:
          DataWorking();
          ~DataWorking();
          Q_INVOKABLE  int randomNumber;
          int winNumber;
          int progressNumber;
          int roundNumber;// число раундов
       
      public slots:
          Q_INVOKABLE  void setRandomNamber(int first, int last);
       
      };
      
      DataWorking::DataWorking()
      {
          winNumber = 0;
          progressNumber = 0;
          roundNumber = 20;
      }
      
      DataWorking::~DataWorking()
      {
          winNumber = 0;
          progressNumber = 0;
      }
      
      void DataWorking::setRandomNamber(int first, int last)
      {
          //randomNumber = Random::get(first, last);
          randomNumber=654;
          qDebug()<<"randomNamber = "<<randomNumber;
      }
      
      

      main.cpp:

      int main(int argc, char *argv[])
      {
          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
      
          QGuiApplication app(argc, argv);
      
          QQmlApplicationEngine engine;
      
          DataWorking dataWorking;
      
          QQmlContext *context = engine.rootContext();   
             context->setContextProperty("dataWorking", &dataWorking);
      
          engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
          if (engine.rootObjects().isEmpty())
              return -1;
      
          return app.exec();
      }
      

      I specify connection in main.qml:

         Connections
           {
               target: dataWorking // Указываем целевое соединение
       
           }
      

      and trying to call a class function:

         page1Button0.onClicked: //left button
          {
              dataWorking.setRandomNamber(1,55)
          }
      

      but I get the error: Property 'setRandomNamber' of object QObject (0x22fd40) is not a function.
      Tell me please, how can I use this function?

      K Offline
      K Offline
      KroMignon
      wrote on 3 Apr 2019, 16:50 last edited by KroMignon 4 Mar 2019, 17:11
      #2

      @Mikeeeeee said in I can't use a function from C ++:

      DataWorking

      I think you have to change the DataWorking constructor, to call QObject constructor, and very important add Q_OBJECT macro to enable QObject magic:

      class DataWorking : public QObject
      {
          Q_OBJECT
      public:
          explicit DataWorking(QObject *parent = 0);
       ...
      

      in cpp

      DataWorking::DataWorking(QObject *parent) : QObject(parent)
      {
          winNumber = 0;
          progressNumber = 0;
          roundNumber = 20;
      }
      

      Other remark, Q_INVOKABLE is only required if function is not a slot. slots are accessible from QML.

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

      1 Reply Last reply
      5

      1/2

      3 Apr 2019, 16:15

      • Login

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