Показаны различия между двумя версиями страницы.
Предыдущая версия справа и слева Предыдущая версия Следующая версия | Предыдущая версия | ||
develop:qt:quick3 [2021/08/31 17:00] admin |
develop:qt:quick3 [2021/09/04 10:55] (текущий) admin |
||
---|---|---|---|
Строка 734: | Строка 734: | ||
=== Экспорт объектов и виджетов из C++ в QML === | === Экспорт объектов и виджетов из C++ в QML === | ||
---- | ---- | ||
- | Пример экспорта, | + | Пример экспорта, |
< | < | ||
Строка 740: | Строка 740: | ||
**Файл класса myWidget.cpp** | **Файл класса myWidget.cpp** | ||
<code cpp-qt> | <code cpp-qt> | ||
+ | #include " | ||
+ | MyWidget:: | ||
+ | { | ||
+ | QQuickWidget *pv= new QQuickWidget(this); | ||
+ | pv-> | ||
+ | |||
+ | QVBoxLayout *pvbx= new QVBoxLayout(this); | ||
+ | pvbx-> | ||
+ | setLayout(pvbx); | ||
+ | |||
+ | QQmlContext *pcon= pv-> | ||
+ | QStringList lst; | ||
+ | for(int i= 0; i < 100; i++) | ||
+ | lst << " | ||
+ | QStringListModel *pmodel= new QStringListModel(this); | ||
+ | pmodel-> | ||
+ | |||
+ | pcon-> | ||
+ | pcon-> | ||
+ | pcon-> | ||
+ | pcon-> | ||
+ | } | ||
+ | |||
+ | void MyWidget:: | ||
+ | { | ||
+ | QMessageBox:: | ||
+ | } | ||
</ | </ | ||
+ | |||
**Форма QML** | **Форма QML** | ||
<code QML> | <code QML> | ||
+ | import QtQuick 2.8 | ||
+ | Rectangle | ||
+ | { | ||
+ | width: 200; height: 200; | ||
+ | color: myColor | ||
+ | Text | ||
+ | { | ||
+ | anchors.centerIn: | ||
+ | text: myText | ||
+ | } | ||
+ | ListView | ||
+ | { | ||
+ | anchors.fill: | ||
+ | model: myModel | ||
+ | delegate: Text { text: model.display } | ||
+ | } | ||
+ | MouseArea | ||
+ | { | ||
+ | anchors.fill: | ||
+ | onPressed: | ||
+ | { | ||
+ | myWidget.setWindowTitle(" | ||
+ | myWidget.slotDisplayDialog(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
</ | </ | ||
</ | </ | ||
- | |||
- | === Использов зарегистр объектов C++ в QML === | + | === Использование |
---- | ---- | ||
+ | Как я понял, это передача в QML только **конкретных свойств и методов**, | ||
+ | Прежде всего производим регистрацию нашего класса, | ||
< | < | ||
- | < | + | < |
+ | **Calculate.h** | ||
+ | <code cpp-qt> | ||
+ | #ifndef CALCULATION_H | ||
+ | #define CALCULATION_H | ||
+ | |||
+ | #include < | ||
+ | |||
+ | class Calculation : public QObject | ||
+ | { | ||
+ | Q_OBJECT | ||
+ | private: | ||
+ | Q_PROPERTY(qulonglong input WRITE setInputValue | ||
+ | READ inputValue | ||
+ | NOTIFY inputValueChanged) // Уведомление об изменении | ||
+ | Q_PROPERTY(qulonglong result READ resultValue | ||
+ | | ||
+ | qulonglong m_nInput; | ||
+ | qulonglong m_nResult; | ||
+ | |||
+ | public: | ||
+ | explicit Calculation(QObject *parent = nullptr); | ||
+ | |||
+ | Q_INVOKABLE qulonglong factorial(const qulonglong &n); | ||
+ | qulonglong inputValue() const; | ||
+ | void setInputValue(const qulonglong& | ||
+ | qulonglong resultValue() const; | ||
+ | |||
+ | signals: | ||
+ | void inputValueChanged(qulonglong); | ||
+ | void resultValueChanged(qulonglong); | ||
+ | }; | ||
+ | |||
+ | #endif // CALCULATION_H | ||
+ | </ | ||
+ | |||
+ | |||
+ | **Calculate.cpp** | ||
+ | <code cpp-qt> | ||
+ | #include " | ||
+ | |||
+ | Calculation:: | ||
+ | { | ||
+ | } | ||
+ | |||
+ | qulonglong Calculation:: | ||
+ | { | ||
+ | return n ? (n * factorial(n-1)) : 1; | ||
+ | } | ||
+ | |||
+ | qulonglong Calculation:: | ||
+ | { | ||
+ | return m_nInput; | ||
+ | } | ||
+ | |||
+ | qulonglong Calculation:: | ||
+ | { | ||
+ | return m_nResult; | ||
+ | } | ||
+ | |||
+ | void Calculation:: | ||
+ | { | ||
+ | m_nInput= n; | ||
+ | m_nResult= factorial(m_nInput); | ||
+ | emit inputValueChanged(m_nInput); | ||
+ | emit resultValueChanged(m_nResult); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | **main.cpp** | ||
+ | <code cpp-qt> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include " | ||
+ | |||
+ | int main(int argc, char** argv) | ||
+ | { | ||
+ | QGuiApplication app(argc, argv); | ||
+ | qmlRegisterType< | ||
+ | QQmlApplicationEngine engine; | ||
+ | engine.load(QUrl(" | ||
+ | |||
+ | return app.exec(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | **Два варианта** реализации, | ||
<code QML> | <code QML> | ||
+ | import QtQuick 2.8; import QtQuick.Controls 2.2; import QtQuick.Layouts 1.3; | ||
+ | import my.class.Calc 1.0 | ||
+ | ApplicationWindow | ||
+ | { | ||
+ | title: " | ||
+ | visible: true | ||
+ | Calculation | ||
+ | { | ||
+ | id: calc | ||
+ | } | ||
+ | ColumnLayout | ||
+ | { | ||
+ | anchors.fill: | ||
+ | RowLayout // Первый подход, | ||
+ | { | ||
+ | SpinBox | ||
+ | { | ||
+ | id: sbx | ||
+ | value: 0 | ||
+ | } | ||
+ | Text | ||
+ | { | ||
+ | text: " | ||
+ | } | ||
+ | } | ||
+ | RowLayout // Второй подход, | ||
+ | { | ||
+ | SpinBox | ||
+ | { | ||
+ | value: 0 | ||
+ | onValueChanged: | ||
+ | } | ||
+ | Text | ||
+ | { | ||
+ | text: " | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
</ | </ | ||
- | </ | ||
+ | |||
+ | **Альтернативный подход** с использованием сигналов | ||
+ | <code QML> | ||
+ | import QtQuick 2.8; import QtQuick.Controls 2.2; import QtQuick.Layouts 1.3; | ||
+ | import my.class.Calc 1.0 | ||
+ | |||
+ | ApplicationWindow | ||
+ | { | ||
+ | title: " | ||
+ | visible: true | ||
+ | Calculatoin | ||
+ | { | ||
+ | input: sbx.value | ||
+ | onResultValueChanged: | ||
+ | } | ||
+ | RowLayout | ||
+ | { | ||
+ | SpinBox | ||
+ | { | ||
+ | id: sbx | ||
+ | value: 0 | ||
+ | } | ||
+ | Text | ||
+ | { | ||
+ | id: txt | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | </ | ||
=== Реализация визуальных элементов QML на C++ === | === Реализация визуальных элементов QML на C++ === | ||
---- | ---- | ||
+ | Все базовые **элементы QML** (такие как Rectangle или Text), **реализованы на C++** (QQuickRectangle, | ||
< | < | ||
- | < | + | < |
- | < | + | **Ellipse.h** |
+ | < | ||
+ | #ifndef ELLIPSE_H | ||
+ | #define ELLIPSE_H | ||
+ | #include < | ||
+ | |||
+ | class QPainter; | ||
+ | |||
+ | class Ellipse: public QQuickPaintedItem | ||
+ | { | ||
+ | Q_OBJECT | ||
+ | |||
+ | private: | ||
+ | Q_PROPERTY(QColor color WRITE setColorValue | ||
+ | READ colorValue) | ||
+ | QColor m_color; | ||
+ | |||
+ | public: | ||
+ | Ellipse(QQuickItem *parent= nullptr); | ||
+ | // Отвечает рисование элемента, | ||
+ | void paint(QPainter *ppainter); | ||
+ | QColor colorValue() const; | ||
+ | void setColorValue(const QColor& | ||
+ | }; | ||
+ | |||
+ | #endif // ELLIPSE_H | ||
</ | </ | ||
- | </ | ||
+ | **Ellipse.hpp** | ||
+ | <code cpp-qt> | ||
+ | #include " | ||
+ | #include < | ||
- | + | Ellipse:: | |
- | === Класс QQuickImageProvider ==== | + | { |
- | ---- | + | } |
+ | void Ellipse:: | ||
+ | { | ||
+ | ppainter-> | ||
+ | ppainter-> | ||
+ | ppainter-> | ||
+ | ppainter-> | ||
+ | } | ||
- | <details> | + | QColor Ellipse:: |
- | <summary>:!: Пример: | + | { |
+ | return this-> | ||
+ | } | ||
+ | |||
+ | void Ellipse:: | ||
+ | { | ||
+ | this-> | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | |||
+ | **main.hpp** | ||
+ | <code cpp-qt> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include " | ||
+ | |||
+ | int main(int argc, char** argv) | ||
+ | { | ||
+ | QGuiApplication app(argc, argv); | ||
+ | qmlRegisterType< | ||
+ | QQmlApplicationEngine engine; | ||
+ | engine.load(QUrl(" | ||
+ | |||
+ | return app.exec(); | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | |||
+ | **main.qml** | ||
<code QML> | <code QML> | ||
+ | import QtQuick 2.8; import QtQuick.Controls 2.2 | ||
+ | import my.class.Ellipse 1.0 | ||
+ | ApplicationWindow | ||
+ | { | ||
+ | title: "Paint Element"; | ||
+ | visible: true | ||
+ | Ellipse | ||
+ | { | ||
+ | anchors.fill: | ||
+ | color: " | ||
+ | } | ||
+ | } | ||
</ | </ | ||
</ | </ | ||
+ | |||
+ | === Класс QQuickImageProvider ==== | ||
+ | ---- | ||
+ | Этот класс является неким объектом, | ||
+ | Т.к. ImageProvider возвращает (методом request) изображение в как таковом виде (тип данных image/ | ||
< | < | ||
- | < | + | < |
+ | |||
+ | **ImageProvider.h** | ||
+ | <code cpp-qt> | ||
+ | #ifndef IMAGEPROVIDER_H | ||
+ | #define IMAGEPROVIDER_H | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | class ImageProvider: | ||
+ | { | ||
+ | private: | ||
+ | QImage brightness(const QImage & | ||
+ | |||
+ | public: | ||
+ | ImageProvider(); | ||
+ | QImage requestImage(const QString&, | ||
+ | }; | ||
+ | |||
+ | #endif // IMAGEPROVIDER_H | ||
+ | </ | ||
+ | |||
+ | |||
+ | **ImageProvider.hpp** | ||
+ | <code cpp-qt> | ||
+ | #include " | ||
+ | |||
+ | ImageProvider:: | ||
+ | { | ||
+ | } | ||
+ | |||
+ | QImage ImageProvider:: | ||
+ | { | ||
+ | QImage imgTemp= imgOrig; | ||
+ | qint32 nHeigt= imgTemp.height(); | ||
+ | qint32 nWidth= imgTemp.width(); | ||
+ | |||
+ | for(qint32 y= 0; y < nHeigt; ++y) | ||
+ | { | ||
+ | QRgb *tempLine= reinterpret_cast< | ||
+ | for(qint32 x= 0; x < nWidth; ++x) | ||
+ | { | ||
+ | int r= qRed(*tempLine) + n; | ||
+ | int g= qGreen(*tempLine) + n; | ||
+ | int b= qBlue(*tempLine) + n; | ||
+ | int a= qAlpha(*tempLine); | ||
+ | |||
+ | *tempLine++ = qRgba(r > 255 ? 255 : r < 0 ? 0:r, | ||
+ | g > 255 ? 255 : g < 0 ? 0:g, | ||
+ | b > 255 ? 255 : b < 0 ? 0:b, | ||
+ | a); | ||
+ | } | ||
+ | } | ||
+ | return imgTemp; | ||
+ | } | ||
+ | |||
+ | QImage ImageProvider:: | ||
+ | { | ||
+ | QStringList lst= strId.split(";" | ||
+ | bool bOk= false; | ||
+ | int nBrightness= lst.last().toInt(& | ||
+ | QImage img= brightness(QImage(":/" | ||
+ | |||
+ | if(ps) | ||
+ | *ps= img.size(); | ||
+ | |||
+ | return img; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | **main.hpp** | ||
+ | <code cpp-qt> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include " | ||
+ | |||
+ | int main(int argc, char** argv) | ||
+ | { | ||
+ | QGuiApplication app(argc, argv); | ||
+ | QQmlApplicationEngine engine; | ||
+ | engine.addImageProvider(QLatin1String(" | ||
+ | engine.load(QUrl(" | ||
+ | |||
+ | return app.exec(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | **main.qml** | ||
<code QML> | <code QML> | ||
+ | import QtQuick 2.8; import QtQuick.Controls 2.2 | ||
+ | ApplicationWindow | ||
+ | { | ||
+ | title: " | ||
+ | visible: true | ||
+ | Column | ||
+ | { | ||
+ | id: controls | ||
+ | Image | ||
+ | { | ||
+ | id: img | ||
+ | source: " | ||
+ | } | ||
+ | Slider | ||
+ | { | ||
+ | id: sld | ||
+ | width: img.width | ||
+ | value: 0.75 | ||
+ | stepSize: 0.01 | ||
+ | property int brightnessValue: | ||
+ | } | ||
+ | Text | ||
+ | { | ||
+ | width: img.width | ||
+ | text: "< | ||
+ | } | ||
+ | } | ||
+ | } | ||
</ | </ | ||
</ | </ | ||
- |