视频播放器换肤功能实现(1)背景属性添加
作者:虚坏叔叔
早餐店不会开到晚上,想吃的人早就来了!😄
视频播放器换肤功能实现(1)背景属性添加
回到我们的编辑器源码中,
运行程序,点击衣服
按钮,就可以进行皮肤的切换:
当下一次在运行程序的时候,软件会自动记忆上一次设置好的皮肤。
如何实现这个功能呢、
一、编辑Qml
文件
添加背景图片,这里的source
用到了myplay
,所以需要添加MyPlay
插件:
//背景图片
Image {
id: backGround
width: parent.width
height: parent.height
source: myplay.backGround ? "file:///" + myplay.backGround : ""
//fillMode: Image.TileHorizontally
smooth: true
}
二、添加MYPlay.h
和MyPlay.cpp
:
2.1 从零开始在QT
中创建C++
的类
你可以在QT
中 ,右键
项目名称,点击Add New
,然后选择C++ Class,最后创建MYPlay
,继承于QObject
类
2.2 MYPlay.h
添加backGround
属性
最后,你可以看到成品播放器源码中,定义了backGround
属性
Q_PROPERTY(QString backGround READ backGround WRITE setBackGround NOTIFY backGroundChanged)
//背景图片路径
QString backGround() const;
void setBackGround(QString url);
signals:
void backGroundChanged(QString url);
private:
QString m_strBackgroundPath{""};
QString m_strBackGroundChoosePath{""};
2.3 MYPlay.cpp
中添加属性函数的实现
//背景图片路径
QString MYPlay::backGround() const
{
return m_strBackgroundPath;
}
void MYPlay::setBackGround(QString url)
{
QSettings *settingIni = new QSettings("setting.ini",QSettings::IniFormat);
QString backGroundpath = settingIni->value("Path/Background").toString();
if(url == NULL)
{
if(backGroundpath == NULL)
{
delete settingIni;
return;
}
else
{
m_strBackgroundPath = backGroundpath;
QFileInfo fileInfo(backGroundpath);
m_strBackGroundChoosePath = fileInfo.path();
qDebug()<<m_strBackGroundChoosePath<<endl;
delete settingIni;
emit backGroundChanged(m_strBackgroundPath);
emit backGroundChooseChanged(m_strBackGroundChoosePath);
return;
}
}
if(m_strBackgroundPath != url)
{
settingIni->setValue("Path/Background",url);
m_strBackgroundPath = url;
QFileInfo fileInfo(url);
//qDebug()<<fileInfo.suffix();
m_strBackGroundChoosePath = fileInfo.path();
emit backGroundChanged(m_strBackgroundPath);
emit backGroundChooseChanged(m_strBackGroundChoosePath);
}
delete settingIni;
}
//选择背景路径
QString MYPlay::backGroundChoose() const
{
return m_strBackGroundChoosePath;
}
三、 将MYPlay
注册为Qml
类:
#include "MYPlay.h"
qmlRegisterType<MYPlay>("com.imooc.myplayer", 1, 0, "MYPlay");
四、总结
- 本文从0开始介绍了视频播放器换肤功能部分实现步骤。