Exposing Qt's Q_ENUMS to QML

转载:http://stackoverflow.com/questions/4461017/exposing-qts-q-enums-to-qml


Enum values can only be accessed as "ElementName.EnumValue", not "object.EnumValue". So, aVar.FirstValue won't work; you'll need to use MyClass.FirstValue instead (and to do this, you'll need to register MyClass with qmlRegisterType() and then import the registered module).

Also, enum values as not returned as strings since they are defined as integer values.




转载:http://www.tuicool.com/articles/rERbu2


class ColorMaker : public QObject
{
  Q_OBJECT
  Q_ENUMS(GenerateAlgorithm)
  Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
  Q_PROPERTY(QColor timeColor READ timeColor)

public:
  ColorMaker(QObject *parent = 0);
  ~ColorMaker();

  enum GenerateAlgorithm{
    RandomRGB,
    RandomRed,
    RandomGreen,
    RandomBlue,
    LinearIncrease
  };

  QColor color() const;
  void setColor(const QColor & color);
  QColor timeColor() const;

  Q_INVOKABLE GenerateAlgorithm algorithm() const;
  Q_INVOKABLE void setAlgorithm(GenerateAlgorithm algorithm);

signals:
  void colorChanged(const QColor & color);
  void currentTime(const QString &strTime);

public slots:
  void start();
  void stop();

protected:
  void timerEvent(QTimerEvent *e);

private:
  GenerateAlgorithm m_algorithm;
  QColor m_currentColor;
  int m_nColorTimer;
};

import QtQuick 2.0
import QtQuick.Controls 1.1
import an.qt.ColorMaker 1.0

Rectangle {
  width: 360;
  height: 360;
  Text {
    id: timeLabel;
    anchors.left: parent.left;
    anchors.leftMargin: 4;
    anchors.top: parent.top;
    anchors.topMargin: 4;
    font.pixelSize: 26;
  }
  ColorMaker {
    id: colorMaker;
    color: Qt.green;
  }

  Rectangle {
    id: colorRect;
    anchors.centerIn: parent;
    width: 200;
    height: 200;
    color: "blue";
  }

  Button {
    id: start;
    text: "start";
    anchors.left: parent.left;
    anchors.leftMargin: 4;
    anchors.bottom: parent.bottom;
    anchors.bottomMargin: 4;
    onClicked: {
      colorMaker.start();
    }
  }
  Button {
    id: stop;
    text: "stop";
    anchors.left: start.right;
    anchors.leftMargin: 4;
    anchors.bottom: start.bottom;
    onClicked: {
      colorMaker.stop();
    }
  }

  function changeAlgorithm(button, algorithm){
    switch(algorithm)
    {
    case 0:
      button.text = "RandomRGB";
      break;
    case 1:
      button.text = "RandomRed";
      break;
    case 2:
      button.text = "RandomGreen";
      break;
    case 3:
      button.text = "RandomBlue";
      break;
    case 4:
      button.text = "LinearIncrease";
      break;
    }
  }

  Button {
    id: colorAlgorithm;
    text: "RandomRGB";
    anchors.left: stop.right;
    anchors.leftMargin: 4;
    anchors.bottom: start.bottom;
    onClicked: {
      var algorithm = (colorMaker.algorithm() + 1) % 5;
      changeAlgorithm(colorAlgorithm, algorithm);
      colorMaker.setAlgorithm(algorithm);
    }
  }

  Button {
    id: quit;
    text: "quit";
    anchors.left: colorAlgorithm.right;
    anchors.leftMargin: 4;
    anchors.bottom: start.bottom;
    onClicked: {
      Qt.quit();
    }
  }

  Component.onCompleted: {
    colorMaker.color = Qt.rgba(0,180,120, 255);
    colorMaker.setAlgorithm(ColorMaker.LinearIncrease);
    changeAlgorithm(colorAlgorithm, colorMaker.algorithm());
  }

  Connections {
    target: colorMaker;
    onCurrentTime:{
      timeLabel.text = strTime;
      timeLabel.color = colorMaker.timeColor;
    }
  }

  Connections {
    target: colorMaker;
    onColorChanged:{
      colorRect.color = color;
    }
  }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值