【问题标题】:Method in class inheriting QObject is identified as property when called, doesn't run method继承 QObject 的类中的方法在调用时被标识为属性,不运行方法
【发布时间】:2020-05-21 18:12:45
【问题描述】:

基本上,我有一个我正在开发的项目,它应该是井字游戏的图形游戏,并且从主程序调用一个名为“Game”的类(以及任何方法调用) qml 文件不起作用。返回此错误:

TypeError: Property 'takeTurn' of object [object Object] is not a function

我在这个类中继承了 QObject,并包含了 Q_OBJECT 宏并将该方法标记为 Q_INVOKABLE。

代码编译和链接都很好,这是一个运行时错误。

以下是相关代码以提供帮助:

Game.hpp:

#define GAME_HPP

#include "Board.hpp"
#include <ostream>

#include <QObject>

class Game : public QObject
{
    Q_OBJECT;
    public:
        //...
        Q_INVOKABLE void takeTurn(int x, int y);
        Q_INVOKABLE bool checkWin();

        friend std::ostream& operator<<(std::ostream&, const Game&);
    private:
        char player_;
        int turns_;
        Board board_;
};

std::ostream& operator<<(std::ostream&, const Game&);

#endif // GAME_HPP

游戏.cpp:

#include <iostream>

#include <QObject>
#include <QApplication>

using std::cout;
using std::endl;

//...
void Game::takeTurn(int x, int y)
{
    QWindow* app = QApplication::topLevelWindows()[0];
    cout << app << endl;
    board_.setTile(x, y, player_);
    player_ == 'X' ? player_ = 'O' : player_ = 'X';
    turns_++;
}
//...

main.cpp:

#include "Game.hpp"

#include <iostream>
#include <QGuiApplication>
#include <QQmlApplicationEngine>

using std::cout;
using std::endl;

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

    QGuiApplication app(argc, argv);
    qmlRegisterType<Game>("com.myself", 1, 0, "Game");
    qmlRegisterType<Board>("com.myself", 1, 0, "Board");

    QQmlApplicationEngine engine;
    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.Window 2.12
import com.myself 1.0

Window {
    visible: true
    width: 600
    height: 600
    title: qsTr("TicTacToe")

    Item {
        //...

        MouseArea {
            id: mouseArea1
            anchors.fill: parent
            onClicked: {
                Game.takeTurn(0,0)
            }
        }
    }
//...

}
//...

【问题讨论】:

    标签: c++ qt


    【解决方案1】:

    您已经将Game注册为一个类型,所以您需要做的是创建一个该类型的对象,错误指向该问题:

    // ...
    Window {
        visible: true
        width: 600
        height: 600
        title: qsTr("TicTacToe")
        Game {
            id: game
        }
    
        // ...
        Item {
            // ...
            MouseArea {
                id: mouseArea1
                anchors.fill: parent
                onClicked: {
                    game.takeTurn(0,0)
                }
           }
        }
        // ...

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-08
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    相关资源
    最近更新 更多