Fluent-Qt

Embed QML (QmlBridge)

QmlView

A QML surface that shares application models and Fluent theme tokens with the surrounding Widgets.

Example

QmlView: Load a QML page

Usage

Load a QML page

Activate a QmlSession, create a QmlView with QmlSession::createView(), and load a QML URL. When sharing a window with OpenGL views, select the Quick graphics backend before creating the first Quick view.

#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();
}

fluent::qmlbridge::QmlView

Inherits QWidget · FluentElement · QMLPlus

QWidget container for embedding and displaying an application QML page. Attach to an active QmlSession before use; load only trusted QML (scenes unload automatically when the session deactivates).

Constructors

Properties

QUrl source
Read source()Notify sourceChanged()
Status status
Read status()Notify statusChanged()
bool active
Read isActive()Notify activeChanged()
QStringList errors
Read errors()Notify errorsChanged()
QString lastError
Read lastError()Notify errorsChanged()
QObject* theme

Returns the view-owned fluentTheme context object.

Read themeObject()
bool textureRenderingEnabled

Whether offscreen texture rendering is enabled.

Read isTextureRenderingEnabled()Write setTextureRenderingEnabled()Notify textureRenderingEnabledChanged()

Methods

QObject* rootObject() const

Returns the loaded QML root object, or nullptr until ready or after unload().

QObject* themeObject() const

Returns the view-owned fluentTheme context object.

bool setContextObject(const QString& name, QObject* object)

Registers a GUI-thread QObject under name before loading (pass nullptr to remove; "fluentTheme" is reserved). Caller retains object lifetime; bindings persist across unload() and destroying a bound object unloads the scene.

bool load(const QUrl& source, QString* error = nullptr)

Loads an absolute QML URL; returns true when ready or async loading starts. Watch statusChanged() and errorOccurred() for async errors; call unload() first to reload the same URL.

void unload()

Unloads the QML scene and resets status while keeping external context bindings intact. Safe to call from QML callbacks (teardown is deferred until callbacks unwind).

bool setTextureRenderingEnabled(bool enabled)

Toggles offscreen OpenGL texture rendering without reloading the QML scene. When enabled, the caller must invoke renderTexture() and draw the texture; may be set before load().

bool isTextureRenderingEnabled() const

Whether offscreen texture rendering is enabled.

bool renderTexture(QOpenGLContext* context, qreal devicePixelRatio, quint64 maximumBytes = 67108864)

Renders the QML scene into a texture using the caller's current OpenGL context (GUI thread only). devicePixelRatio must be in (0, 4]; maximumBytes caps color/depth buffers.

unsigned int textureId() const

Returns the premultiplied RGBA OpenGL texture (bottom-left origin), or 0 if unavailable. Invalidated on releaseTexture(), unload(), resize, or context change.

QSize textureSize() const

Texture dimensions in device pixels.

quint64 textureRevision() const

Frame revision counter incremented on each rendered frame.

QVariantMap textureStatistics() const

Diagnostics map with texture memory usage, frame count, and last error.

void releaseTexture()

Frees GPU render targets while keeping the QML scene and texture-mode setting intact.

Signals

void textureRenderingEnabledChanged(bool enabled)

Whether offscreen texture rendering is enabled.

Enumerations

enum Status
  • Unloaded
  • Loading
  • Ready
  • Error

Destructors

Protected members

Methods

Inherited from fluent::FluentElement

Methods

static void setTheme(Theme theme)

Updates the global theme and notifies active FluentElement instances.

static void setThemeDeferred(Theme theme)

Schedules a visible-first batched theme refresh without blocking the current interaction.

static Theme currentTheme()

Returns the currently active global theme.

static bool themeUsesDarkAppearance(Theme theme)

Returns whether a theme uses dark-backed chrome and material tokens.

static void refreshTheme()

Re-broadcasts the current theme to repaint after a design-token change.

static int themeGeneration()

Monotonic counter bumped on every theme change, for staleness checks.

Theme effectiveTheme() const

Resolves the theme used by this element, honoring an inherited QWidget override.

bool effectiveThemeUsesDarkAppearance() const

Returns whether this element's effective theme uses dark-backed chrome.

bool setThemeOverrides(const QJsonObject& overrides)

Replaces this element's sparse color, radius, and font token overrides.

QJsonObject themeOverrides() const

Returns this element's authored overrides.

void clearThemeOverrides()

Restores all token fields to global theme defaults.

const Colors& themeColorsRef() const

Non-copying access to the active color set for hot paint paths.

Material::AcrylicToken themeAcrylic() const

Material, elevation, and adaptive layout tokens.

QColor themeBackdrop(bool active) const

Window-chrome backdrop color for the given activation state.

virtual void onThemeUpdated()

Called after the global theme changes.

Enumerations

enum Theme

Global visual theme used when resolving design tokens.

  • Light = 0
  • Dark = 1
  • HighContrast = 2

Types and aliases

struct Colors

Design token snapshots returned to components.

Inherited from fluent::QMLPlus

Methods

AnchorLayout::Anchors* anchors()

Returns mutable anchors for the QWidget host.

void setState(const QString& name)

Applies a registered state by name, or restores the default state when @p name is empty. Unknown names are ignored, and existing bindings resume when leaving the state.

void addState(const QMLState& state)

Registers or replaces a named state definition; replacing the active state applies it immediately.

void evaluateStates()

Reevaluates state conditions in registration order and activates the first match, or the default state if none match. Also runs automatically when dependency properties notify; derived states using extend inherit ancestor changes unless cyclic or missing.

QMLPlusRuntime* runtime()

Returns the lazily created state observer owned by this host.

void setAnchors(const AnchorLayout::Anchors& anchors)

Replaces all host anchors and invalidates the parent layout.

void resetAnchors()

Clears all host anchors and invalidates the parent layout.

void bind(const char* targetProp, QObject* source, const char* sourceProp, PropertyBinder::Direction dir = PropertyBinder::OneWay)

Binds @p sourceProp on @p source to @p targetProp on this QWidget host via PropertyBinder::bind().

void addTransition(const StateTransition& transition)

Registers a state transition in precedence order.

void setBehavior(const char* propertyName, const PropertyAnimation& animation)

Sets the default animation used when @p propertyName is written via writeProperty() or a binding. Explicit StateTransition rules take precedence; direct C++ setter calls are not animated.

void clearBehavior(const char* propertyName)

Removes the default animation for future managed writes.

bool writeProperty(const char* propertyName, const QVariant& value)

Writes a host property through Behavior without removing its binding.

Qt base class docs: QWidget