QT MVC模式

MVC模式使用

 

1.model更新如何通知view?

答:当然不需要使用观察者模式了,直接使用QT的信号槽

2.control层/view层/model层关系,他们的生命周期如何控制?

答:view要绑定control,那么直接继承接口类DECLARE_MVC_INTERFACE(XXX)就行了,下面看接口类是怎么实现的。 因为QT  object对象不能进行多继承,这里做了一些巧妙处理。

直接展示代码!!!


#define DECLARE_MVC_INTERFACE(class_name) \
class class_name##Interface \
{\
public:\
    class_name##Interface()\
    {\
        m_p##class_name = NULL;\
    }\
    void Set##class_name( class_name* p##class_name)\
    {\
        m_p##class_name = p##class_name;\
    }\
    class_name* Get##class_name()\
    {\
        Q_ASSERT(m_p##class_name);\
        return m_p##class_name;\
    }\
    void New##class_name()\
    {\
        if(!m_p##class_name)\
            m_p##class_name = new class_name();\
    }\
    void Release##class_name()\
    {\
        if (m_p##class_name)\
            delete m_p##class_name;\
        m_p##class_name = NULL;\
    }\
private:\
    class_name* m_p##class_name;\
};
#ifndef ALARMMOD_H
#define ALARMMOD_H

#include <QObject>

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

    bool GetUndealtAlarmList(QList<SREALREPORT> &newList);
    bool HasUndealtAlarm();

    bool GetUndealtAlarmByID(SREALREPORT& data,QString id);

    void GetAlarmLog(QList<SLOGDATA>& listData);

    bool AlarmReportFromJson(QByteArray &bytes);

    bool AlarmLogFromJson(QByteArray &bytes);

    void Clear();

    void UpdateCommonList(QList<SREALREPORT> &newList, QList<SREALREPORT> &oldList);

signals:
    void sigUpdateUndealtAlarmList(int type,QList<QString> ids);
    void sigUpdateAllAlarmLogList(int type,QList<QString> ids);

public slots:

private:
    QMutex m_mutex;

    QList<SREALREPORT> m_listUndealtAlarm;

    QList<SLOGDATA> m_listAlarmLogData;

};

DECLARE_MVC_INTERFACE(AlarmMod)
#endif // ALARMMOD_H

 

#include "alarmmod.h"

AlarmMod::AlarmMod(QObject *parent) : QObject(parent)
{

}

bool AlarmMod::GetUndealtAlarmList(QList<SREALREPORT> &newList)
{
     m_mutex.lock();
     newList = m_listUndealtAlarm;
     m_mutex.unlock();
     return true;
}

bool AlarmMod::HasUndealtAlarm()
{
    bool bHas = false;
    m_mutex.lock();
    bHas = !m_listUndealtAlarm.isEmpty();
    m_mutex.unlock();
    return bHas;
}

bool AlarmMod::GetUndealtAlarmByID(SREALREPORT &data, QString id)
{
    bool bRet = false;
    m_mutex.lock();
    data.id = id;
    int index = m_listUndealtAlarm.indexOf(data);
    if (index >=0)
    {
        bRet = true;
        data = m_listUndealtAlarm.at(index);
    }
    m_mutex.unlock();
    return bRet;
}

void AlarmMod::GetAlarmLog(QList<SLOGDATA> &listData)
{
    m_mutex.lock();
    listData = m_listAlarmLogData;
    m_mutex.unlock();
}


void AlarmMod::Clear()
{
    m_mutex.lock();
    m_listUndealtAlarm.clear();
    m_mutex.unlock();
}


void AlarmMod::UpdateCommonList(QList<SREALREPORT>& newList,QList<SREALREPORT>& oldList)
{
    QList<QString> addlist,dellist,updatelist;
    m_mutex.lock();
    bool bNew = oldList.isEmpty();
    if (!bNew)//这里耗时?
    {
        DataChangedCheck<SREALREPORT,QString>(newList,oldList,addlist,dellist,updatelist);
    }
    oldList = newList;
    m_mutex.unlock();

    if (!addlist.isEmpty())
        emit sigUpdateUndealtAlarmList(E_ADD,addlist);
    if (!dellist.isEmpty())
        emit sigUpdateUndealtAlarmList(E_DEL,dellist);
    if (!updatelist.isEmpty())
        emit sigUpdateUndealtAlarmList(E_UPDATE,updatelist);
    if (bNew)
        emit sigUpdateUndealtAlarmList(E_NEW,QList<QString>());
}

#ifndef CENTRALCTRLMANAGER_H
#define CENTRALCTRLMANAGER_H

#include "model/alarmmod.h"
class CentralCtrlManager : public AlarmModInterface
{
    Q_OBJECT
public:
    explicit CentralCtrlManager(QObject *parent = 0);
    ~CentralCtrlManager();

    void Init();//初始化配置
    void ConnectInterfaces();
};

DECLARE_MVC_INTERFACE(CentralCtrlManager)
#endif // CENTRALCTRLMANAGER_H
#ifndef MAINCENTRALFORM_H
#define MAINCENTRALFORM_H

#include <QWidget>
#include <QButtonGroup>
#include "control/centralctrlmanager.h"

namespace Ui {
class MainCentralForm;
}

class MainCentralForm : public QMainBaseWidget
        ,public CentralCtrlManagerInterface
{
    Q_OBJECT

public:
    explicit MainCentralForm(QWidget *parent = 0);
    ~MainCentralForm();

    void InitUI();
    void ConnectInterfaces();


private:

    void RemoveChildWidget();
    Ui::MainCentralForm *ui;

    enum
    {

        EChildForm_num
    };

    QWidget* m_pChildForm[EChildForm_num];



};

#endif // MAINCENTRALFORM_H

 

有时间再一一补充

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值