I keep getting this error for some reason. Everything seems to work when I use this to pass a C++ class instance to QML.

main.cpp:

QQmlApplicationEngine engine;
gui::Wallet wallet;
engine.rootContext()->setContextProperty("Wallet", &wallet);

But what I really want to do is create an uncreatable type and import it in my main.qml. In my main.cpp file, I've replaced the previous code with this line: qmlRegisterUncreatableType<gui::Wallet>("neroshop.Wallet", 1, 0, "Wallet", "Wallet cannot be instantiated directly.");

main.qml:

import QtQuick 2.12
import QtQuick.Controls 2.12

import neroshop.Wallet 1.0



ApplicationWindow {
    id: mainWindow
    visible: true
    title: qsTr("neroshop")
    width: 1280
    height: 720
    minimumWidth: 850
    minimumHeight: 500    
    color: "#ffffff"

                NeroshopComponents.ProgressBar {
                    id: moneroDaemonSyncBar
                    radius: daemonSyncBar.radius
                    foregroundColor: NeroshopComponents.Style.moneroOrangeColor
                    backgroundColor: "#8c8c8c"
                    hoverEnabled: true
                    anchors.verticalCenter: parent.verticalCenter
                    
                    Timer {
                        interval: 1 // trigger every x miliseconds
                        running: true
                        repeat: true
                        onTriggered: {
                            moneroDaemonSyncBar.value = Wallet.getSyncPercentage()
                        }
                    }                                    
                }
}

Header where I declare my wallet class:

#pragma once

#ifndef WALLET_PROXY_HPP_NEROSHOP
#define WALLET_PROXY_HPP_NEROSHOP

#include <QObject>
#include <QString>
#include <QStringList>
#include <QVariant>

#include <memory> // std::unique_ptr

#include "../core/wallet.hpp"

namespace neroshop {

namespace gui { // or just remove the "gui" namespace and rename gui::Wallet to WalletProxy?

class Wallet : public QObject, public neroshop::Wallet {
    Q_OBJECT 
    // properties (for use in QML)
    Q_PROPERTY(neroshop::Wallet* wallet READ getWallet WRITE setWallet);// NOTIFY wallet_changed);
    //Q_PROPERTY(<type> <variable_name> READ <get_function_name>)
public:    
    enum KeyfileStatus {
        KeyfileStatus_Ok = 0,
        KeyfileStatus_Wrong_Password,
        KeyfileStatus_No_Matching_Passwords,
        KeyfileStatus_Exists,
    };
    Q_ENUM(KeyfileStatus)
    // functions (for use in QML)
    ////explicit Wallet(QObject* parent = 0);
    Q_INVOKABLE int createRandomWallet(const QString& password, const QString& confirm_pwd, const QString& path) const;
    Q_INVOKABLE void closeWallet(bool save = false);
    Q_INVOKABLE QVariantMap createUniqueSubaddressObject(unsigned int account_idx, const QString & label = "");
    Q_INVOKABLE double getSyncPercentage() const;
    Q_INVOKABLE unsigned int getSyncHeight() const;
    Q_INVOKABLE unsigned int getSyncStartHeight() const;
    Q_INVOKABLE unsigned int getSyncEndHeight() const;
    Q_INVOKABLE QString getSyncMessage() const;
    Q_INVOKABLE QString getMnemonic() const;
    Q_INVOKABLE QStringList getMnemonicList() const;
    Q_INVOKABLE QString getPrimaryAddress() const;

    Q_INVOKABLE QStringList getAddressesAll() const;
    Q_INVOKABLE QStringList getAddressesUsed() const;
    Q_INVOKABLE QStringList getAddressesUnused() const;
    Q_INVOKABLE double getBalanceLocked(unsigned int account_index) const;
    Q_INVOKABLE double getBalanceLocked(unsigned int account_index, unsigned int subaddress_index) const;
    Q_INVOKABLE double getBalanceUnlocked(unsigned int account_index) const;
    Q_INVOKABLE double getBalanceUnlocked(unsigned int account_index, unsigned int subaddress_index) const;
    Q_INVOKABLE neroshop::Wallet * getWallet() const;
    Q_INVOKABLE void setWallet(const neroshop::Wallet* wallet);

    Q_INVOKABLE void daemonExecute(const QString& ip, const QString& port, bool confirm_external_bind, bool restricted_rpc, bool remote, QString data_dir, QString network_type, unsigned int restore_height);// const;
    Q_INVOKABLE bool isGenerated() const;
    Q_INVOKABLE bool fileExists(const QString& filename) const;
private:
    std::unique_ptr<neroshop::Wallet> wallet;
public:
    Wallet();
    ~Wallet();
};

}

}

#endif

I was expecting it to work right away but instead I got this error:

qrc:/qml/main.qml:186: TypeError: Property 'getSyncPercentage' of object [object Object] is not a function
qrc:/qml/main.qml:186: TypeError: Type error

getSyncPercentage is indeed a function attached to the Wallet class. I don't know why it does not recognize it as a function. Am I doing something wrong here?

1

There are 1 answers

5
Jürgen Lutz On

Why do you use an uncreatable type? This is only usefull for e.g. enums. You then try to create an instance. To make that work you should use

 qmlRegisterType<gui::Wallet>("Wallet", 1, 0, "Wallet");

Or you can register a singleton instance, if you what just one instance of our class.