├── .gitignore ├── EventPenetrate ├── EventPenetrate.pro ├── main.cpp ├── main.qml └── qml.qrc ├── LICENSE ├── MultiView ├── 0.gif ├── 1.gif ├── 2.gif ├── 3.gif ├── MultiView.pro ├── image.qrc ├── main.cpp ├── main.qml ├── play.png ├── qml.qrc ├── view.cpp ├── view.h ├── viewprovider.cpp └── viewprovider.h ├── README.md ├── Translator ├── Translator.pro ├── Translator_qml │ ├── Translator_qml.pro │ ├── Translator_qml_zh_CN.ts │ ├── language │ │ └── Translator_qml_zh_CN.qm │ ├── main.cpp │ ├── main.qml │ └── qml.qrc └── Translator_widget │ ├── Translator_widget.pro │ ├── Translator_widget_zh_CN.ts │ ├── language │ └── Translator_widget_zh_CN.qm │ ├── main.cpp │ ├── mainwindow.cpp │ ├── mainwindow.h │ ├── mainwindow.ui │ └── ui_mainwindow.h └── demonstrate ├── EventPenetrate.gif ├── MultiView.gif ├── Translator_qml.gif ├── Translator_widget.gif └── demonstrate.md /.gitignore: -------------------------------------------------------------------------------- 1 | debug/ 2 | release/ 3 | *.user 4 | *.Debug 5 | *.Release 6 | Makefile 7 | *.stash 8 | *.rc 9 | *.exe 10 | *.qmlc -------------------------------------------------------------------------------- /EventPenetrate/EventPenetrate.pro: -------------------------------------------------------------------------------- 1 | QT += quick 2 | CONFIG += c++11 3 | 4 | # The following define makes your compiler emit warnings if you use 5 | # any Qt feature that has been marked deprecated (the exact warnings 6 | # depend on your compiler). Refer to the documentation for the 7 | # deprecated API to know how to port your code away from it. 8 | DEFINES += QT_DEPRECATED_WARNINGS 9 | 10 | # You can also make your code fail to compile if it uses deprecated APIs. 11 | # In order to do so, uncomment the following line. 12 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 13 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 14 | 15 | SOURCES += \ 16 | main.cpp 17 | 18 | RESOURCES += qml.qrc 19 | 20 | # Additional import path used to resolve QML modules in Qt Creator's code model 21 | QML_IMPORT_PATH = 22 | 23 | # Additional import path used to resolve QML modules just for Qt Quick Designer 24 | QML_DESIGNER_IMPORT_PATH = 25 | 26 | # Default rules for deployment. 27 | qnx: target.path = /tmp/$${TARGET}/bin 28 | else: unix:!android: target.path = /opt/$${TARGET}/bin 29 | !isEmpty(target.path): INSTALLS += target 30 | -------------------------------------------------------------------------------- /EventPenetrate/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); 7 | 8 | QGuiApplication app(argc, argv); 9 | 10 | QQmlApplicationEngine engine; 11 | const QUrl url(QStringLiteral("qrc:/main.qml")); 12 | QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, 13 | &app, [url](QObject *obj, const QUrl &objUrl) { 14 | if (!obj && url == objUrl) 15 | QCoreApplication::exit(-1); 16 | }, Qt::QueuedConnection); 17 | engine.load(url); 18 | 19 | return app.exec(); 20 | } 21 | -------------------------------------------------------------------------------- /EventPenetrate/main.qml: -------------------------------------------------------------------------------- 1 | import QtQuick 2.12 2 | import QtQuick.Window 2.12 3 | 4 | Window { 5 | visible: true 6 | width: 1000 7 | height: 800 8 | title: qsTr("Hello World") 9 | 10 | Column { 11 | anchors.fill: parent 12 | spacing: 20 13 | 14 | Rectangle { 15 | width: parent.width / 2 16 | height: parent.height / 2 - 20 17 | anchors.horizontalCenter: parent.horizontalCenter 18 | color: "#e6a8f7" 19 | 20 | MouseArea { 21 | anchors.fill: parent 22 | hoverEnabled: true 23 | onEntered: parent.color = "#c91bf9"; 24 | onExited: parent.color = "#e6a8f7"; 25 | } 26 | 27 | Row { 28 | anchors.centerIn: parent 29 | spacing: 20 30 | 31 | Rectangle { 32 | width: 80 33 | height: 80 34 | color: "#ff4d4d" 35 | 36 | MouseArea { 37 | anchors.fill: parent 38 | hoverEnabled: true 39 | onEntered: parent.color = "#cc0808"; 40 | onExited: parent.color = "#ff4d4d"; 41 | } 42 | } 43 | 44 | Rectangle { 45 | width: 80 46 | height: 80 47 | color: "#4dff4d" 48 | 49 | MouseArea { 50 | anchors.fill: parent 51 | hoverEnabled: true 52 | onEntered: parent.color = "#08cc08"; 53 | onExited: parent.color = "#4dff4d"; 54 | } 55 | } 56 | 57 | Rectangle { 58 | width: 80 59 | height: 80 60 | color: "#4d4dff" 61 | 62 | MouseArea { 63 | anchors.fill: parent 64 | hoverEnabled: true 65 | onEntered: parent.color = "#0808cc"; 66 | onExited: parent.color = "#4d4dff"; 67 | } 68 | } 69 | } 70 | } 71 | 72 | Rectangle { 73 | width: parent.width / 2 74 | height: parent.height / 2 - 20 75 | anchors.horizontalCenter: parent.horizontalCenter 76 | color: "#e6a8f7" 77 | 78 | MouseArea { 79 | anchors.fill: parent 80 | hoverEnabled: true 81 | onEntered: parent.color = "#c91bf9"; 82 | onExited: parent.color = "#e6a8f7"; 83 | onClicked: console.log("parent clicked"); 84 | 85 | Row { 86 | anchors.centerIn: parent 87 | spacing: 20 88 | 89 | Rectangle { 90 | width: 80 91 | height: 80 92 | color: "#ff4d4d" 93 | 94 | MouseArea { 95 | anchors.fill: parent 96 | hoverEnabled: true 97 | propagateComposedEvents: true 98 | onEntered: parent.color = "#cc0808"; 99 | onExited: parent.color = "#ff4d4d"; 100 | onClicked: { 101 | console.log("child 1 clicked"); 102 | mouse.accepted = false; 103 | } 104 | } 105 | } 106 | 107 | Rectangle { 108 | width: 80 109 | height: 80 110 | color: "#4dff4d" 111 | 112 | MouseArea { 113 | anchors.fill: parent 114 | hoverEnabled: true 115 | propagateComposedEvents: true 116 | onEntered: parent.color = "#08cc08"; 117 | onExited: parent.color = "#4dff4d"; 118 | onClicked: { 119 | console.log("child 2 clicked"); 120 | mouse.accepted = false; 121 | } 122 | } 123 | } 124 | 125 | Rectangle { 126 | width: 80 127 | height: 80 128 | color: "#4d4dff" 129 | border.color: "blue" 130 | 131 | MouseArea { 132 | anchors.fill: parent 133 | hoverEnabled: true 134 | propagateComposedEvents: true 135 | onEntered: parent.color = "#0808cc"; 136 | onExited: parent.color = "#4d4dff"; 137 | onClicked: { 138 | console.log("child 3 clicked"); 139 | mouse.accepted = false; 140 | } 141 | } 142 | } 143 | } 144 | } 145 | } 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /EventPenetrate/qml.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | main.qml 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 mengps 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MultiView/0.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mengps/QmlExamples/3ad6857f9991ab398e0051e456711f30f802fcde/MultiView/0.gif -------------------------------------------------------------------------------- /MultiView/1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mengps/QmlExamples/3ad6857f9991ab398e0051e456711f30f802fcde/MultiView/1.gif -------------------------------------------------------------------------------- /MultiView/2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mengps/QmlExamples/3ad6857f9991ab398e0051e456711f30f802fcde/MultiView/2.gif -------------------------------------------------------------------------------- /MultiView/3.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mengps/QmlExamples/3ad6857f9991ab398e0051e456711f30f802fcde/MultiView/3.gif -------------------------------------------------------------------------------- /MultiView/MultiView.pro: -------------------------------------------------------------------------------- 1 | QT += quick 2 | CONFIG += c++11 3 | 4 | # The following define makes your compiler emit warnings if you use 5 | # any Qt feature that has been marked deprecated (the exact warnings 6 | # depend on your compiler). Refer to the documentation for the 7 | # deprecated API to know how to port your code away from it. 8 | DEFINES += QT_DEPRECATED_WARNINGS 9 | 10 | # You can also make your code fail to compile if it uses deprecated APIs. 11 | # In order to do so, uncomment the following line. 12 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 13 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 14 | 15 | SOURCES += \ 16 | main.cpp \ 17 | view.cpp \ 18 | viewprovider.cpp 19 | 20 | RESOURCES += qml.qrc \ 21 | image.qrc 22 | 23 | # Additional import path used to resolve QML modules in Qt Creator's code model 24 | QML_IMPORT_PATH = 25 | 26 | # Additional import path used to resolve QML modules just for Qt Quick Designer 27 | QML_DESIGNER_IMPORT_PATH = 28 | 29 | # Default rules for deployment. 30 | qnx: target.path = /tmp/$${TARGET}/bin 31 | else: unix:!android: target.path = /opt/$${TARGET}/bin 32 | !isEmpty(target.path): INSTALLS += target 33 | 34 | HEADERS += \ 35 | view.h \ 36 | viewprovider.h 37 | -------------------------------------------------------------------------------- /MultiView/image.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | 0.gif 4 | 1.gif 5 | 2.gif 6 | 3.gif 7 | play.png 8 | 9 | 10 | -------------------------------------------------------------------------------- /MultiView/main.cpp: -------------------------------------------------------------------------------- 1 | #include "view.h" 2 | #include "viewprovider.h" 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | int main(int argc, char *argv[]) 9 | { 10 | QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); 11 | 12 | QGuiApplication app(argc, argv); 13 | 14 | View *view = new View; 15 | 16 | QQmlApplicationEngine engine; 17 | engine.addImageProvider(QLatin1String("MultiView"), view->multiView()); 18 | engine.addImageProvider(QLatin1String("MultiSource"), view->multiSource()); 19 | engine.rootContext()->setContextProperty("view", view); 20 | const QUrl url(QStringLiteral("qrc:/main.qml")); 21 | QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, 22 | &app, [url](QObject *obj, const QUrl &objUrl) { 23 | if (!obj && url == objUrl) 24 | QCoreApplication::exit(-1); 25 | }, Qt::QueuedConnection); 26 | engine.load(url); 27 | 28 | return app.exec(); 29 | } 30 | -------------------------------------------------------------------------------- /MultiView/main.qml: -------------------------------------------------------------------------------- 1 | import QtQuick 2.12 2 | import QtQuick.Window 2.12 3 | import QtQuick.Controls 2.12 4 | 5 | ApplicationWindow { 6 | id: root 7 | visible: true 8 | width: 860 9 | height: 660 + tabBar.height + 4 10 | title: qsTr("MultiView Test") 11 | 12 | SwipeView { 13 | id: swipeView 14 | anchors.fill: parent 15 | currentIndex: tabBar.currentIndex 16 | 17 | Page { 18 | id: page1 19 | width: 860 20 | height: 660 21 | 22 | Connections { 23 | target: view 24 | onViewNeedUpdate: { 25 | /** 26 | * 1. index为视图索引 27 | * 2. ###用于分隔 28 | * 3. Date.now()用于更新 29 | */ 30 | viewRepeater.itemAt(index).source = "image://MultiView/" + index + "###" + Date.now(); 31 | } 32 | } 33 | 34 | Grid { 35 | rows: 3 36 | columns: 3 37 | anchors.fill: parent 38 | 39 | Repeater { 40 | id: viewRepeater 41 | model: 9 42 | 43 | Image { 44 | cache: false 45 | mipmap: true 46 | width: page1.width / 3 47 | height: page1.height / 3 48 | 49 | Text { 50 | anchors.left: parent.left 51 | anchors.leftMargin: 12 52 | anchors.top: parent.top 53 | anchors.topMargin: 12 54 | font.bold: true 55 | font.pointSize: 30 56 | color: "red" 57 | text: index 58 | } 59 | 60 | Rectangle { 61 | anchors.fill: parent 62 | border.width: 2 63 | border.color: "yellow" 64 | color: "transparent" 65 | } 66 | } 67 | } 68 | } 69 | } 70 | 71 | Page { 72 | id: page2 73 | width: 860 74 | height: 660 75 | 76 | Connections { 77 | target: view 78 | onSourceNeedUpdate: { 79 | /** 80 | * 1. source为图像源索引 81 | * 2. ###用于分隔 82 | * 3. Date.now()用于更新 83 | */ 84 | sourceRepeater.itemAt(source).source = "image://MultiSource/" + source + "###" + Date.now(); 85 | } 86 | } 87 | 88 | Grid { 89 | rows: 2 90 | columns: 2 91 | anchors.fill: parent 92 | 93 | Repeater { 94 | id: sourceRepeater 95 | model: 4 96 | 97 | Image { 98 | mipmap: true 99 | width: page2.width / 2 100 | height: page2.height / 2 101 | fillMode: Image.PreserveAspectFit 102 | 103 | property bool running: true 104 | 105 | Image { 106 | width: 80 107 | height: 80 108 | anchors.centerIn: parent 109 | opacity: 0.7 110 | mipmap: true 111 | source: parent.running ? "" : "qrc:/play.png" 112 | } 113 | 114 | Text { 115 | anchors.left: parent.left 116 | anchors.leftMargin: 12 117 | anchors.top: parent.top 118 | anchors.topMargin: 12 119 | font.bold: true 120 | font.pointSize: 30 121 | color: "red" 122 | text: index 123 | } 124 | 125 | Rectangle { 126 | anchors.fill: parent 127 | border.width: 2 128 | border.color: "#89f2f5" 129 | color: "transparent" 130 | } 131 | 132 | MouseArea { 133 | anchors.fill: parent 134 | onClicked: { 135 | if (parent.running) { 136 | view.pauseSource(index); 137 | parent.running = false; 138 | } else { 139 | view.resumeSource(index); 140 | parent.running = true; 141 | } 142 | } 143 | } 144 | } 145 | } 146 | } 147 | } 148 | } 149 | 150 | footer: TabBar { 151 | id: tabBar 152 | currentIndex: swipeView.currentIndex 153 | 154 | TabButton { 155 | font.pointSize: 12 156 | text: qsTr("多视图") 157 | onClicked: view.stopSource(); 158 | } 159 | 160 | TabButton { 161 | font.pointSize: 12 162 | text: qsTr("多图像源") 163 | onClicked: { 164 | view.startSource(); 165 | 166 | for (let i = 0; i < 4; i++) 167 | sourceRepeater.itemAt(i).running = true; 168 | } 169 | } 170 | } 171 | } 172 | -------------------------------------------------------------------------------- /MultiView/play.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mengps/QmlExamples/3ad6857f9991ab398e0051e456711f30f802fcde/MultiView/play.png -------------------------------------------------------------------------------- /MultiView/qml.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | main.qml 4 | 5 | 6 | -------------------------------------------------------------------------------- /MultiView/view.cpp: -------------------------------------------------------------------------------- 1 | #include "view.h" 2 | #include "viewprovider.h" 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | class ViewPrivate 12 | { 13 | public: 14 | ViewPrivate() { 15 | m_multiView = new ViewProvider; 16 | m_multiSource = new ViewProvider; 17 | } 18 | 19 | ViewProvider *m_multiView = nullptr; 20 | ViewProvider *m_multiSource = nullptr; 21 | QTimer *m_generator = nullptr; 22 | QMovie *m_sources[4] = { nullptr }; 23 | }; 24 | 25 | View::View() 26 | { 27 | d = new ViewPrivate; 28 | 29 | //多视图图像生成器 30 | d->m_generator = new QTimer(this); 31 | connect(d->m_generator, &QTimer::timeout, this, [this]() { 32 | static int index = 0; 33 | QImage image = QGuiApplication::primaryScreen()->grabWindow(0).toImage(); 34 | d->m_multiView->updateView(index, image); 35 | emit viewNeedUpdate(index); 36 | 37 | if (++index == 9) index = 0; 38 | }); 39 | d->m_generator->start(1000 / 9); 40 | 41 | /** 42 | * @note 多图像源 43 | * @source[0-4] gif[0-4] 44 | */ 45 | for (int i = 0; i < 4; i++) { 46 | d->m_sources[i] = new QMovie(":/" + QString::number(i) + ".gif", "GIF", this); 47 | qDebug() << d->m_sources[i]->fileName() << d->m_sources[i]->isValid(); 48 | connect(d->m_sources[i], &QMovie::frameChanged, this, [i, this](int) { 49 | d->m_multiSource->updateView(i, d->m_sources[i]->currentImage()); 50 | emit sourceNeedUpdate(i); 51 | }); 52 | } 53 | } 54 | 55 | QQuickImageProvider* View::multiView() const 56 | { 57 | return d->m_multiView; 58 | } 59 | 60 | QQuickImageProvider* View::multiSource() const 61 | { 62 | return d->m_multiSource; 63 | } 64 | 65 | void View::startSource() 66 | { 67 | if (d->m_generator) d->m_generator->stop(); 68 | 69 | for (auto it : d->m_sources) { 70 | if (it) it->start(); 71 | } 72 | } 73 | 74 | void View::resumeSource(int source) 75 | { 76 | if (d->m_sources[source]) d->m_sources[source]->setPaused(false); 77 | } 78 | 79 | void View::stopSource() 80 | { 81 | if (d->m_generator) d->m_generator->start(1000 / 9); 82 | 83 | for (auto it : d->m_sources) { 84 | if (it) it->stop(); 85 | } 86 | } 87 | 88 | void View::pauseSource(int source) 89 | { 90 | if (d->m_sources[source]) d->m_sources[source]->setPaused(true); 91 | } 92 | -------------------------------------------------------------------------------- /MultiView/view.h: -------------------------------------------------------------------------------- 1 | #ifndef VIEW_H 2 | #define VIEW_H 3 | 4 | #include 5 | 6 | class ViewPrivate; 7 | class QQuickImageProvider; 8 | class View : public QObject 9 | { 10 | Q_OBJECT 11 | 12 | public: 13 | View(); 14 | 15 | QQuickImageProvider* multiView() const; 16 | QQuickImageProvider* multiSource() const; 17 | 18 | Q_INVOKABLE void startSource(); 19 | Q_INVOKABLE void resumeSource(int source); 20 | Q_INVOKABLE void stopSource(); 21 | Q_INVOKABLE void pauseSource(int source); 22 | 23 | signals: 24 | void viewNeedUpdate(int index); 25 | void sourceNeedUpdate(int source); 26 | 27 | private: 28 | ViewPrivate *d = nullptr; 29 | }; 30 | 31 | #endif // VIEW_H 32 | -------------------------------------------------------------------------------- /MultiView/viewprovider.cpp: -------------------------------------------------------------------------------- 1 | #include "viewprovider.h" 2 | 3 | ViewProvider::ViewProvider() 4 | : QQuickImageProvider(QQuickImageProvider::Image) 5 | { 6 | 7 | } 8 | 9 | void ViewProvider::updateView(int index, const QImage &view) 10 | { 11 | m_views[index] = view; 12 | } 13 | 14 | QImage ViewProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize) 15 | { 16 | //这里可以id.left(1); 17 | int index = id.left(id.indexOf("###")).toInt(); 18 | QImage view = m_views[index]; 19 | 20 | if (!view.isNull()) { 21 | view.scaled(requestedSize); 22 | 23 | if (size) *size = requestedSize; 24 | } 25 | 26 | return view; 27 | } 28 | -------------------------------------------------------------------------------- /MultiView/viewprovider.h: -------------------------------------------------------------------------------- 1 | #ifndef VIEWPROVIDER_H 2 | #define VIEWPROVIDER_H 3 | 4 | #include 5 | 6 | class ViewProvider : public QQuickImageProvider 7 | { 8 | public: 9 | ViewProvider(); 10 | void updateView(int index, const QImage &view); 11 | QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize) override; 12 | 13 | private: 14 | /** 15 | * @note 为什么使用 hash 而不是 vector 16 | * 因为这样可以扩展到更广泛的使用场景 17 | */ 18 | QHash m_views; 19 | }; 20 | 21 | #endif // VIEWPROVIDER_H 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # QmlExamples 2 | 3 | 提供一些有用Qml示例 4 | 5 | ------ 6 | 7 | ### 所有示例预览 8 | 9 | [预览图均为GIF,较大](https://github.com/mengps/QmlExamples/blob/master/demonstrate/demonstrate.md) 10 | 11 | ------ 12 | 13 | ### 示例列表 14 | 15 | - MultiView Qml中实现多视图,多图像源(QImage / QPixmap) 16 | 17 | - Translator Qt / Qml 中支持多国语言 18 | 19 | - EventPenetrate MouseArea 的 Click / Hover 事件穿透 20 | 21 | ------ 22 | 23 | ### 许可证 24 | 25 | 使用 `MIT LICENSE` 26 | 27 | ------ 28 | 29 | ### 开发环境 30 | 31 | windows 10,Qt 5.13.0 -------------------------------------------------------------------------------- /Translator/Translator.pro: -------------------------------------------------------------------------------- 1 | TEMPLATE = subdirs 2 | 3 | Translator_widget.file = ./Translator_widget/Translator_widget.pro 4 | Translator_qml.file = ./Translator_qml/Translator_qml.pro 5 | 6 | SUBDIRS += Translator_widget Translator_qml 7 | -------------------------------------------------------------------------------- /Translator/Translator_qml/Translator_qml.pro: -------------------------------------------------------------------------------- 1 | QT += quick 2 | CONFIG += c++11 3 | 4 | TARGET = Translator_qml 5 | 6 | TRANSLATIONS = $${TARGET}_zh_CN.ts 7 | 8 | # The following define makes your compiler emit warnings if you use 9 | # any Qt feature that has been marked deprecated (the exact warnings 10 | # depend on your compiler). Refer to the documentation for the 11 | # deprecated API to know how to port your code away from it. 12 | DEFINES += QT_DEPRECATED_WARNINGS 13 | 14 | # You can also make your code fail to compile if it uses deprecated APIs. 15 | # In order to do so, uncomment the following line. 16 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 17 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 18 | 19 | SOURCES += \ 20 | main.cpp 21 | 22 | RESOURCES += qml.qrc 23 | 24 | # Additional import path used to resolve QML modules in Qt Creator's code model 25 | QML_IMPORT_PATH = 26 | 27 | # Additional import path used to resolve QML modules just for Qt Quick Designer 28 | QML_DESIGNER_IMPORT_PATH = 29 | 30 | # Default rules for deployment. 31 | qnx: target.path = /tmp/$${TARGET}/bin 32 | else: unix:!android: target.path = /opt/$${TARGET}/bin 33 | !isEmpty(target.path): INSTALLS += target 34 | -------------------------------------------------------------------------------- /Translator/Translator_qml/Translator_qml_zh_CN.ts: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | TranslateController 6 | 7 | 8 | --- Language changed to Chinese 9 | --- 语言变更为中文 10 | --- 语言变更为中文 11 | 12 | 13 | 14 | --- Language changed to English 15 | --- 语言变更为英文 16 | --- 语言变更为英文 17 | 18 | 19 | 20 | main 21 | 22 | 23 | MainWindow 24 | 主窗口 25 | 主窗口 26 | 27 | 28 | 29 | Language 30 | 语言 31 | 语言 32 | 33 | 34 | 35 | English 36 | 英文 37 | 英文 38 | 39 | 40 | 41 | Chinese 42 | 中文 43 | 中文 44 | 45 | 46 | 47 | This is "Test Text" 48 | 这是"测试文本" 49 | 这是"测试文本" 50 | 51 | 52 | 53 | Change Language 54 | 更改语言 55 | 更改语言 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /Translator/Translator_qml/language/Translator_qml_zh_CN.qm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mengps/QmlExamples/3ad6857f9991ab398e0051e456711f30f802fcde/Translator/Translator_qml/language/Translator_qml_zh_CN.qm -------------------------------------------------------------------------------- /Translator/Translator_qml/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define q_invokalbe Q_INVOKABLE 7 | 8 | class TranslateController : public QObject 9 | { 10 | Q_OBJECT 11 | Q_ENUMS(Language) 12 | 13 | public: 14 | enum class Language 15 | { 16 | English = 1, 17 | Chinese 18 | }; 19 | 20 | public: 21 | static TranslateController* instance(QQmlEngine *engine) { 22 | static TranslateController controller(engine); 23 | return &controller; 24 | } 25 | 26 | void retranslateUi() { 27 | m_engine->retranslate(); 28 | } 29 | 30 | q_invokalbe void loadLanguage(Language lang) { 31 | switch (lang) { 32 | case Language::Chinese: 33 | if (m_translator->load("./language/Translator_qml_zh_CN.qm")) { 34 | emit message(tr("--- Language changed to Chinese")); 35 | } 36 | break; 37 | case Language::English: 38 | m_translator->load(""); 39 | emit message(tr("--- Language changed to English")); 40 | break; 41 | } 42 | 43 | retranslateUi(); 44 | } 45 | 46 | signals: 47 | void message(const QString &msg); 48 | 49 | private: 50 | TranslateController(QQmlEngine *engine) { 51 | m_engine = engine; 52 | m_translator = new QTranslator(this); 53 | QCoreApplication::installTranslator(m_translator); 54 | } 55 | 56 | QQmlEngine *m_engine = nullptr; 57 | QTranslator *m_translator = nullptr; 58 | }; 59 | 60 | int main(int argc, char *argv[]) 61 | { 62 | QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); 63 | 64 | QGuiApplication app(argc, argv); 65 | 66 | qmlRegisterUncreatableType("an.translate", 1, 0, "Language", "不能创建TranslateController对象"); 67 | 68 | QQmlApplicationEngine engine; 69 | auto translateController = TranslateController::instance(&engine); 70 | engine.rootContext()->setContextProperty("controller", translateController); 71 | const QUrl url(QStringLiteral("qrc:/main.qml")); 72 | QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, 73 | &app, [url](QObject *obj, const QUrl &objUrl) { 74 | if (!obj && url == objUrl) 75 | QCoreApplication::exit(-1); 76 | }, Qt::QueuedConnection); 77 | engine.load(url); 78 | 79 | return app.exec(); 80 | } 81 | 82 | #include "main.moc" 83 | -------------------------------------------------------------------------------- /Translator/Translator_qml/main.qml: -------------------------------------------------------------------------------- 1 | import QtQuick 2.13 2 | import QtQuick.Window 2.13 3 | import QtQuick.Controls 2.13 4 | import an.translate 1.0 5 | 6 | ApplicationWindow { 7 | id: root 8 | visible: true 9 | width: 640 10 | height: 480 11 | title: qsTr("MainWindow") 12 | 13 | property int language: Language.English 14 | 15 | ActionGroup { 16 | id: languageGroup 17 | } 18 | 19 | menuBar: MenuBar { 20 | Menu { 21 | id: languageMenu 22 | title: qsTr("Language") 23 | 24 | Action { 25 | checked: language == Language.English 26 | checkable: true 27 | text: qsTr("English") 28 | ActionGroup.group: languageGroup 29 | onTriggered: { 30 | controller.loadLanguage(Language.English); 31 | language = Language.English; 32 | } 33 | } 34 | 35 | Action { 36 | checked: language == Language.Chinese 37 | checkable: true 38 | text: qsTr("Chinese") 39 | ActionGroup.group: languageGroup 40 | onTriggered: { 41 | controller.loadLanguage(Language.Chinese); 42 | language = Language.Chinese; 43 | } 44 | } 45 | } 46 | } 47 | 48 | Connections { 49 | target: controller 50 | onMessage: { 51 | statusBar.text = msg; 52 | } 53 | } 54 | 55 | Text { 56 | id: statusBar 57 | visible: false 58 | color: "red" 59 | anchors { 60 | bottom: parent.bottom 61 | bottomMargin: 30 62 | left: parent.left 63 | leftMargin: 20 64 | } 65 | onTextChanged: { 66 | visible = true; 67 | textTimer.restart(); 68 | } 69 | 70 | Timer { 71 | id: textTimer 72 | running: false 73 | interval: 1000 74 | onTriggered: statusBar.visible = false; 75 | } 76 | } 77 | 78 | Label { 79 | id: label 80 | anchors.centerIn: parent 81 | text: qsTr("This is \"Test Text\"") 82 | font { pointSize: 26 } 83 | } 84 | 85 | Button { 86 | id: button 87 | text: qsTr("Change Language") 88 | anchors { 89 | bottom: label.top 90 | bottomMargin: 30 91 | horizontalCenter: parent.horizontalCenter 92 | } 93 | 94 | onClicked: { 95 | if (language == Language.English){ 96 | controller.loadLanguage(Language.Chinese); 97 | language = Language.Chinese; 98 | } else { 99 | controller.loadLanguage(Language.English); 100 | language = Language.English; 101 | } 102 | } 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /Translator/Translator_qml/qml.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | main.qml 4 | 5 | 6 | -------------------------------------------------------------------------------- /Translator/Translator_widget/Translator_widget.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2020-05-14T21:32:53 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = Translator_widget 12 | TEMPLATE = app 13 | 14 | TRANSLATIONS += $${TARGET}_zh_CN.ts 15 | 16 | # The following define makes your compiler emit warnings if you use 17 | # any feature of Qt which has been marked as deprecated (the exact warnings 18 | # depend on your compiler). Please consult the documentation of the 19 | # deprecated API in order to know how to port your code away from it. 20 | DEFINES += QT_DEPRECATED_WARNINGS 21 | 22 | # You can also make your code fail to compile if you use deprecated APIs. 23 | # In order to do so, uncomment the following line. 24 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 25 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 26 | 27 | CONFIG += c++11 28 | 29 | SOURCES += \ 30 | main.cpp \ 31 | mainwindow.cpp 32 | 33 | HEADERS += \ 34 | mainwindow.h 35 | 36 | FORMS += \ 37 | mainwindow.ui 38 | 39 | # Default rules for deployment. 40 | qnx: target.path = /tmp/$${TARGET}/bin 41 | else: unix:!android: target.path = /opt/$${TARGET}/bin 42 | !isEmpty(target.path): INSTALLS += target 43 | -------------------------------------------------------------------------------- /Translator/Translator_widget/Translator_widget_zh_CN.ts: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | MainWindow 6 | 7 | 8 | MainWindow 9 | 10 | 11 | 12 | 13 | This is "Test Text" 14 | 15 | 16 | 17 | 18 | Language 19 | 20 | 21 | 22 | 23 | English 24 | 25 | 26 | 27 | 28 | Chinese 29 | 30 | 31 | 32 | 33 | 34 | Change Language 35 | 36 | 37 | 38 | 39 | --- Language changed to Chinese 40 | 41 | 42 | 43 | 44 | --- Language changed to English 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /Translator/Translator_widget/language/Translator_widget_zh_CN.qm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mengps/QmlExamples/3ad6857f9991ab398e0051e456711f30f802fcde/Translator/Translator_widget/language/Translator_widget_zh_CN.qm -------------------------------------------------------------------------------- /Translator/Translator_widget/main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include 3 | #include 4 | #include 5 | 6 | int main(int argc, char *argv[]) 7 | { 8 | QApplication a(argc, argv); 9 | 10 | MainWindow w; 11 | w.show(); 12 | 13 | return a.exec(); 14 | } 15 | -------------------------------------------------------------------------------- /Translator/Translator_widget/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include "ui_mainwindow.h" 3 | #include 4 | #include 5 | 6 | MainWindow::MainWindow(QWidget *parent) : 7 | QMainWindow(parent), 8 | ui(new Ui::MainWindow) 9 | { 10 | ui->setupUi(this); 11 | m_translator = new QTranslator(this); 12 | 13 | QCoreApplication::instance()->installTranslator(m_translator); 14 | 15 | m_changeBtn = new QPushButton(this); 16 | m_changeBtn->setText(tr("Change Language")); 17 | m_changeBtn->setGeometry(40, 100, 120, 40); 18 | 19 | connect(m_changeBtn, &QPushButton::clicked, this, [this]() { 20 | if (m_language == Language::English) { 21 | setLanguage(Language::Chinese); 22 | } else { 23 | setLanguage(Language::English); 24 | } 25 | }); 26 | connect(ui->actionChinese, &QAction::triggered, this, [this]() { 27 | setLanguage(Language::Chinese); 28 | }); 29 | connect(ui->actionEnglish, &QAction::triggered, this, [this]() { 30 | setLanguage(Language::English); 31 | }); 32 | 33 | emit ui->actionEnglish->triggered(); 34 | } 35 | 36 | MainWindow::~MainWindow() 37 | { 38 | delete ui; 39 | } 40 | 41 | void MainWindow::setLanguage(MainWindow::Language lang) 42 | { 43 | switch (lang) { 44 | case Language::Chinese: 45 | if (m_translator->load("./language/Translator_widget_zh_CN.qm")) { 46 | ui->actionChinese->setChecked(true); 47 | ui->actionEnglish->setChecked(false); 48 | m_language = Language::Chinese; 49 | statusBar()->showMessage(tr("--- Language changed to Chinese"), 1000); 50 | } 51 | break; 52 | case Language::English: 53 | m_translator->load(""); 54 | ui->actionChinese->setChecked(false); 55 | ui->actionEnglish->setChecked(true); 56 | m_language = Language::English; 57 | statusBar()->showMessage(tr("--- Language changed to English"), 1000); 58 | break; 59 | } 60 | 61 | retranslateUi(); 62 | } 63 | 64 | void MainWindow::retranslateUi() 65 | { 66 | ui->retranslateUi(this); 67 | m_changeBtn->setText(QCoreApplication::translate("MainWindow", "Change Language", nullptr)); 68 | } 69 | -------------------------------------------------------------------------------- /Translator/Translator_widget/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | 6 | namespace Ui { 7 | class MainWindow; 8 | } 9 | 10 | class QTranslator; 11 | class QPushButton; 12 | class MainWindow : public QMainWindow 13 | { 14 | Q_OBJECT 15 | 16 | public: 17 | enum class Language 18 | { 19 | English = 1, 20 | Chinese 21 | }; 22 | 23 | explicit MainWindow(QWidget *parent = nullptr); 24 | ~MainWindow(); 25 | 26 | void setLanguage(Language lang); 27 | 28 | private: 29 | void retranslateUi(); 30 | 31 | Ui::MainWindow *ui; 32 | QTranslator *m_translator; 33 | QPushButton *m_changeBtn; 34 | Language m_language = Language::English; 35 | }; 36 | 37 | #endif // MAINWINDOW_H 38 | -------------------------------------------------------------------------------- /Translator/Translator_widget/mainwindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MainWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 611 10 | 358 11 | 12 | 13 | 14 | MainWindow 15 | 16 | 17 | 18 | 19 | 20 | 220 21 | 110 22 | 301 23 | 71 24 | 25 | 26 | 27 | 28 | 微软雅黑 29 | 26 30 | 31 | 32 | 33 | 1 34 | 35 | 36 | This is "Test Text" 37 | 38 | 39 | 40 | 41 | 42 | 43 | 0 44 | 0 45 | 611 46 | 22 47 | 48 | 49 | 50 | 51 | Language 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | true 62 | 63 | 64 | false 65 | 66 | 67 | English 68 | 69 | 70 | 71 | 72 | true 73 | 74 | 75 | Chinese 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /Translator/Translator_widget/ui_mainwindow.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************** 2 | ** Form generated from reading UI file 'mainwindow.ui' 3 | ** 4 | ** Created by: Qt User Interface Compiler version 5.13.0 5 | ** 6 | ** WARNING! All changes made in this file will be lost when recompiling UI file! 7 | ********************************************************************************/ 8 | 9 | #ifndef UI_MAINWINDOW_H 10 | #define UI_MAINWINDOW_H 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | QT_BEGIN_NAMESPACE 23 | 24 | class Ui_MainWindow 25 | { 26 | public: 27 | QAction *actionEnglish; 28 | QAction *actionChinese; 29 | QWidget *centralWidget; 30 | QLabel *label; 31 | QMenuBar *menuBar; 32 | QMenu *menuLanguage; 33 | QStatusBar *statusBar; 34 | 35 | void setupUi(QMainWindow *MainWindow) 36 | { 37 | if (MainWindow->objectName().isEmpty()) 38 | MainWindow->setObjectName(QString::fromUtf8("MainWindow")); 39 | MainWindow->resize(611, 358); 40 | actionEnglish = new QAction(MainWindow); 41 | actionEnglish->setObjectName(QString::fromUtf8("actionEnglish")); 42 | actionEnglish->setCheckable(true); 43 | actionEnglish->setChecked(false); 44 | actionChinese = new QAction(MainWindow); 45 | actionChinese->setObjectName(QString::fromUtf8("actionChinese")); 46 | actionChinese->setCheckable(true); 47 | centralWidget = new QWidget(MainWindow); 48 | centralWidget->setObjectName(QString::fromUtf8("centralWidget")); 49 | label = new QLabel(centralWidget); 50 | label->setObjectName(QString::fromUtf8("label")); 51 | label->setGeometry(QRect(220, 110, 301, 71)); 52 | QFont font; 53 | font.setFamily(QString::fromUtf8("\345\276\256\350\275\257\351\233\205\351\273\221")); 54 | font.setPointSize(26); 55 | label->setFont(font); 56 | label->setLineWidth(1); 57 | MainWindow->setCentralWidget(centralWidget); 58 | menuBar = new QMenuBar(MainWindow); 59 | menuBar->setObjectName(QString::fromUtf8("menuBar")); 60 | menuBar->setGeometry(QRect(0, 0, 611, 22)); 61 | menuLanguage = new QMenu(menuBar); 62 | menuLanguage->setObjectName(QString::fromUtf8("menuLanguage")); 63 | MainWindow->setMenuBar(menuBar); 64 | statusBar = new QStatusBar(MainWindow); 65 | statusBar->setObjectName(QString::fromUtf8("statusBar")); 66 | MainWindow->setStatusBar(statusBar); 67 | 68 | menuBar->addAction(menuLanguage->menuAction()); 69 | menuLanguage->addAction(actionEnglish); 70 | menuLanguage->addAction(actionChinese); 71 | 72 | retranslateUi(MainWindow); 73 | 74 | QMetaObject::connectSlotsByName(MainWindow); 75 | } // setupUi 76 | 77 | void retranslateUi(QMainWindow *MainWindow) 78 | { 79 | MainWindow->setWindowTitle(QCoreApplication::translate("MainWindow", "MainWindow", nullptr)); 80 | actionEnglish->setText(QCoreApplication::translate("MainWindow", "English", nullptr)); 81 | actionChinese->setText(QCoreApplication::translate("MainWindow", "Chinese", nullptr)); 82 | label->setText(QCoreApplication::translate("MainWindow", "This is \"Test Text\"", nullptr)); 83 | menuLanguage->setTitle(QCoreApplication::translate("MainWindow", "Language", nullptr)); 84 | } // retranslateUi 85 | 86 | }; 87 | 88 | namespace Ui { 89 | class MainWindow: public Ui_MainWindow {}; 90 | } // namespace Ui 91 | 92 | QT_END_NAMESPACE 93 | 94 | #endif // UI_MAINWINDOW_H 95 | -------------------------------------------------------------------------------- /demonstrate/EventPenetrate.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mengps/QmlExamples/3ad6857f9991ab398e0051e456711f30f802fcde/demonstrate/EventPenetrate.gif -------------------------------------------------------------------------------- /demonstrate/MultiView.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mengps/QmlExamples/3ad6857f9991ab398e0051e456711f30f802fcde/demonstrate/MultiView.gif -------------------------------------------------------------------------------- /demonstrate/Translator_qml.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mengps/QmlExamples/3ad6857f9991ab398e0051e456711f30f802fcde/demonstrate/Translator_qml.gif -------------------------------------------------------------------------------- /demonstrate/Translator_widget.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mengps/QmlExamples/3ad6857f9991ab398e0051e456711f30f802fcde/demonstrate/Translator_widget.gif -------------------------------------------------------------------------------- /demonstrate/demonstrate.md: -------------------------------------------------------------------------------- 1 | ## 示例预览 2 | 3 | - MultiView Qml中实现多视图,多图像源(QImage / QPixmap) 4 | 5 |
6 | 7 | - Translator Qt / Qml 中支持多国语言 8 | 9 |
10 |
11 | 12 | - EventPenetrate MouseArea 的 Click / Hover 事件穿透 13 | 14 |
15 | --------------------------------------------------------------------------------