欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 国际 > 【Qt】Qt C++ Widget中嵌入qml

【Qt】Qt C++ Widget中嵌入qml

2024/10/31 19:11:45 来源:https://blog.csdn.net/yangshuoSB/article/details/142203352  浏览:    关键词:【Qt】Qt C++ Widget中嵌入qml

1. 效果

在这里插入图片描述

2. 方法

  • 使用QQuickWidget方式
	QQuickWidget *view = new QQuickWidget;view->setSource(QUrl::fromLocalFile("myqmlfile.qml"));view->show();
  • 除了QQuickWidget方式还可以使用QQuickView方式,请自行查阅资料

3. 代码

3.1 工程目录

在这里插入图片描述

3.2 pro文件

备注:需要添加 quickwidgets

QT       += core gui quickwidgetsgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++17# 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_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \dialog.cppHEADERS += \dialog.hFORMS += \dialog.ui# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += targetDISTFILES +=RESOURCES += \res.qrc

3.3 main.cpp

#include "dialog.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Dialog w;w.show();return a.exec();
}

3.4 dialog.h

#ifndef DIALOG_H
#define DIALOG_H#include <QDialog>QT_BEGIN_NAMESPACE
namespace Ui { class Dialog; }
QT_END_NAMESPACEclass Dialog : public QDialog
{Q_OBJECTpublic:Dialog(QWidget *parent = nullptr);~Dialog();signals:cppClick();public slots:void onQmlClick();private slots:void on_pushButton_clicked();private:Ui::Dialog *ui;
};
#endif // DIALOG_H

3.5 dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"
#include <QtQuickWidgets>Dialog::Dialog(QWidget *parent): QDialog(parent), ui(new Ui::Dialog)
{ui->setupUi(this);//c++向qml中传数据ui->quickWidget->rootContext()->setContextProperty("$color", "red");//qml导入QUrl source("qrc:/qmlFile.qml");ui->quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);ui->quickWidget->setSource(source);ui->quickWidget->setClearColor(QColor(Qt::transparent));//qml与c++交互QQuickItem* qmlItem = ui->quickWidget->rootObject();connect(qmlItem, SIGNAL(qmlClick()), this, SLOT(onQmlClick()));connect(this, SIGNAL(cppClick()), qmlItem, SLOT(onCppClick()));
}Dialog::~Dialog()
{delete ui;
}void Dialog::onQmlClick()
{qDebug() << "this is C++: qml's click";
}void Dialog::on_pushButton_clicked()
{emit cppClick();
}

3.6 qmlFile.qml

import QtQuick 2.15
import QtQuick.Controls 2.15Item {id: _mRootsignal qmlClick();function onCppClick(){console.log("this is qml: cpp's click")}Rectangle{width: 200height: 200color: $colorborder.color: "grey"border.width: 1Button{width: 100height: 50text: "qml button"anchors.centerIn: parentonClicked: {qmlClick();}}}
}

3.7 dialog.ui

在这里插入图片描述

4. 参考

  • Qt 之 QWidget嵌入Qml文件以及如何交互
  • 通俗易懂玩QT:QQuickWidget学习

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com