main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QDeclarativeView>
#include <QDeclarativeContext>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QDeclarativeView view;
QDeclarativeContext *context = view.rootContext();
context->setContextProperty("backgroundColor",
QColor(Qt::yellow));
view.setSource(QUrl::fromLocalFile("main.qml"));
view.show();
return app.exec();
}
main.qml
import QtQuick 1.1
Rectangle {
width: 100
height: 62
color:backgroundColor
Text {
anchors.centerIn: parent
text: "Hello Yellow World!"
}
}
截图效果: