Skip to content

UniDeskExpr Type

Expression engine singleton that supports %variable syntax for system data references and %{math expression} syntax for numerical computations in text. All text controls (e.g. UDCText) call convertStr() before rendering.

ItemDescription
Control TypeGlobal Singleton (QML_SINGLETON)
Source FileUniDesk/CppExt/UniDeskExpr.h / .cpp
InheritsQQuickItem
QML Importimport UniDesk 1.0

Properties

readonly property SystemStats systemStats

Cached system stats, updated every second by an internal timer. The SystemStats struct contains:

  • cpu.usagePercent / cpu.temperature — CPU usage and temperature
  • cpu.name / cpu.physicalCores / cpu.logicalCores / cpu.maxClockMHz — CPU model and specs
  • gpu.name / gpu.usagePercent / gpu.vramTotal / gpu.vramUsed / gpu.temperature — GPU info
  • mem.virtmemTotal / mem.virtmemUsed / mem.virtmemPercent — Physical memory
  • mem.swapmemTotal / mem.swapmemUsed / mem.swapmemPercent — Swap memory
  • net.bytesSend / net.bytesRecv — Total network traffic
  • net.bytesSendPerSec / net.bytesRecvPerSec — Network rate per second
  • net.dropPercent — Packet drop rate
  • bat.batteryPercent / bat.charging / bat.remainMinutes — Battery info
  • disk.totalSpace / disk.freeSpace / disk.usagePercent — Disk info
  • sysInfo.uptimeSeconds / sysInfo.hostname / sysInfo.osName — System info
  • sysInfo.screenWidth / sysInfo.screenHeight — Screen resolution

Methods

function convertStr(text, presets) → QString

Parses all %variable and %{math expression} tokens in the text and replaces them with actual values.

Replacement priority:

  1. All %variable tokens (system data, date/time) are replaced directly via QString::replace
  2. Then %{math expression} blocks are evaluated with the exprtk library, with custom variables from presets
qml
import UniDesk 1.0

// %variable replacement
var result = UniDeskExpr.convertStr("CPU: %cpuPercent%")
// → "CPU: 23.5%"

// %{math expression} evaluation (system variables still need % prefix inside)
var result = UniDeskExpr.convertStr(
    "Speed: %{%bytesRecvPerSec/1024} KB/s"
)
// → "Speed: 512.5 KB/s"

// Custom variables via presets (no % prefix needed)
var result = UniDeskExpr.convertStr(
    "Total: %{a + b}",
    { "a": 10, "b": 20 }
)
// → "Total: 30"

function evalResponse(response, expression) → QVariant

Parses an API response (JSON string) and evaluates a JS expression in the response global object context, supporting dictionary and list access.

qml
var value = UniDeskExpr.evalResponse(
    '{"temp": 26.5, "weather": {"city": "Beijing"}}',
    "weather.city"
)
// → "Beijing"

function stopTimer()

Stops the internal data refresh timer (typically called on application exit).

%variable Replacement Table

All %variable tokens are matched and replaced directly via QString::replace, executed in order — earlier matches take priority.

CPU

VariableDescriptionType
%cpuPercentCPU usagedouble (%)
%cpuTempCPU temperaturedouble (°C)
%cpuNameCPU model namestring
%cpuCoresCPU logical core count (threads)int
%cpuMaxClockCPU max clock speeddouble (MHz)

GPU

VariableDescriptionType
%gpuUsagePercentGPU usagedouble (%)
%gpuVramTotalGPU total VRAMuint64 (bytes)
%gpuVramUsedGPU used VRAMuint64 (bytes)
%gpuTempGPU temperaturedouble (°C)
%gpuNameGPU model namestring

Memory

VariableDescriptionType
%virtmemTotalTotal physical memoryuint64 (bytes)
%virtmemUsedUsed physical memoryuint64 (bytes)
%virtmemPercentPhysical memory usagedouble (%)
%swapmemTotalTotal swap memoryuint64 (bytes)
%swapmemUsedUsed swap memoryuint64 (bytes)
%swapmemPercentSwap memory usagedouble (%)

Network

VariableDescriptionType
%bytesSendTotalTotal bytes sentuint64
%bytesRecvTotalTotal bytes receiveduint64
%bytesSendPerSecBytes sent per seconduint64
%bytesRecvPerSecBytes received per seconduint64
%dropPercentPacket drop ratedouble (%)

Battery

