江苏营销型网站策划,四川网站建设电话咨询,seo快速入门教程,梅州市建设局网站1. 目的
qml做出的界面漂亮#xff0c;但是执行效率低#xff0c;一直想找一个方法实现qml中嵌入c界面。现在从网上找到一个方法#xff0c;简单试了一下貌似可行#xff0c;分享一下。
2. 显示效果 3. 代码
3.1 工程结构 3.2 pro文件
需要添加widgets QT quick …1. 目的
qml做出的界面漂亮但是执行效率低一直想找一个方法实现qml中嵌入c界面。现在从网上找到一个方法简单试了一下貌似可行分享一下。
2. 显示效果 3. 代码
3.1 工程结构 3.2 pro文件
需要添加widgets QT quick widgets
QT quick widgets# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES QT_DISABLE_DEPRECATED_BEFORE0x060000 # disables all the APIs deprecated before Qt 6.0.0SOURCES \main.cppRESOURCES qml.qrc# Additional import path used to resolve QML modules in Qt Creators 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
3.3 main.cpp文件重点
#include QGuiApplication
#include QQmlApplicationEngine
#include QWindow
#include QtWidgets/QWidget
#include QPushButton
#include QApplicationint main(int argc, char *argv[])
{
#if QT_VERSION QT_VERSION_CHECK(6, 0, 0)QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endifQApplication app(argc, argv); //这里改为QApplication QQmlApplicationEngine engine;const QUrl url(QStringLiteral(qrc:/main.qml));QObject::connect(engine, QQmlApplicationEngine::objectCreated,app, [url](QObject *obj, const QUrl objUrl) {if (!obj url objUrl)QCoreApplication::exit(-1);}, Qt::QueuedConnection);engine.load(url);//获取QML源窗口QObject *QmlObj engine.rootObjects().first();QWindow *QmlWindow qobject_castQWindow *(QmlObj);QmlWindow-setTitle(C set title);WId parent_HWND QmlWindow-winId();//新建widgetQWidget widget;widget.setGeometry(0, 0, 300, 300);QPushButton btn(send, widget);btn.setGeometry(5, 5, 60, 20);widget.winId();//将widget插入QMLwidget.windowHandle()-setParent(QmlWindow);widget.show();return app.exec();
}
3.4 main.qml文件
这里的titile的Helllo World被c修改了
import QtQuick 2.15
import QtQuick.Window 2.15Window {width: 640height: 480visible: truetitle: qsTr(Hello World)
}
4. 参考
震惊QWidget竟然可以嵌入到QML中QMl窗口句柄竟然是这样获取