Important: Please read the Qt Code of Conduct -

[SOLVED] Passing QML variant to C++ method

  • Hi,

    I'm trying to make a game where I have to able to create and move game pieces around a board. The way my code is setup now is I have a C++ object that acts as the game engine and C++ objects that act as the game pieces.

    I create a QML element dynamically containing a variant property which stores a pointer to the C++ game piece object.

    What would like to do now, is to be able to call a C++ method via QML, with the variant property as an argument. However, I get the error "Error: Unknown method parameter type: HivePiece*" (HivePiece* being the pointer that I store).

    Is there a way to do this? Or should I should use a different code structure?

    Thanks in advance,


  • Hi,

    1. Are your object QObject derivatives?
    2. Do you register your types with Q_DECLARE_METATYPE?

    I've had issues that Qt sends QVariant of a different type than I expect, but that's probably a not related here.

    A piece of QML code (C++ part is rather obvious) how you manage objects there would be helpful to understand your implementation.

  • Hi,

    Thanks for the reply. I checked my code and it seems I forgot to register the class. Registering the class solved my problem :)

Log in to reply