Q_GLOBAL_STATIC 生成全局静态变量

typedef enum {
    ShowType_Login = 0,
    ShowType_ResourceBar,
    ShowType_Setting,
    ShowType_Class,
    ShowType_MoreClass,

}NavigationShowType;

class NavigtionManager : public QObject
{
    Q_OBJECT
public:
    explicit NavigtionManager(QObject *parent = 0);

public:
    static NavigtionManager *instance();

signals:
    void exitApp();
public slots:

    void showWithType(NavigationShowType showType);

    void moreClassDialogReturnAction();
    void loginAction();
    void beginingClassAction(ClassDataItem *pItem);
    void logoutAction();

public:
    void showMoreClassDialog(const QString &subjectID, const QString &charpterID);
    void showClassDialog();
    void showResourceBar();
    void showSettingDialog();
    void showLoginDialog();
    void showMainDialog();
    void showUserInfoWidget();
    void showWebToolWidget(QUrl url);
    void showClassIdDialog();
    void showCloseConfirmDlg();
private:
    bool checkClassId();
private:
    ResourceDialog *_pResDlg;
    LoginDialog *_pLoginDlg;
    ClassMenuDialog *_pClassDlg;
    MoreClassDialog *_pMoreDlg;
    SettingDialog *_pSettingDlg;
    Dialog *_pMainDlg;
    CUserInforWidget *_pUserInfoWidget;
    LoadingDlg * _pLoadingDlg;
    NavigationShowType _showType;

    DragWidget *_pDrag;
    CTeamLearningIdWidget * _pClassIdDlg;

    CCloseWidget * _pCloseConfirmDlg;
};



Q_GLOBAL_STATIC可以帮助我们建立一个全局变量

navigtionmanager.h文件中:

public:
    static NavigtionManager *instance();



navigtionmanager.cpp文件中:

Q_GLOBAL_STATIC(NavigtionManager, navigtionInstance)
NavigtionManager *NavigtionManager::instance()
{
    return navigtionInstance;
}

根据不同的模式显示界面的不同状态

void NavigtionManager::showWithType(NavigationShowType showType)
{
    if (showType == ShowType_ResourceBar) {
        showResourceBar();
    } else if (showType == ShowType_Class) {
        showClassDialog();
    } else if (showType == ShowType_Setting) {
        showSettingDialog();
    }
}



外部使用:

NavigtionManager::instance()->showWebToolWidget(QUrl(pListWebTool->at(tag - LOCALTOOLSMAXCOUNT)->url));


可以将我们项目中所用到的一些变量(窗口类)通过类的方式进行存放,每次通过访问全局变量来获取或者修改数据。但是这种做法是有问题,因为我们可以通过公有的构造函数生成新的对象,就会出问题了。

详情可查看设计模式:单例模式



你可能感兴趣的:(qt,qt,c,全局变量,Q_GLOBAL_STATIC)