代码格式

头文件的格式:

  开头两行与结尾一行是防止文件重复包含的,也仅仅是防止文件被重复包含,是无法防止变量被重复定义的,

  之后是在此头文件下包含的其他头文件,

  假如使用了前向声明,那么在头文件的下方紧接着就可以 class这些类了,

  如果不打算在qml中调用C++的枚举的话,枚举可以接着前向声明写,

  这里是正式对类的定义了,一般的类如果你想使用qt的信号槽功能的话那么需要让类直接继承或间接继承Q_OBJECT类, 

  不算头文件,在类的声明中将Q_ENUMS Q_FLAGS的使用放在最前面(Q_ENUMS在这里声明),Q_PROPERTY的定义紧跟其后,

  再然后是构造函数与析构函数的修饰符及其定义,修饰符之前不加任何空白符,

  接着是成员函数及成员变量的定义,public 成员函数,信号,槽函数,protect的成员函数,槽函数,成员变量,private的成员函数,槽函数,成员变量

  示例:

  1 #ifndef STA_SETUPMAIN_H
  2 #define STA_SETUPMAIN_H
  3 
  4 /*---------------------------------------------------------------------------*/
  5 // Include headers
  6 /*---------------------------------------------------------------------------*/
  7 
  8 #include "UIMainScreenBase.h"
  9 #include "UIDivViewBase.h"
 10 #include "onsapi/NGONSIdDefine.h"
 11 #include "onsapi/NGONSAPI.h"
 12 
 13 // Forward declaration
 14 class CC_ListAll_T2M1_LL;
 15 class CC_ListAll_T1_L;
 16 class NQTimer;
 17 class FCProxyBase;
 18 class WifiSettingPropertyModel;
 19 //class MediaMrlMrcBasicModel;
 20 
 21 
 22 class STA_SetupMain : public UIMainScreenBase
 23 {
 24 Q_OBJECT
 25 // Register to meta object system&& enum declaration must in class
 26 Q_ENUMS(STA_SETUPMAIN_CHOOSELIST)
 27 public:
 28 
 29 // for set btnid
 30 enum STA_SETUPMAIN_CHOOSELIST
 31 {
 32 STA_SETUPMAIN_WIFIPOWER = 0x01,
 33 STA_SETUPMAIN_ACCESSTONETWORK,
 34 STA_SETUPMAIN_AVAILABLENETWORK,
 35 STA_SETUPMAIN_DETAILEDSETTINGS,
 36 STA_SETUPMAIN_OTHER
 37 };
 38 
 39 /// Constructor
 40 STA_SetupMain(QQuickItem* parent = 0);
 41 
 42 /// Destructor
 43 ~STA_SetupMain();
 44 
 45 /// onCreate
 46 void onCreate();
 47 /// onPreActive
 48 void onPreActive();
 49 /// onActive
 50 void onActive();
 51 /// onPreDeactive
 52 void onPreDeactive();
 53 /// onDeactive
 54 void onDeactive();
 55 /// onDestroy
 56 void onDestroy();
 57 
 58 // Q_INVOKABLE make qml can call func of C++
 59 Q_INVOKABLE void onMainListItemPressed(int index);
 60 Q_INVOKABLE void onBackBtnPressed();
 61 
 62 signals:
 63 
 64 // view emit request to fc
 65 void request(QString, QVariant);
 66 
 67 public slots:
 68 /// slots should to be public
 69 
 70 // onTimeOut
 71 void onTimeOut();
 72 
 73 // emit request and recieve notify
 74 void notify(QString uri, QVariant value);
 75 
 76 // wlan status change notify: wlan ctrol sta&&p2p device
 77 void onWlanStatusChangedNotify();
 78 
 79 // sta status change notify sta = wifi && wlan =sta+p2p
 80 void onStaPowerStatusChangedNotify();
 81 
 82 // miracast status change notify :miracast is p2p
 83 // void onMirracastPowerStatusChangedNotify();
 84 
 85 
 86 private:
 87 /*---------------------------------------------------------------------------*/
 88 // Private functions
 89 /*---------------------------------------------------------------------------*/
 90 // init screen
 91 void screenInit();
 92 
 93 // one ons should close after 3s
 94 void onTimerStart(int msc);
 95 void onTimerStop();
 96 
 97 /* copy constructor */
 98 STA_SetupMain(const STA_SetupMain&);
 99 /* assign constructor */
100 STA_SetupMain& operator = (const STA_SetupMain&);
101 
102 /*---------------------------------------------------------------------------*/
103 // Constant declaration
104 /*--------------------------------------------------------------------------*/
105 
106 ///static const vaiable
107 
108 /*---------------------------------------------------------------------------*/
109 // Variable declaration
110 /*---------------------------------------------------------------------------*/
111 /// UICC Pointer
112 CC_ListAll_T2M1_LL *m_wifiPower;
113 CC_ListAll_T2M1_LL *m_accessToNetwork;
114 CC_ListAll_T1_L *m_connectInfo;
115 CC_ListAll_T1_L *m_availableNetworks;
116 CC_ListAll_T1_L *m_detailsettings;
117 CC_ListAll_T2M1_LL *m_miracast;
118 
119 NQTimer *m_Timer; // one ons should closed after 3s
120 
121 /// FC Pointer
122 FCProxyBase *m_FCProxyWifiSetting;
123 WifiSettingPropertyModel *m_FCModelWifiSettingProperty;
124 // FCProxyBase *m_FCProxyMrlMrc;
125 // MediaMrlMrcBasicModel *m_FCModelMrlMrc;
126 
127 };
128 
129 #endif // STA_SETUPMAIN_H

源文件格式:

  一般的先包含头文件,之后是一些const型变量的定义,接着是构造函数以及析构函数的实现

 1 /*---------------------------------------------------------------------------*/
 2 // Include headers
 3 /*---------------------------------------------------------------------------*/
 4 
 5 #include "STA_SetupMain.h"
 6 #include "ngbase/NGLog.h"
 7 #include "UICC/CC_ListAll_T2M1_LL.h"
 8 
 9 /*---------------------------------------------------------------------------*/
10 //  Constant definition
11 /*---------------------------------------------------------------------------*/
12 
13 const static char *LogTag = "Setup";
14 
15 STA_SetupMain::STA_SetupMain(QQuickItem *parent)
16     :UIMainScreenBase(parent)
17     , m_wifiPower(NULL)
18     , m_accessToNetwork(NULL)
19 {
20     /// in here is copy constructor
21 }
22 
23 STA_SetupMain::~STA_SetupMain()
24 {
25 }
原文地址:https://www.cnblogs.com/qianqiannian/p/6415280.html