QT给控件绑定数据:setProperty

setProperty()、property()函数

 bool setProperty(const char *name, const QVariant &value);
QVariant property(const char *name) const;

setProperty给控件绑定值,property获取控件绑定的值,可以自定义所要设定的值得类型。

实际使用

目的

给每一个自定义widget设定结构体类型的值,方便后面取出widget的信息。

实际使用

  1. 定义一个存储播放框播放信息的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*)
  1. 每次创建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));
  1. 获取widget的绑定值
this->property("materialPlayingInfo").value<materialPlayingInfo*>()->x = x.x();

以上就可以实现使用setProperty绑定数据的功能了。

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值