Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

(Newbie warning) TypeError, function from backend doesn't exist



  • Hey. This is my first attempt at a QT application and I've been having quite a bit of trouble. Currently, I am getting the following error:

    qrc:/main.qml:86: TypeError: Property 'generatetown' of object [object Object] is not a function
    qrc:/main.qml:91: TypeError: Property 'wMagic1' of object [object Object] is not a function
    qrc:/main.qml:96: TypeError: Property 'wMagic2' of object [object Object] is not a function
    

    Here is my code, main.cpp:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QObject>
    #include <QString>
    #include "includes/tom.hpp"
    #include "includes/dnd.hpp"
    #include "includes/classes.hpp"
    #include "includes/buttonclass.hpp"
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
        qmlRegisterType<buttonactions>("buttonactionsimp", 1, 0, "Buttonactions");
    
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
    
    
        return app.exec();
    }
    
    

    buttonclass.hpp:

    #ifndef BUTTONCLASS_HPP
    #define BUTTONCLASS_HPP
    
    #include "tom.hpp"
    #include "dnd.hpp"
    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QObject>
    #include <QString>
    
    class buttonactions : public QObject{
        Q_OBJECT
        Q_PROPERTY(string text MEMBER bigtext READ gettext WRITE generatetown WRITE wMagic1 WRITE wMagic2)
    public:
        explicit buttonactions(QObject *parent = nullptr) : QObject(parent){};
        virtual ~buttonactions() {};
        string gettext() {
            return bigtext;
        }
        void generatetown(string pop) {
            bigtext = genTown(std::atoi(pop.c_str()));
        };
         void wMagic1(string rolls) {
            bigtext = gennlrmev2(std::atoi(rolls.c_str()));
        };
         void wMagic2(string nom) {
            bigtext = nlrmev2(std::atoi(nom.c_str()));
        };
    private:
         string bigtext;
    };
    #endif // BUTTONCLASS_HPP
    
    

    main.qml:

    import QtQuick 2.6
    import QtQuick.Controls 2.0
    import QtQuick.Controls.Universal 2.0
    import buttonactionsimp 1.0
    
    ApplicationWindow {
        id: root
        width: 480
        height: 480
        color: "#000000"
        property Buttonactions buttonactions: null
        title: "DND Tools"
        visible: true
    
        Button {
            id: button
            x: 291
            width: 138
            height: 50
            text: qsTr("Generate Town Levels")
            anchors.right: parent.right
            anchors.rightMargin: 51
            anchors.top: parent.top
            anchors.topMargin: 150
            property string property0: "none.none"
            transformOrigin: Item.Center
        }
    
        Button {
            id: button1
            x: 291
            width: 138
            text: qsTr("Wild Magic")
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 215
            anchors.right: parent.right
            anchors.rightMargin: 51
            anchors.top: parent.top
            anchors.topMargin: 215
            transformOrigin: Item.Center
        }
    
        Button {
            id: button2
            x: 291
            y: 279
            width: 138
            height: 50
            text: qsTr("Wild Magic Roll")
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 151
            anchors.right: parent.right
            anchors.rightMargin: 51
            transformOrigin: Item.Center
        }
    
        TextInput {
            id: textInput
            x: 349
            width: 80
            height: 20
            color: "#ffffff"
            text: qsTr("0")
            anchors.right: parent.right
            anchors.rightMargin: 51
            anchors.top: parent.top
            anchors.topMargin: 93
            font.pixelSize: 12
        }
    
        Text {
            id: element
            x: 291
            color: "#ffffff"
            text: qsTr("INPUT:")
            anchors.right: parent.right
            anchors.rightMargin: 151
            anchors.top: parent.top
            anchors.topMargin: 93
            styleColor: "#ffffff"
            font.pixelSize: 12
        }
    
        Connections {
            target: button
            onClicked: Buttonactions.generatetown(textInput.text)
        }
    
        Connections {
            target: button1
            onClicked: Buttonactions.wMagic1(textInput.text)
        }
    
        Connections {
            target: button2
            onClicked: Buttonactions.wMagic2(textInput.text)
        }
    
        TextEdit {
            id: textEdit
            width: 183
            color: "#ffffff"
            text: "output here"
            anchors.left: parent.left
            anchors.leftMargin: 54
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 151
            anchors.top: parent.top
            anchors.topMargin: 93
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
            font.pixelSize: 12
        }
    
    }
    
    /*##^##
    Designer {
        D{i:2;anchors_height:50}D{i:9;anchors_height:236;anchors_x:54;anchors_y:93}
    }
    ##^##*/
    
    

    The other headers are too big to put here, but they just define certain functions and such for future development. I didn't quite get that far yet. I can guarantee they do not have any issues. All of the functions you see in buttonclass.hpp are declared in dnd.hpp: inline string genTown(uint64_t pop), inline string gennlrmev2(short rolls), inline string nlrmev2(short number). If you're wondering why i did stuff weird with the argument types, it is because it wouldn't work any other way. Some insight would be great.

    I appreciate it


  • Qt Champions 2017

    C++ methods cannot be called directly. Methods have to be either slots or Q_INVOKABLE.

    void generatetown(string pop) 
    void wMagic1(string rolls) 
    void wMagic2(string nom)
    

    Above methods need to be declared as slots.



  • I've declared them as slots. I am unsure how the slots system works. I've been reading up on it from here. How would i create a signal for the buttons to call the slot?

    I also wanna apologize for the horrible casing lol


Log in to reply