#include "colormaker.h"
#include <QDateTime>
#include <QTimerEvent>
ColorMaker::ColorMaker(QObject *parent)
: QObject(parent)
,m_algorithm(RandomRGB) //初始化一个随机的颜色算法
,m_currentColor(Qt::black) //初始化赋值为黑色
,m_nColorTimer(0)
{
qsrand(QDateTime::currentDateTime().toTime_t());
}
QColor ColorMaker::color() const
{
return m_currentColor;
}
//单纯的调用该函数, 确定设置颜色顺便会发射颜色改变的信号
void ColorMaker::setColor(const QColor &color)
{
m_currentColor = color;
emit colorChanged(m_currentColor); //顺便发射信号
}
//颜色由当前时间作为参数来生成
QColor ColorMaker::timeColor() const
{
QTime time = QTime::currentTime(); //获取当前现在的时间
int r = time.hour();
int g = time.minute()*2;
int b = time.second()*4;
return QColor::fromRgb(r, g, b);
}
//传送出去一个(算法)的值 [确定是使用哪一种算法,RGB? Green? Red? Blue? ]
ColorMaker::GenerateAlgorithm ColorMaker::algorithm() const
{
return m_algorithm;
}
//赋值给类成员
void ColorMaker::setAlgorithm(ColorMaker::GenerateAlgorithm algorithm)
{
m_algorithm = algorithm;
}
//启动时间
void ColorMaker::satrt()
{
if(m_nColorTimer == 0)
{
m_nColorTimer = startTimer(1000); //返回一个时间ID. ID==0 就不启动
}
}
//停止计时器的使用
void ColorMaker::stop()
{
if(m_nColorTimer > 0)
{
killTimer(m_nColorTimer); //终止计时器,销毁
m_nColorTimer = 0;
}
}
//timerEvent事件消息
void ColorMaker::timerEvent(QTimerEvent *event)
{
if(event->timerId() == m_nColorTimer)
{
switch (m_algorithm) //怎么是在这里选择使用 哪一种颜色的算法
{
case RandomRGB:
m_currentColor.setRgb(qrand() % 255, qrand() % 255, qrand() % 255); //这个setRgb()是怎么出现在一个类的私有成员变量里面的????? what???
break;
case RandomRed:
m_currentColor.setRed(qrand() % 255);
break;
case RandomGreen:
m_currentColor.setGreen(qrand() % 255);
break;
case RandomBlue:
m_currentColor.setBlue(qrand() % 255);
break;
case LinearIncrease:
{
int r = m_currentColor.red() + 10;
int g = m_currentColor.green() + 10;
int b = m_currentColor.blue() + 10;
m_currentColor.setRgb(r % 255, g % 255, b % 255);
}
break;
}
emit colorChanged(m_currentColor); //发射信号
emit currentTimer(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
}
else
{
QObject::timerEvent(event);
}
}
QML CPlusePluse 1.zip_arrange4yg_qml_qml 界面_qquick C++_qquick官网
版权申诉
199 浏览量
2022-09-20
11:56:15
上传
评论
收藏 6KB ZIP 举报
JonSco
- 粉丝: 66
- 资源: 1万+
评论0