Hi,
I have a qml file which contains a Rectangle Element as the root item. I want this rectangle item to be displayed as the mainwindows central widget and this Rectangle Element should be resized according to the mainwindow. So i did like this,
MainWindow
::MainWindow(QWidget *parent
){
ui->setupUi(this);
QDeclarativeView *view = new QDeclarativeView;
setCentralWidget(view);
QDeclarativeEngine *engine = view->engine();
QDeclarativeComponent component
(engine,
QUrl::fromLocalFile("qml_files/screen.qml"));
object = component.create();
view->scene()->addItem(qobject_cast<QDeclarativeItem*>(object));
}
{
object->setProperty("width", width());
object->setProperty("height", height());
}
screen.qml
Rectangle {
id: window
width: 480; height: 360
color: "#282828"
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
QDeclarativeView *view = new QDeclarativeView;
setCentralWidget(view);
QDeclarativeEngine *engine = view->engine();
QDeclarativeComponent component(engine, QUrl::fromLocalFile("qml_files/screen.qml"));
object = component.create();
view->setScene(new QGraphicsScene);
view->scene()->addItem(qobject_cast<QDeclarativeItem*>(object));
}
void MainWindow::resizeEvent(QResizeEvent *)
{
object->setProperty("width", width());
object->setProperty("height", height());
}
screen.qml
Rectangle {
id: window
width: 480; height: 360
color: "#282828"
}
To copy to clipboard, switch view to plain text mode
which uses QDeclarativeView, QGraphicsScene, QDeclarativeComponent and QDeclarativeItem, which seems to work. Am i doing it right or a much simpler solution exist
Bookmarks