vendredi 3 janvier 2020

How is a C++ function made accessible from QML?

I tried to make a C++ function accessible from QML by creating the following files.

MyObject.h

#ifndef MYOBJECT_H
#define MYOBJECT_H

#include <QObject>

class MyObject : public QObject
{
    Q_OBJECT
public:
    explicit MyObject(QObject *parent = nullptr);
    Q_INVOKABLE int registernew(int Sno, std::string Name,long long PhoneNo, std::string Country, std::string State, std::string District, int PhoneLine);
    Q_INVOKABLE int querydistrict();
};

#endif // MYOBJECT_H

MyObject.cpp

#include "myobject.h"
#include<QtSql/QSqlDatabase>
#include<QtSql/QSql>
#include<QDebug>
#include<QtSql/QSqlQuery>
#include<QVariant>

bool createConnections();
MyObject::MyObject(QObject *parent) : QObject(parent)
{
}


Q_INVOKABLE int MyObject::registernew(int Sno,std::string Name,long long PhoneNo, std::string Country, std::string State, std::string District, int PhoneLine)
{
    createConnections();
    QSqlQuery query;
    query.exec("CREATE TABLE Persons ("
               "Name varchar(30),"
               "Phone varchar(15),"
               "Country varchar(20),"
               "State varchar(20),"
               "District varchar(20),"
               "Phoneline int"
               ");");
    query.prepare("INSERT INTO Persons (Sno,Name,Phone,Country,State,District,Phoneline) VALUES (:Sno,:Name,:Phone,:Country,:State,:District,:Phoneline)");
    query.bindValue(":Sno",Sno);
    query.bindValue(":Name:", QString::fromStdString(Name));
    query.bindValue(":Phone",PhoneNo);
    query.bindValue(":Country",QString::fromStdString(Country));
    query.bindValue(":State",QString::fromStdString(State));
    query.bindValue(":District",QString::fromStdString(District));
    query.bindValue(":Phoneline",PhoneLine);
    query.exec();

    return 0;
}

Q_INVOKABLE int MyObject::querydistrict()
{
    QSqlQuery query;
    query.exec("SELECT Name FROM Persons WHERE District='Kottayam'");
    while (query.next()) {
            QString name = query.value(0).toString();
            qDebug()<<name;
       }
    return 0;
}



bool createConnections()
{
    // create the default database connection
    QSqlDatabase DB = QSqlDatabase::addDatabase("QSQLITE");
    DB.setDatabaseName( "PKDB" );
    DB.setUserName( "PolyAniline" );
    DB.setPassword( "JesusChrist8456" );
    DB.setHostName( "PUTER" );
    if ( ! DB.open())
    {
        qDebug("aaaargh");
        return false;
    }

    return true;
}

main.cpp

#include <QGuiApplication>
#include<QQmlApplicationEngine>
#include "myobject.h"




int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    qmlRegisterType<MyObject>("io.qt.examples.MyObject", 1, 0, "MyObject");
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

main.qml

import QtQuick 2.12
import QtQuick.Controls 2.5
import QtQuick.Window 2.12
import io.qt.examples.MyObject 1.0

ApplicationWindow {
    visible: true
    width: Screen.width
    height: Screen.height
    title: qsTr("app")

    MyObject{
        id: myobject
    }

    SwipeView {
        id: swipeView
        anchors.fill: parent
        currentIndex: 0

        Page1Form {
            button.onClicked:
            {
                MyObject.registernew();
}
        }

        Page2Form {
        }



    }
    footer:
        PageIndicator {
            id: pageIndicator
            x: Screen.width/2-30
            y: 450
            count: 2
            currentIndex: swipeView.currentIndex
        }
}

I get the error:

qrc:/main.qml:24: TypeError: Property 'registernew' of object [object Object] is not a function

Why does this happen? I followed the documentation exactly. And there's no registernew property of the class that is'nt a function. What I'm basically trying to do is add new entries to an SQLite database with data retrieved from the QML forms. I know the arguments don't implement that right now, but first I figured I'll have to get the function up and running. Thank you!!

Aucun commentaire:

Enregistrer un commentaire