[Interest] QML Values to enum name

Jérôme Godbout jerome at bodycad.com
Tue Sep 20 22:13:23 CEST 2016


We personnaly expose another property into those case (in your WebSockets
C++ class):

Q_PROPERTY(QVarianMap enumToStringMap READ enum_to_string CONST);

QVarianMap WebSockets::enum_to_string()
{
  return QVariantMap {
    std::make_pair<int, QString>(WebSockets::Connecting,
QString("Connecting"))
    , std::make_pair<int, QString>(WebSockets::Open, QString("Open"))
    ...
  };
}

This way you can convert to string easily and access it too with the string:

property string myValue: myWebSocketsId.enumToStringMap[myIntValue]

you can even go the other way

property int myInt: WebSockets["Open"]

or even:

function printAllValue()
{
   for(key in myWebSocketsId.enumToStringMap)
   {
      console.log(key.toString() + " : " + myWebSocketsId.
enumToStringMap[key]);
   }
}

This make it way easier to deal with enum and available value.

On Tue, Sep 20, 2016 at 3:34 PM, Jason H <jhihn at gmx.com> wrote:

> Without having to create my own mapping object, I'd like to get the name
> from values:
>
> WebSocket {
>         id: webSocket
>         onStatusChanged: console.log("webSocket status:", status) //
> prints 3 or 4
> }
>
> I'd like to be able to do:
>   onStatusChanged: console.log("secWebSocket status:",
> WebSocket.Status(status)) // prints "Open" or "Closed"
>
> or something like that...
>  WebSocket.enum(Status).value(status)), WebSocket.enumValue(Status,
> status), WebSocket.Enums.Status.value(4). I don't really care, I just
> want to have to explore the mapping myself. I say explore, because the docs
> say:
>
>
> status : Status
>
> Status of the WebSocket.
> The status can have the following values:
> WebSockets.Connecting
> WebSockets.Open
> WebSockets.Closing
> WebSockets.Closed
> WebSockets.Error
>
>
> So then I have to go through and go through a trivial mapping exercise to
> find out what the constants are.
>
>
> Is there a better way to do this?
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160920/0673c23c/attachment.html>


More information about the Interest mailing list