This commit is contained in:
zhuzichu 2023-11-02 11:34:02 +08:00
parent 636dddaa84
commit 20852ac6bf
3 changed files with 11 additions and 7 deletions

View File

@ -139,7 +139,7 @@ target_include_directories(example PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/component ${CMAKE_CURRENT_SOURCE_DIR}/src/component
) )
#FluentUI.h #FluentUI.h
if(FLUENTUI_BUILD_STATIC_LIB) if(FLUENTUI_BUILD_STATIC_LIB)
target_include_directories(example PRIVATE target_include_directories(example PRIVATE
${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR}/src

View File

@ -35,7 +35,9 @@ bool ViewModelManager::exist(const QString& key){
void ViewModelManager::refreshViewModel(FluViewModel* viewModel,QString key,QVariant value){ void ViewModelManager::refreshViewModel(FluViewModel* viewModel,QString key,QVariant value){
foreach (auto item, _viewmodel) { foreach (auto item, _viewmodel) {
if(item->getKey() == viewModel->getKey()){ if(item->getKey() == viewModel->getKey()){
item->enablePropertyChange = false;
item->setProperty(key.toStdString().c_str(),value); item->setProperty(key.toStdString().c_str(),value);
item->enablePropertyChange = true;
} }
} }
} }
@ -51,14 +53,16 @@ PropertyObserver::~PropertyObserver(){
} }
void PropertyObserver::_propertyChange(){ void PropertyObserver::_propertyChange(){
auto viewModel = (FluViewModel*)parent();
if(viewModel->enablePropertyChange){
auto value = _property.read(); auto value = _property.read();
_model->setProperty(_name.toStdString().c_str(),value); _model->setProperty(_name.toStdString().c_str(),value);
ViewModelManager::getInstance()->refreshViewModel((FluViewModel*)parent(),_name,value); ViewModelManager::getInstance()->refreshViewModel(viewModel,_name,value);
}
} }
FluViewModel::FluViewModel(QObject *parent):QObject{parent}{ FluViewModel::FluViewModel(QObject *parent):QObject{parent}{
scope(FluViewModelType::Scope::Window); scope(FluViewModelType::Scope::Window);
target(nullptr);
ViewModelManager::getInstance()->insertViewModel(this); ViewModelManager::getInstance()->insertViewModel(this);
} }
@ -77,7 +81,7 @@ void FluViewModel::componentComplete(){
} }
const QMetaObject* obj = metaObject(); const QMetaObject* obj = metaObject();
if(_scope == FluViewModelType::Scope::Window){ if(_scope == FluViewModelType::Scope::Window){
_key = property("objectName_").toString()+QString::number(reinterpret_cast<qulonglong>(_window), 16); _key = property("objectName").toString()+"-"+QString::number(reinterpret_cast<qulonglong>(_window), 16);
}else{ }else{
_key = property("objectName").toString(); _key = property("objectName").toString();
} }

View File

@ -20,7 +20,6 @@ class FluViewModel : public QObject, public QQmlParserStatus
Q_OBJECT Q_OBJECT
Q_INTERFACES(QQmlParserStatus) Q_INTERFACES(QQmlParserStatus)
Q_PROPERTY_AUTO(int,scope); Q_PROPERTY_AUTO(int,scope);
Q_PROPERTY_AUTO(QObject*,target);
QML_NAMED_ELEMENT(FluViewModel) QML_NAMED_ELEMENT(FluViewModel)
public: public:
explicit FluViewModel(QObject *parent = nullptr); explicit FluViewModel(QObject *parent = nullptr);
@ -29,6 +28,7 @@ public:
void componentComplete() override; void componentComplete() override;
Q_SIGNAL void initData(); Q_SIGNAL void initData();
QString getKey(); QString getKey();
bool enablePropertyChange = true;
private: private:
QObject* _window = nullptr; QObject* _window = nullptr;
QString _key = ""; QString _key = "";