I have Qt Creator 2.6.1. I created simple Qt Qucik 2.0 project from project templates and changed main.qml file on this:
import QtQuick 2.0
Rectangle {
width: 360
height: 360
color: "red"
MouseArea {
anchors.fill: parent
onClicked: parent.height = 180
}
}
If i will click on rectangle it should be reduced in half. And it is occured, but window is not reduced.
What is a best solution, if i want that main window must repeat geometry of main qml rectangle?
UPDATE. One solution was found. See Amit Tomar answer. But is exist more easier solution, for example, using QtQuick 5.0: Qt Quick Window QML Types ?
You are changing the geometry of your qml, not the Viewer. To do so:
Steps:
main.cpp
#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include "qdeclarativecontext.h"
#include "myclass.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QmlApplicationViewer *viewer = new QmlApplicationViewer();
MyClass myClassObject(viewer);
viewer->rootContext()->setContextProperty("myViewer", &myClassObject);
viewer->setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
viewer->setMainQmlFile(QLatin1String("qml/untitled/main.qml"));
viewer->showExpanded();
return app.exec();
}
myclass.h
#ifndef MYCLASS_H
#define MYCLASS_H
#include "qmlapplicationviewer.h"
class MyClass : public QObject
{
Q_OBJECT
public:
MyClass( QmlApplicationViewer * p ) { internalViewer = p ; }
Q_INVOKABLE void viewerResize(int x, int y, int length, int breadth)
{
internalViewer->setGeometry(internalViewer->x(),internalViewer->y(),length,breadth);
}
private:
QmlApplicationViewer *internalViewer;
};
#endif // MYCLASS_H
main.qml
import QtQuick 1.0
Rectangle {
width: 360
height: 360
Text {
text: "Hello World"
anchors.centerIn: parent
}
MouseArea {
anchors.fill: parent
onClicked:
{
myViewer.viewerResize(0,0,110,110)
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With