setProperty()、property()函数
bool setProperty(const char *name, const QVariant &value);
QVariant property(const char *name) const;
setProperty给控件绑定值,property获取控件绑定的值,可以自定义所要设定的值得类型。
实际使用
目的
给每一个自定义widget设定结构体类型的值,方便后面取出widget的信息。
实际使用
- 定义一个存储播放框播放信息的struct结构体,需要宏定义结构体指针类型,后面获取property的时候编译器才能识别。
struct materialPlayingInfo
{
QString materialName;
int DPI;
int DPIShow;
int x;
int y;
ST_Test(){}
ST_Test(QString name, int dpi=1080,int DPIShows=1080,int xShow=0,int yShow=0)
{
materialName = name;
DPI = dpi;
DPIShow = DPIShows;
x = xShow;
y = yShow;
}
};
Q_DECLARE_METATYPE(materialPlayingInfo*)
- 每次创建widget将materialPlayingInfo 绑定到窗口,
materialPlayingInfo *playingInfo = new materialPlayingInfo{file_name,1080,1080,endPos.x(),endPos.y()};
widgetList.push_front(new MyVideoWidget);
widgetList.at(0)->setProperty("materialPlayingInfo",QVariant::fromValue(playingInfo));
- 获取widget的绑定值
this->property("materialPlayingInfo").value<materialPlayingInfo*>()->x = x.x();
以上就可以实现使用setProperty绑定数据的功能了。