Indeed this is the case there is a problem with this. I was hoping to use this to explicitly do apple-to-apple comparison: (1) make the *.qml file easily readable by replacing hard coded numbers, (2) force errors when interpreting *.qml files when enum changes, and (3) make the code easily searchable to enum item.
However, there is a limitation here that I will not be able to get around.
My code is the following:
FaultWarningItem.h
#ifndef FAULTWARNINGITEM_H
#define FAULTWARNINGITEM_H
#include <QObject>
// Fault Warning Container Class
class FaultWarningItem
{
Q_GADGET
Q_ENUMS(ItemType)
public:
enum ItemType
{
TypeUnspecified = -1,
TypeNoError = 0,
TypeWarning,
TypeFault
};
};
#endif // FAULTWARNINGITEM_H
FaultWarningModel.h
#ifndef FAULTWANRINGMODEL_H
#define FAULTWANRINGMODEL_H
#include <QObject>
#include "FaultWarningItem.h"
class FaultWarningModel: public QObject {
Q_OBJECT
public:
FaultWarningModel(QObject *parent=0);
~FaultWarningModel();
Q_INVOKABLE FaultWarningItem::ItemType getItemErrorType(void) const;
Q_INVOKABLE int getItemErrorTypeAsInt(void) const;
Q_INVOKABLE void setNextFaultWarning();
int getCount() const;
private:
int m_count;
FaultWarningItem::ItemType m_FaultWarningItem;
signals:
void countChanged(int);
};
#endif // FAULTWANRINGMODEL_H
FaultWarningModel.cpp
#include "FaultWarningModel.h"
#include <QQml.h>
#include <QDebug>
FaultWarningModel::FaultWarningModel(QObject *parent) :
QObject(parent)
{
m_FaultWarningItem = FaultWarningItem::TypeUnspecified;
int statusType = qmlRegisterUncreatableType<FaultWarningItem>
(
"ItemType", // const char * uri,
1, // int versionMajor,
0, // int versionMinor,
"ItemType", // const char * qmlName,
"ItemType type cannot be created." // const QString & message
);
qDebug() << "statusType: " << statusType;
}
FaultWarningItem::ItemType FaultWarningModel::getItemErrorType(void) const
{
return m_FaultWarningItem;
}
int FaultWarningModel::getItemErrorTypeAsInt() const
{
int iValue = (int) m_FaultWarningItem;
return iValue;
}
main.qml
import QtQuick 2.0
// The enum types need to be imported before they can be used.
import ItemType 1.0
Rectangle {
width: 200
height: 300
property int currentFaultWarningType: ItemType.TypeUnspecified
Column {
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
spacing: 10
// Toggles the printer type
Rectangle {
id: buttonFaultWaring
width: 100
height: 50
color: "gainsboro"
border.color: "black"
border.width: 1
Text {
text: "TOGGLE Fault/Warning Type"
width: parent.width
anchors.centerIn: parent
wrapMode: Text.WordWrap
horizontalAlignment: Text.AlignHCenter
}
MouseArea {
anchors.fill: parent
onClicked: {
FaultWarningModel.setNextFaultWarning();
currentFaultWarningType = FaultWarningModel.getItemErrorTypeAsInt();
}
}
}
// Shows the type of the printer
Rectangle {
id: statusFaultWarning
width: 100
height: 50
Text {
text: {
// Causes - "Error: Unknown method return type: FaultWarningItem::ItemType"
//currentFaultWarningType = FaultWarningModel.getItemErrorType()
// Causes - "Error: Unknown method return type: FaultWarningItem::ItemType"
/*if (ItemType.TypeUnspecified === FaultWarningModel.getItemErrorType())
{
"Yay, it works"
}*/
currentFaultWarningType = FaultWarningModel.getItemErrorTypeAsInt()
if (ItemType.TypeUnspecified === currentFaultWarningType)
{
"TypeUnspecified (-1)"
}
else if (ItemType.TypeNoError === currentFaultWarningType)
{
"TypeNoError (0)"
}
else if (ItemType.TypeWarning === currentFaultWarningType)
{
"TypeWarning (1)"
}
else if (ItemType.TypeFault === currentFaultWarningType)
{
"TypeFault (2)"
}
else
{
"?"
}
}
anchors.centerIn: parent
}
}
}
}
Is there any future plans in Qt to have this changed so that it can be used without getting “Error: Unknown method return type”?
↧