QmlBridge:嵌入 QML
QmlView QML 视图
在 Widgets 界面中加载 QML 页面,并共享数据和主题。
示例

用法
加载 QML 页面
先激活 QmlSession,再用 createView() 创建 QmlView 并加载 QML 文件;激活失败时显示错误信息。
#include <FluentQt/QmlBridge.h>
using namespace fluent::qmlbridge;
// main.cpp; build with CMakeLists.txt and keep the QML page in its own file.
#include <FluentQt/FluentQt.h>
#include <FluentQt/QmlBridge.h>
#include <QApplication>
#include <QHBoxLayout>
#include <QVBoxLayout>
using fluent::basicinput::Button;
using fluent::qmlbridge::QmlSession;
using fluent::qmlbridge::QmlView;
using fluent::status_info::ProgressBar;
using fluent::textfields::Label;
using fluent::textfields::LineEdit;
using fluent::windowing::Window;
QWidget* makePanel(const QUrl& source, QWidget* parent)
{
auto* panel = new QWidget(parent);
auto* column = new QVBoxLayout(panel);
column->setContentsMargins(16, 16, 16, 16);
column->setSpacing(12);
column->setAlignment(Qt::AlignTop);
auto* session = new QmlSession(panel, panel);
QString error;
QmlView* view = session->activate(&error) ? session->createView(panel) : nullptr;
if (!view) {
column->addWidget(new Label(error.isEmpty() ? "QML is unavailable" : error, panel));
return panel;
}
view->setFixedHeight(100);
column->addWidget(view);
view->load(source);
return panel;
}
int main(int argc, char* argv[])
{
fluent::prepareHighDpiApplication();
QmlSession::prepareApplication();
QApplication application(argc, argv);
fluent::initializeResources();
application.setFont(Typography::fontStyle(Typography::FontRole::Body).toQFont());
Window window;
window.setWindowTitle("QmlView");
window.resize(640, 480);
window.setContentWidget(makePanel(QUrl("qrc:/Basic.qml"), &window));
window.show();
return application.exec();
}import fluentqt
from fluentqt.qmlbridge import QmlSession
# main() calls QmlSession.prepareApplication() before QApplication.
# panel and qml_source() are defined in Full example.
session = QmlSession(panel, panel)
view = session.createView(panel) if session.activate() else None
if view is not None:
view.setFixedHeight(100)
view.load(qml_source("Basic"))API
查看声明没有匹配的 API 成员。
fluent::qmlbridge::QmlView
继承自 QWidget · FluentElement · QMLPlus
在调用方持有的 QWidget 中承载应用的 QML 页面。
构造函数
属性
QUrl source- 读取
source()通知sourceChanged() Status status- 读取
status()通知statusChanged() bool active- 读取
isActive()通知activeChanged() QStringList errors- 读取
errors()通知errorsChanged() QString lastError- 读取
lastError()通知errorsChanged() QObject* theme返回视图持有的 fluentTheme 对象。
读取themeObject()bool textureRenderingEnabled返回请求的纹理呈现模式。
读取isTextureRenderingEnabled()设置setTextureRenderingEnabled()通知textureRenderingEnabledChanged()
方法
QObject* rootObject() const返回借用的 QML 根对象,卸载或替换场景后失效。
QObject* themeObject() const返回视图持有的 fluentTheme 对象。
bool setContextObject(const QString& name, QObject* object)加载前绑定借用的 GUI 线程 QObject;nullptr 移除绑定。
bool load(const QUrl& source, QString* error = nullptr)加载绝对 URL;true 表示已就绪或已接受异步加载。
void unload()分离 QML 场景并安排安全销毁其引擎,不销毁借用模型。
bool setTextureRenderingEnabled(bool enabled)选择 GPU 纹理呈现,保留同一 QML 场景。
bool isTextureRenderingEnabled() const返回请求的纹理呈现模式。
bool renderTexture(QOpenGLContext* context, qreal devicePixelRatio, quint64 maximumBytes = 67108864)在调用方当前 OpenGL 上下文中绘制或复用场景。
unsigned int textureId() const返回借用的预乘 RGBA、下原点 GL 纹理,未就绪时为零;释放、卸载、尺寸或上下文改变后失效。
QSize textureSize() const返回纹理的设备像素尺寸。
quint64 textureRevision() const返回已绘制内容的单调递增版本。
QVariantMap textureStatistics() const返回 GPU 分配、帧及失败诊断。
void releaseTexture()释放 GPU 目标,保留场景及模式请求。
信号
枚举
enum StatusUnloadedLoadingReadyError
析构函数
子类扩展接口
方法
继承自 fluent::FluentElement
方法
static void setTheme(Theme theme)更新全局主题,并通知所有活跃的 FluentElement 实例。
static void setThemeDeferred(Theme theme)以可见元素优先的批次调度主题刷新,避免阻塞当前交互。
static Theme currentTheme()返回当前生效的全局主题。
static bool themeUsesDarkAppearance(Theme theme)返回指定主题是否使用暗色底的 chrome 与材质 token。
static void refreshTheme()在设计 token 变化后重新广播当前主题以触发重绘。
static int themeGeneration()每次主题变化自增的单调计数器,用于过期判断。
Theme effectiveTheme() const解析此元素实际使用的主题,会优先读取 QWidget 父链上的局部主题覆盖。
bool effectiveThemeUsesDarkAppearance() const返回此元素的实际主题是否使用暗色底 chrome。
bool setThemeOverrides(const QJsonObject& overrides)供组件侧访问的设计 token 接口。
QJsonObject themeOverrides() const返回当前元素的覆盖配置。
void clearThemeOverrides()恢复全部 token 跟随全局主题。
const Colors& themeColorsRef() const对当前色板的零拷贝访问(用于绘制热路径)。
Material::AcrylicToken themeAcrylic() const材质、阴影和自适应布局 token。
QColor themeBackdrop(bool active) const给定激活状态下的窗口 chrome 背景色。
virtual void onThemeUpdated()全局主题变化后调用。
枚举
enum Theme解析设计 token 时使用的全局视觉主题。
Light = 0Dark = 1HighContrast = 2
类型与别名
struct Colors返回给组件使用的设计 token 快照。
继承自 fluent::QMLPlus
方法
AnchorLayout::Anchors* anchors()返回 QWidget 宿主的可变锚点配置。
void setState(const QString& name)应用具名 state;名称为空时恢复默认状态。
void addState(const QMLState& state)注册或替换一个具名 state 定义。
void evaluateStates()重新计算条件;按注册顺序选择首个成立的条件。
QMLPlusRuntime* runtime()返回按需创建、由宿主持有的状态观察对象。
void setAnchors(const AnchorLayout::Anchors& anchors)替换宿主的全部锚点,并使父布局失效。
void resetAnchors()清除宿主全部锚点,并使父布局失效。
void bind(const char* targetProp, QObject* source, const char* sourceProp, PropertyBinder::Direction dir = PropertyBinder::OneWay)将 source 属性绑定到 QWidget 宿主上的 target 属性。
BindingHandle bindManaged(const char* targetProp, QObject* source, const char* sourceProp, PropertyBinder::Direction direction = PropertyBinder::OneWay, BindingOptions options = {})在当前 QWidget 宿主上安装可管理的直接绑定。
BindingHandle bindComputed(const char* targetProp, const QVector<BindingDependency>& dependencies, std::function<QVariant()> evaluate, BindingOptions options = {})在当前 QWidget 宿主上安装使用显式依赖的 C++ 表达式绑定。
void setBehavior(const char* propertyName, const PropertyAnimation& animation)设置宿主受管理属性写入时使用的默认动画。
void clearBehavior(const char* propertyName)移除后续受管理写入使用的默认动画。
bool writeProperty(const char* propertyName, const QVariant& value)通过 Behavior 写入宿主属性,且不移除该属性的绑定。
Qt 基类完整成员:QWidget