qt 中枚举enum的使用方法

今天突然想着 感觉好久没用过枚举这个enum这个函数了 就在qt程序中写了个enum变量定义 发现用不了,百度 记录下。才知道qt中使用enum和c和c++中还有点不一样

.h中定义

  enum triggerMode
  {
      triggerAuto = 1 ,
      triggerSignalOne ,
      triggerContinue

  } ;

.cpp 中

#include <QMetaEnum>  // 添加一个定义
// 在函数外面添加如下
QMetaObject enumTriggerMode = MainWindow::staticMetaObject;
int index = enumTriggerMode.indexOfEnumerator("triggerMode");
QMetaEnum metaEnum = enumTriggerMode.enumerator(index);
// 好了  这样就可以调用了  
  qDebug("%s",metaEnum.key(ui->comboBox_autoTrigger->currentIndex()) );
 qDebug("%d", metaEnum.value(ui->comboBox_autoTrigger->currentIndex()));
// 感觉还是有点麻烦

下面在读一段qcustomplot的源代码 他们怎么使用enum的吧

/*!
  Defines the different units in which the image resolution can be specified in the export
  functions.

  \see QCustomPlot::savePng, QCustomPlot::saveJpg, QCustomPlot::saveBmp, QCustomPlot::saveRastered
*/
enum ResolutionUnit { ruDotsPerMeter       ///< Resolution is given in dots per meter (dpm)
                      ,ruDotsPerCentimeter ///< Resolution is given in dots per centimeter (dpcm)
                      ,ruDotsPerInch       ///< Resolution is given in dots per inch (DPI/PPI)
                    };

extern const QMetaObject staticMetaObject; // in moc-run we create a static meta object for QCP "fake" object. This line is the link to it via QCP::staticMetaObject in normal operation as namespace

} // end of namespace QCP

Q_DECLARE_METATYPE(QCP::ResolutionUnit)

有空还是要多看看源代码 才能更好的提高自己

  • 8
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七 六 伍

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值