0

I have exposed C++ enum to Qt 5.7 QML, like it was described in this question and here is class declaration/implementation:

#ifndef SPLASHSCREENLOGVIEWERMESSAGESTATUS_H
#define SPLASHSCREENLOGVIEWERMESSAGESTATUS_H

#include <QObject>
#include <QQmlEngine>

/**
 * @class Enum class for splash screen log viewer message status
 */
class SplashScreenLogViewerMessageStatus : public QObject
{
    Q_OBJECT

public:
    /**
     *! Splash screen log viewer message status enum
     */
    enum messageStatus
    {
        STATUS_MESSAGE_LOADING=0x01,               /*!< Message loading state */
        STATUS_MESSAGE_LOAD_OK=0x02,               /*!< Message load OK state */
        STATUS_MESSAGE_LOAD_FAILED=0x03            /*!< Message load FAILED state */
    };

    Q_ENUM(messageStatus)

public:
    /**
     * @brief Constructor
     */
    inline explicit SplashScreenLogViewerMessageStatus()
        : QObject()
    {
    }   // constructor

    /**
     * @brief QML type registration method
     */
    inline static void registerQMLType()
    {
        qmlRegisterType<SplashScreenLogViewerMessageStatus>("com.example",
                                                            1,
                                                            0,
                                                            "SplashScreenLogViewerMessageType");
    }   // registerQMLType
};  // SplashScreenLogViewerMessageStatus (class)

#endif // SPLASHSCREENLOGVIEWERMESSAGESTATUS_H

and I use it on following class:

#ifndef SPLASHSCREENLOGVIEWERMODELRECORD_H
#define SPLASHSCREENLOGVIEWERMODELRECORD_H

#include <QString>

#include "splashscreenlogviewermessagestatus.h"

/**
 * @class Splash screen log viewer model record
 */
class SplashScreenLogViewerModelRecord
{
private:
    /**
     * @brief Message text
     */
    QString messageText;

    /**
     * @brief Message status object
     */
    SplashScreenLogViewerMessageStatus messageStatusObj;

public:
    /**
     * @brief Constructor
     */
    SplashScreenLogViewerModelRecord();

    /**
     * @brief Constructor
     */
    SplashScreenLogViewerModelRecord(const QString& messageText,
                                     const SplashScreenLogViewerMessageStatus& messageStatusObj);

    /**
     * @brief Message text getter
     * @return message text
     */
    QString getMessageText() const;

    /**
     * @brief Message text setter
     * @param text
     */
    void setMessageText(const QString& text);

    /**
     * @brief Message status getter
     * @return message status
     */
    QString getMessageStatus() const;

    /**
     * @brief Message status setter
     * @param status
     */
    void setMessageStatus(const SplashScreenLogViewerMessageStatus& status);
};

#endif // SPLASHSCREENLOGVIEWERMODELRECORD_H

and implementation:

#include "splashscreenlogviewermodelrecord.h"

SplashScreenLogViewerModelRecord::SplashScreenLogViewerModelRecord()
{
    SplashScreenLogViewerMessageStatus messageStatus;

    this->setMessageText(QString(""));
    this->setMessageStatus(messageStatus);
}   // constructor

QString SplashScreenLogViewerModelRecord::getMessageText() const
{
    return this->messageText;
}   // getMessageText

void SplashScreenLogViewerModelRecord::setMessageText(const QString& text)
{
    this->messageText=text;
}   // setMessageText

QString SplashScreenLogViewerModelRecord::getMessageStatus() const
{
    if(this->messageStatusObj.messageStatus==SplashScreenLogViewerMessageStatus.STATUS_MESSAGE_LOADING)
    {
        return QString("Loading ...");
    }
//    else if(this->messageStatusObj.messageStatus==SplashScreenLogViewerMessageStatus.STATUS_MESSAGE_LOAD_OK)
//    {
//        return QString("Load OK.");
//    }
//    else if(this->messageStatusObj.messageStatus==SplashScreenLogViewerMessageStatus.STATUS_MESSAGE_LOAD_FAILED)
//    {
//        return QString("Load FAILED.");
//    }
    else
    {
        return QString("");
    };
}   // getMessageStatus

void SplashScreenLogViewerModelRecord::setMessageStatus(const SplashScreenLogViewerMessageStatus &status)
{
//    this->messageStatus.SplashScreenLogViewerMessageStatusEnum=status.SplashScreenLogViewerMessageStatusEnum;
//    this->messageStatus.messageStatus=status;
}   // setMessageStatus

If I try to build project with these files, I get following error:

models/splashscreenlogviewermodelrecord.cpp: In member function ‘QString SplashScreenLogViewerModelRecord::getMessageStatus() const’: models/splashscreenlogviewermodelrecord.cpp:23:31: error: invalid use of ‘enum SplashScreenLogViewerMessageStatus::messageStatus’
     if(this->messageStatusObj.messageStatus ==SplashScreenLogViewerMessageStatus.STATUS_MESSAGE_LOADING)
                               ^~~~~~~~~~~~~ models/splashscreenlogviewermodelrecord.cpp:23:81: error: expected primary-expression before ‘.’ token
     if(this->messageStatusObj.messageStatus ==SplashScreenLogViewerMessageStatus.STATUS_MESSAGE_LOADING)

The error resides in method QString SplashScreenLogViewerModelRecord::getMessageStatus() const, specificaly in its first line:

if(this->messageStatusObj.messageStatus==SplashScreenLogViewerMessageStatus.STATUS_MESSAGE_LOADING)

How do I compare enum value? I am using Qt Creator 4.9.2 on Ubuntu 18.04.3 LTS 64bit.

KernelPanic
  • 2,328
  • 7
  • 47
  • 90

1 Answers1

3

Getting rid of all the Qt obfuscation, what's left is roughly this:

class Foo {
public:
  enum fooValues { one, two };
};

void user(const Foo& foo) {
  if (foo.fooValues == Foo::one) {
    // doesn't compile
  }
}

And the problem here is that Foo::fooValues is a nested type, not a member variable. You never declare a member variable of that type.

You have been slightly mislead by the linked answers, because they don't do that either. But they don't try to use it this way either. Look at the linked question instead about what you need to do.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157