상세 컨텐츠

본문 제목

[C++ QT QML] QML 버튼, 시간 표시 등의 기능을 C++에 연동

QT|QML

by donggyu1998 2021. 6. 10. 17:07

본문

반응형

개발 환경 구현 : https://donggyu.tistory.com/9

QML이 혼합 된 QT Quick의 C ++를 배우고, 주로 QML이 C ++ 슬롯 함수에 응답하는 C ++ 객체 유형을 호출하는 방법입니다.

💡 실행 화면

💡 TestObj.h 코드 

//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 코드

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 코드

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 코드 

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;
        }
    }
}

코드 : https://pastebin.com/hyTsNXLG

반응형

관련글 더보기