Files
Obsidian-Main/20.01. Programming/QT/QVariant.md

10 lines
609 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
Qt有許多UI元件都可以另外設定相依的「資料」例如`QCombobox`,這樣我們就不必老是依賴選項的文字來做許多麻煩的處理。
例如,`QCombobox.addItem()`可以夾帶一個`QVariant`,這個`QVariant`就可以夾帶一個pointer。
要讓`QVariant`支援`std::shared_ptr`的方式很簡單只要在你程式的最上方加入你要使用的type就可以例如我要支援我自己的class AwSentinelDevice就這樣寫
```cpp
#include <memory>
Q_DECLARE_SMART_POINTER_METATYPE(std::shared_ptr);
Q_DECLARE_METATYPE(std::shared_ptr<AwSentinelDevice>);
```