2017-05-31 15 views
0

我沒有找到,就Qt的文檔,有一段介紹有關qApp->setProperty()選項(這大概是有的,但我不能找到它)。有人可以向我解釋基本上它是如何工作的,我應該在什麼時候使用它?什麼時候應該使用qApp->的setProperty Qt的

我在問,因爲我需要將我的數據庫文件的路徑定義爲「全局常量」,以用於許多類,所以我在考慮將它設置爲使用setProperty函數。

例如:

qApp->setProperty("DATABASE_PATH", "C:/../Database.db"); 

然後訪問它使用:

qApp->property("DATABASE_PATH").toString(); 

我能做到這一點還是有這樣做的更好的/正確的方法?

回答

2

此功能不顯示在QApplication documentation的主視圖中。

在此頁面上有一段名爲:List of all members, including inherited members,在其中你會發現你的所有,是獨佔或您的父類的屬性。

尋找這個功能是建立在QObject

BOOL的QObject ::的setProperty(爲const char *名,常量的QVariant &值)

對象的名稱屬性的值設置爲值。

如果屬性使用Q_PROPERTY類的定義,那麼真正是 成功和假否則返回。如果該屬性不是使用Q_PROPERTY定義的 ,因此未在元對象 中列出,則將其添加爲動態屬性,並返回false。通過 元對象()和dynamicPropertyNames提供

關於所有可用的屬性信息()。

可以使用property()再次查詢動態屬性,並且可以通過將屬性值設置爲無效的QVariant來刪除 。更改 動態屬性的值會導致將QDynamicPropertyChangeEvent 發送到該對象。

注意:以「q」開頭的動態屬性保留用於內部 的目的。 (),metaObject(),dynamicPropertyNames()和 QMetaProperty :: write()。

setProperty用於通過它的名字暴露的屬性,如果你能在你的情況下,由於qApp使用它是可以通過你的整個程序訪問的指針。由QObject提供

+0

感謝您的答案,但我仍然不明白什麼時候應該使用它,如果我可以在問題中顯示的示例中使用它。 – KelvinS

+0

@KelvinS更新我的答案 – eyllanesc

1

屬性,這是QApplication-A。

您使用的屬性是所謂的動態屬性,當您有一個對象無法修改但需要向其中添加一些附加數據時,它非常有用。

請注意,這是非常糟糕的風格傳遞字符串字面量propertysetProperty:您可以輕鬆地讓錯別字忙裏偷閒,正因爲如此,性能只能與獨特的恆定名稱中使用:

// common code 
// proplist.h 

PROPLIST_PROP(databasePath) 
PROPLIST_PROP(foo) 

// interface 
// properties.h 

#define PROPLIST_PROP(name) static const char name[]; 
struct Prop { 
    #include "proplist.h" 
}; 
#undef PROPLIST_PROP 

// implementation 
// properties.cpp 

#define PROPLIST_PROP(name) const char Prop::name[] = #name; 
#include "proplist.h" 

// use 
obj.setProperty(Prop::databasePath, ...); 
auto path = obj.property(Prop::databasePath).toString(); 

你可以封裝類型屬性的名稱:

// common code 
// proplist.h 

PROPLIST_PROP(QString, databasePath) 
PROPLIST_PROP(int, foo) 

// interface 
#define DECLARE_PROPERTY(type_, name_) using name_ = Property<type_, struct name_>; 
#define DEFINE_PROPERTY(type_, name_) const char name_::name[] = #name_; 

template <typename T, typename Tag> Property { 
    static const char name[]; 
    static T get(QObject * obj) { 
    return obj->property(name).value<T>(); 
    } 
    static void set(QObject * obj, const T& val) { 
    obj->setProperty(name, QVariant::fromValue(val)); 
    } 
}; 

#define PROPLIST_PROP DECLARE_PROPERTY 
namespace Prop { 
    #include "proplist.h" 
} 
#undef PROPLIST_PROP 

// implementation 
#define PROPLIST_PROP DEFINE_PROPERTY 
namespace Prop { 
    #include "proplist.h" 
}  

// use 
Prop::DatabasePath::set(obj, ...); 
auto path = Prop::DatabasePath::get(obj); 

主要的問題,我看到的是,最有可能你濫用財產製度,而是可能會奔efit從一個全局設置對象:

// interface 
struct SettingsData { 
    SettingsData() { Q_ASSERT(!m_instance); m_instance = this; } 
    ~SettingsData() { m_instance = nullptr; } 
    static SettingsData * instance() { return m_instance; } 

    QString databasePath = "some default value"; 
    int gizmoSize = 44; 
private: 
    static SettingsData * m_instance; 
}; 

SettingsData & Settings() { return *SettingsData::instance(); } 

// implementation 
SettingsData * SettingsData::m_instance; 

// use 
int main(int argc, char ** argv) { 
    QApplication app(argc, argv); 
    SettingsData settings; 
    ... 
} 

void test() { 
    Settings().databasePath = ...; 
    auto path = Settings().databasePath; 
} 
相關問題