VariableDescriptionType
%bpercentBattery percentageint
%bplugCurrently charging (1/0)int
%bleftdaysDays remaining (UNLIMITED when charging)string
%blefthoursHours remaining (UNLIMITED when charging)string
%bleftminsMinutes remaining (UNLIMITED when charging)string
%blefthoursrHours remaining (excluding days)string
%bleftminsrMinutes remaining (excluding hours)string

Disk

VariableDescriptionType
%diskTotalSystem drive total spaceuint64 (bytes)
%diskFreeSystem drive free spaceuint64 (bytes)
%diskPercentSystem drive usagedouble (%)

System

VariableDescriptionType
%uptimeSystem uptimequint64 (seconds)
%hostnameHost namestring
%osNameOperating system namestring
%screenWidthPrimary screen widthint (pixels)
%screenHeightPrimary screen heightint (pixels)

Date & Time

VariableDescriptionExample
%yyyy4-digit year2026
%yy2-digit year26
%MMMMFull month nameAugust
%MMMAbbreviated monthAug
%MM2-digit month08
%MMonth8
%ddddFull day nameSaturday
%dddAbbreviated daySat
%dd2-digit day22
%dDay22
%HH2-digit hour (24h)14
%hh2-digit hour (12h)02
%HHour (24h)14
%hHour (12h)2
%mm2-digit minute35
%mMinute35
%ss2-digit second59
%sSecond59
%apLowercase am/pmam / pm
%APUppercase AM/PMAM / PM
%zzz3-digit milliseconds123
%zMilliseconds123
%tTimestamp1756878959

Calendar

VariableDescriptionType
%isLeapYearLeap year (1/0)int
%yearDaysDays in current yearint
%monthDaysDays in current monthint
%dayOfYearDay of yearint
%dayOfWeekDay of week (1=Monday)int

%{math expression} Blocks

%{...} supports exprtk math expression syntax. Execution flow:

  1. All %variable tokens (including those inside %{}) are replaced with their actual numeric values
  2. The content inside %{...} is then evaluated as a math expression

Therefore, when referencing system variables inside %{}, you must keep the % prefix (e.g. %{%cpuPercent * 2}).

Custom variables passed via presets do not need a % prefix — use them directly by name.

Supports:

  • Arithmetic: + - * / ^
  • Math functions: sin cos sqrt abs log exp min max etc.
  • Comparison: > < >= <= == !=
  • Nested parentheses
qml
// Unit conversion (system variables need % prefix, substituted before evaluation)
"%{%bytesRecvPerSec/1048576} MB/s"

// Percentage calculation
"%{%virtmemUsed/%virtmemTotal*100}%"

// CPU temperature to Fahrenheit
"%{%cpuTemp * 9 / 5 + 32}°F"

// GPU VRAM usage percentage
"%{%gpuVramUsed / %gpuVramTotal * 100}%"

// Using preset variables (no % prefix)
UniDeskExpr.convertStr("%{price * count}", { "price": 9.9, "count": 3 })
// → "29.7"

// Mixed usage
UniDeskExpr.convertStr("%{%cpuPercent + bonus}", { "bonus": 5 })
// → "28.5"

Full Example

qml
import UniDesk 1.0
import UniDesk.Controls 1.0

UDCText {
    textContent: "CPU: %cpuPercent%  GPU: %gpuUsagePercent%  Memory: %virtmemPercent%"
}

UDCText {
    textContent: "CPU Temp: %cpuTemp°C  GPU Temp: %gpuTemp°C"
}

UDCText {
    textContent: "GPU: %gpuName  VRAM: %{%gpuVramUsed/1048576}MB"
}

UDCText {
    textContent: "Network: %{%bytesRecvPerSec/1048576} MB/s  Disk: %diskPercent%"
}

UDCText {
    textContent: "%osName | %hostname | Uptime: %uptime seconds"
}

Implementation Details

  • System data is refreshed by an internal 1000ms (1 second) timer.
  • % variables use QString::replace for direct substitution — no escaping. To display a literal %, use two consecutive % (%%), which the engine temporarily replaces with an internal placeholder [(*&*%^*$^%#%%^^&&*^*&(^))] before restoring to % after all variables are processed.
  • %{} expressions support nested parentheses, using a bracket-counting algorithm to find matching closing brackets.
  • Unmatched %variable or mismatched %{} brackets are preserved as-is.
  • %variable substitution happens before %{} evaluation, so system variables inside %{} are already replaced with their numeric values.
  • Temperature variables may return -1 (unavailable) on some devices; use conditional checks in UI to handle this.

Released under the CC BY-SA 4.0 open documentation license.