colormaker.h文件
#ifndef COLORMAKER_H
#define COLORMAKER_H
#include <QObject>
#include <QColor>
class ColorMaker: public QObject
{
Q_OBJECT
Q_ENUMS(GenerateAlgorithm)
Q_PROPERTY(QColor color READ getColor WRITE setColor NOTIFY colorChanged)
Q_PROPERTY(QColor timeColor READ timeColor)
public:
ColorMaker(QObject *parent = 0);
~ColorMaker();
enum GenerateAlgorithm{
RandomRGB,
RandomRed,
RandomGreen,
RandomBlue,
LinearIncrease
};
QColor getColor() const;
void setColor(const QColor & color);
QColor timeColor() const;
Q_INVOKABLE GenerateAlgorithm algorithm() const;
Q_INVOKABLE void setAlgorithm (GenerateAlgorithm algorithm);
signals:
void colorChanged(const QColor & color);
void currentTime(const QString &strTime);
public slots:
void start();
void stop();
protected:
void timerEvent (QTimerEvent *e);
private:
GenerateAlgorithm m_algorithm;
QColor m_currentColor;
int m_nColorTimer;
};
#endif
colormaker.cpp文件
#include "colormaker.h"
#include <QTimerEvent>
#include <QDateTime>
ColorMaker::ColorMaker(QObject *parent)
:QObject(parent)
,m_algorithm(RandomRGB)
,m_currentColor(Qt::black)
,m_nColorTimer(0)
{
qsrand(QDateTime::currentDateTime() .toTime_t());
}
ColorMaker::~ColorMaker()
{
}
QColor ColorMaker::getColor() 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);
}
ColorMaker::GenerateAlgorithm ColorMaker::algorithm() const
{
return m_algorithm;
}
void ColorMaker::setAlgorithm(GenerateAlgorithm algorithm)
{
m_algorithm = algorithm;
}
void ColorMaker::start()
{
if(m_nColorTimer == 0);
{
m_nColorTimer = startTimer(1000);
}
}
void ColorMaker::stop()
{
if(m_nColorTimer > 0)
{
killTimer(m_nColorTimer);
m_nColorTimer = 0;
}
}
void ColorMaker::timerEvent(QTimerEvent *e)
{
if(e->timerId() == m_nColorTimer)
{
switch(m_algorithm)
{
case RandomRGB:
m_currentColor.setRgb(qrand() % 255, qrand() % 255,qrand() % 255);
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 currentTime(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
}
else
{
QObject::timerEvent(e);
}
}
main.cpp文件
#include <QGuiApplication>
#include <QtQuick>
#include <QtQml>
#include <colormaker.h>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType<ColorMaker>("an.qt.ColorMaker",1,0,"ColorMaker");
QQuickView viewer;
viewer.setResizeMode(QQuickView::SizeRootObjectToView);
viewer.setSource(QUrl("qrc://main.qml"));
viewer.show();
return app.exec();
}
main.qml文件
import QtQuick 2.9
import QtQuick.Controls 1.0
import an.qt.ColorMaker 1.0
Rectangle{
width: 360;
height: 360;
Text {
id: timeLabel;
anchors.left: parent.left;
anchors.leftMargin: 4;
anchors.top: parent.top;
anchors.topMargin: 4;
font.pixelSize: 26;
}
ColorMaker{
id:colormaker;
color: Qt.green;
}
Rectangle{
id:colorRect;
anchors.centerIn: parent;
width: 200;
height: 200;
color: "blue";
}
Button{
id:start;
text: "start";
anchors.left: parent.left;
anchors.leftMargin: 4;
anchors.bottom: parent.bottom;
anchors.bottomMargin: 4;
onClicked: {
colormaker.start();
}
}
Button{
id:stop;
text: "stop";
anchors.left: start.right;
anchors.leftMargin: 4;
anchors.bottom: start.bottom;
onClicked: {
colormaker.stop();
}
}
function changeAlgorithm(button,algorithm){
switch(algorithm){
case 0;
button.text = "RandomRGB";
break;
case 1;
button.text = "RandomRed";
break;
case 2
button.text = "RandomGreen";
break;
case 3;
button.text = "RandomBlue";
break;
case 4;
button.text = "LinearIncrease";
break;
}
}
Button{
id:colorAlgorithm;
text:"RandomRGB";
anchors.left: stop.right;
anchors.leftMargin: 4;
anchors.bottom: start.bottom;
onClicked: {
var algorithm = (colormaker.algorithm() + 1) % 5;
changeAlgorithm(ColorAnimation,algorithm);
colormaker.setAlgorithm(algorithm);
}
}
Button{
id:quit;
text:"quit";
anchors.left: colorAlgorithm.right;
anchors.leftMargin: 4;
anchors.bottom: start.bottom;
onClicked:{Qt.quit();}
}
Component.objectName: {
colormaker.color = Qt.rgba(0,180,120,255);
colormaker.setAlgorithm(ColorMaker.LinearIncrease);
changeAlgorithm(ColorAlgorithm,colormaker.algorithm());
}
Connections{
target:colormaker;
onCurrentTime:{
timeLabel.text = strTime;
timeLabel.color = colormaker.timeColor;
}
}
Connections{
target:colormaker;
onColorChanged: {
colorRect.color = color;
}
}
}