개발 환경 구현 : https://donggyu.tistory.com/9
//TestObj.h
#ifndef TESTOBJ_H
#define TESTOBJ_H
#include <QObject>
#include<QColor>
#include<QTime>
#include<QDateTime>
#include<QTimerEvent>
class TestObj : public QObject
{
Q_OBJECT
Q_ENUMS(GenerateAlgorithm)
Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChange)
Q_PROPERTY(QColor timeColor READ timeColor)
public:
explicit TestObj(QObject *parent = nullptr);
~TestObj();
enum GenerateAlgorithm{
RandomRGB,
RandomRed,
RandomGreen,
RandomBlue,
LinearIncrease
};
Q_INVOKABLE GenerateAlgorithm algorithm()const;
Q_INVOKABLE void setAlgorithm(GenerateAlgorithm algorithm);
QColor color()const;
void setColor(const QColor & color );
QColor timeColor()const;
protected:
void timerEvent(QTimerEvent * e);
private:
GenerateAlgorithm m_algorithm;
QColor m_currentColor;
int m_nColorTimer;
signals:
void colorChange(const QColor & color);
void currenTime(const QString & strTime);
public slots:
void start();
void stop();
};
#endif // TESTOBJ_H
코드 : https://pastebin.com/gTcxCkXs
TestObj.cpp
#include "testobj.h"
#include<QtDebug>
TestObj::TestObj(QObject *parent) : QObject(parent)
,m_algorithm(RandomRGB)
,m_currentColor(Qt::black)
,m_nColorTimer(0)
{
qsrand(QDateTime::currentDateTime().toTime_t());
}
TestObj::~TestObj()
{
}
TestObj::GenerateAlgorithm TestObj::algorithm() const
{
return m_algorithm;
}
void TestObj::setAlgorithm(TestObj::GenerateAlgorithm algorithm)
{
m_algorithm=algorithm;
}
QColor TestObj::color() const
{
return m_currentColor;
}
void TestObj::setColor(const QColor &color)
{
m_currentColor=color;
emit colorChange(m_currentColor);
}
QColor TestObj::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);
}
void TestObj::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 RandomBlue:m_currentColor.setBlue(qrand()%255);
break;
case RandomGreen:m_currentColor.setGreen(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 colorChange(m_currentColor);
emit currenTime(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
}else {
QObject::timerEvent(e);
}
}
void TestObj::start()
{
if(m_nColorTimer== 0 ){
m_nColorTimer=startTimer(1000);
}
}
void TestObj::stop()
{
if(m_nColorTimer>0){
killTimer(m_nColorTimer);
m_nColorTimer=0;
}
}
코드 : https://pastebin.com/96Ruxqx4
main.cpp
#include <QGuiApplication>
//#include <QQmlApplicationEngine>
#include<QtQuick/QQuickView>
#include<QtQml>
#include"testobj.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType<TestObj>("he.TestObj.Color",1,0,"TestObj");
QQuickView viewer;
viewer.rootContext()->setContextProperty("rootv",&viewer);
viewer.setResizeMode(QQuickView::SizeRootObjectToView);
viewer.setSource(QUrl("qrc:///main.qml"));
viewer.show();
return app.exec();
}
코드 : https://pastebin.com/PK9S7WtE
main.qml
import QtQuick 2.12
import QtQuick.Controls 2.0
// Import C ++ type
import he.TestObj.Color 1.0
Rectangle {
width: 360;
height: 360;
Text {
id: ttext;
anchors.left: parent.left;
anchors.leftMargin: 4;
anchors.top: parent.top;
anchors.topMargin: 4;
font.pixelSize: 32;
}
TestObj{
id:testObj;
color:Qt.green;
}
Rectangle{
id:colorRect;
anchors.centerIn: parent;
width: 150;
height: 150;
color: "blue";
}
Button{
id:start;
text: "Start";
anchors.left: parent.left;
anchors.leftMargin: 4;
anchors.bottom: parent.bottom;
anchors.bottomMargin: 4;
onClicked: {
testObj.start();
}
}
Button{
id:stop;
text:"Stop";
anchors.left: start.right;
anchors.leftMargin: 4;
anchors.bottom: start.bottom;
onClicked: {
testObj.stop();
}
}
function changeAlgorithm(button,algorithm){
switch(algorithm){
case 0:
button.text="RandomRGB";
break;
case 1:
button.text="RandomRed";
break;
case 2:
button.text="RandomBlue";
break;
case 3:
button.text="RandomGreen";
break;
case 4:
button.text="LinearIncrease";
break;
}
}
Button{
id:colorAlgorithm;
text: "RandomRGB";
anchors.left: stop.right;
anchors.leftMargin: 4;
anchors.bottom: start.bottom;
onClicked: {
// Use two functions defined by the Q_INVOKABLE macro
var algorithm=(testObj.algorithm()+1)%5;
changeAlgorithm(colorAlgorithm,algorithm);
testObj.setAlgorithm(algorithm);
}
}
Button{
id:quit;
text: "quit";
anchors.left: colorAlgorithm.right;
anchors.leftMargin: 4;
anchors.bottom: start.bottom;
//anchors.bottomMargin: 4;
onClicked: {
rootv.close();
}
}
Component.onCompleted: {
testObj.color=Qt.rgba(0,180,120,125);
testObj.setAlgorithm(testObj.LinearIncrease);
changeAlgorithm(colorAlgorithm,testObj.algorithm());
}
Connections{
target: testObj;
onCurrenTime:{
ttext.text=strTime;
ttext.color=testObj.timeColor;
}
}
Connections{
target: testObj;
onColorChange:{
colorRect.color=color;
}
}
}
[C++ QT QML] Signal slot 활용하여 Button 클릭 시 이벤트 처리 (0) | 2021.06.14 |
---|---|
[QT QML] QML로 버튼 클릭시 Dialog 창 구현하기 (0) | 2021.06.11 |
[PyQt5 QT QML] QtQuick 활용하여 Image 불러오기 (0) | 2021.06.09 |
[PyQt5 QT QML] QtQuick 활용하여 CheckBox 구현하기 (0) | 2021.06.09 |
[PyQt5 QT QML] QtQuick 활용하여 Fragment, Loader 구현하기 (0) | 2021.06.09 |