It appears that the accepted answer for this question works, so you could expose that font to QML as a context property:
main.cpp:
#include <QApplication>
#include <QFontDatabase>
#include <QQmlApplicationEngine>
#include <QQmlContext>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
QQmlApplicationEngine engine;
const QFont fixedFont = QFontDatabase::systemFont(QFontDatabase::FixedFont);
engine.rootContext()->setContextProperty("fixedFont", fixedFont);
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
return app.exec();
}
main.qml:
import QtQuick 2.0
import QtQuick.Window 2.0
import QtQuick.Controls 2.0
Window {
width: 400
height: 400
visible: true
Switch {
id: monospaceSwitch
}
Text {
text: "Hello World"
font: monospaceSwitch.checked ? fixedFont : Qt.application.font
anchors.centerIn: parent
}
}
This assumes that a monospace font exists on the system.