10

I am on learning to QML with Qt and get some trouble with passing enum class to qml.

When I use signal with int parameters - it's all right and code work perfectly.

But, and here the trouble, if I use signal with some enum class parameters I have undefined values in qml signal handler. I tried to register enum class through qmlRegisterType but it's not working. I will be grateful for the help.

Here the code:

Some Helper class

class Helper : public QObject
{
    Q_OBJECT
    Q_ENUMS(Requester)
    Q_ENUMS(JANSWER)

public:

enum class Requester {
    ReqLogin,
    ReqNull,
    ReqSave,
    ReqError,
    ReqUnknown
};

enum class JANSWER {
    OK,
    Complete,
    Error,
    Unknown
};

};

main.cpp

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    qmlRegisterType<JsonPackWorker>("com.jsonpackworker", 1, 0, "JsonPackWorker");
    qmlRegisterType<Helper>("com.Helper", 1, 0, "Helper");

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

JsonPackWorker.h

class JsonPackWorker : public QObject
{
    Q_OBJECT
public:
    <...>

signals:    
    <...>
    void sendAnswer(Helper::Requester req, Helper::JANSWER answer);

public slots:
    <...>
};

Somewehere in JsonPackWorker.cpp

emit sendAnswer(Helper::Requester::ReqNull, Helper::JANSWER::OK);

main.qml

<...>
import com.jsonpackworker 1.0
import com.Helper 1.0

ApplicationWindow {
    id: mainWindow

    // Requests in answers
    property int reqLogin: Helper.ReqLogin

    <...>

    JsonPackWorker {
        id: packWorker
        <...>
        onSendAnswer: {
            // Here I've got undefined undefined if there enum class parameters
            console.log(req + " " + answer)
            switch(req) {
            case reqLogin: loginDial.checkLogin(answer)
                break;
            default:
            }
        }
    }

}
5
  • How is it not working? Do you get build errors? Crashes at run-time? Unexpected results? Something else? Please elaborate. And if you haven't done so yet, please read about how to ask good questions. Commented May 24, 2016 at 13:51
  • Does it work by changing enum class to enum ? Commented May 24, 2016 at 13:52
  • @Joachim Pileborg, I wrote that I've got an undefined values in handler. Code shows console.log() where I commented that part with description. Commented May 24, 2016 at 13:53
  • 1
    @coyotte508, if I changes enum class to enum I should change my signal to void sendAnswer(int req, int answer); and it will work perfectly. But it's not interesting way. Commented May 24, 2016 at 13:56
  • For what it's worth, it works when the enum belongs to the JsonPackWorker class itself. As for why it doesn't work when using the enum of another registered class, I can only assume it's a bug. Commented May 24, 2016 at 14:16

1 Answer 1

6

The problem was in Helper class (thanks to Qt community).

class Helper {
   ...
};
Q_DECLARE_METATYPE(Helper::Requester)
Q_DECLARE_METATYPE(Helper::JANSWER)
Sign up to request clarification or add additional context in comments.

1 Comment

For more info read this official doc: Enumeration Types as Signal and Method Parameters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.