在 QML 中创建 C++ 导入类型的实例
在 QML 中创建 C++ 导入类型的实例
文件列表:
Project1.pro
QT += quick CONFIG += c++11 CONFIG += declarative_debug CONFIG += qml_debug # The following define makes your compiler emit warnings if you use # any feature of Qt which as been marked deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if you use deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += main.cpp \ colormaker.cpp RESOURCES += qml.qrc # Additional import path used to resolve QML modules in Qt Creator's code model QML_IMPORT_PATH = # Additional import path used to resolve QML modules just for Qt Quick Designer QML_DESIGNER_IMPORT_PATH = # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target HEADERS += \ colormaker.h
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 color WRITE setColor NOTIFY colorChanged) Q_PROPERTY(QColor timeColor READ timeColor) public: explicit ColorMaker(QObject *parent = nullptr); ~ColorMaker(); enum GenerateAlgorithm { RandomRGB, RandomRed, RandomGreen, RandomBlue, LinearIncrease }; QColor color() 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 *ev); private: GenerateAlgorithm m_algorithm; QColor m_currentColor; int m_nColorTimer; }; #endif // COLORMAKER_H
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::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); } ColorMaker::GenerateAlgorithm ColorMaker::algorithm() const { return m_algorithm; } void ColorMaker::setAlgorithm(ColorMaker::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 *ev) { if ( ev->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; default: break; } emit colorChanged(m_currentColor); emit currentTime(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")); } else { QObject::timerEvent(ev); } }
main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include "colormaker.h" int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); qmlRegisterType<ColorMaker>("an.qt.ColorMaker", 1, 0, "ColorMaker"); QQmlApplicationEngine engine; engine.load(QUrl(QLatin1String("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); }
main.qml
import QtQuick 2.7 import QtQuick.Controls 2.0 import QtQuick.Layouts 1.3 import an.qt.ColorMaker 1.0 ApplicationWindow { visible: true; width: 640; height: 480; title: qsTr("This is My Test"); MainForm { anchors.fill: parent; } }
MainForm.qml
import QtQuick 2.7 import QtQuick.Controls 2.0 import QtQuick.Layouts 1.3 import an.qt.ColorMaker 1.0 Rectangle { id: mainForm; width: 640; height: 480; Text { id: timeLabel; anchors.left: parent.left; anchors.leftMargin: 4; anchors.top: parent.top; anchors.topMargin: 4; font.pixelSize: 24; } 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; default: button.text = "null"; 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(colorAlgorithm, algorithm); colorMaker.setAlgorithm(algorithm); } } Button { id: quit; text: "quit"; anchors.left: colorAlgorithm.right; anchors.leftMargin: 4; anchors.bottom: start.bottom; onClicked: { Qt.quit(); } } Component.onCompleted: { colorMaker.color = Qt.rgba(0, 180, 120, 1); 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; } } }
运行效果: