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. Touble calling a simple c++ function in QML
Forum Updated to NodeBB v4.3 + New Features

Touble calling a simple c++ function in QML

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 869 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.
  • X Offline
    X Offline
    xalam
    wrote on last edited by
    #1

    I am new to Qt/Qml and want to integrate C++ class into qml project. I want to use a method SquareIt() from Qml from a class Calculator. Here is the class I wrote

    @#ifndef CALCULATOR_H
    #define CALCULATOR_H

    #include <QObject>

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

    Q_INVOKABLE int SqaureIt(int a);
    

    signals:

    public slots:

    };

    #endif // CALCULATOR_H
    @

    Here is how I am binding them. Note I didn't find any example which binds with QQmlApplicationEngine, is that a problem?

    @#include <QGuiApplication>
    #include <QQmlApplicationEngine>

    #include "calculator.h"

    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    
    Calculator calc;
    
    engine.setProperty("TheCalc", &calc);
    
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
    return app.exec();
    

    }@

    I get error:

    @C:\Users...\My Qt Projects\Load\main.cpp:14: error: C2248: 'QVariant::QVariant' : cannot access private member declared in class 'QVariant'@

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      It should rather be:

      @engine.rootContext()->setContextProperty("TheCalc", &calc);@

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      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