QQuickView custom resize to behave in a PreserveAspectRatio - mode
-
I am wondering if there is a simple way of implementing (or maybe there is already there, and only waits to be used) a resize mechanism which behaves like PreserveAspectRatio does for Image qml-component.
In my application the entire UI is developed using QtQuick, and the main qml will be loaded inside one QQuickView.
UI is implemented in a static way. Every component and item has a fix position, and images which are used are also fixed.I know that as a best practice, I should use anchors in qml, and I should create the entire UI like in web technologies (everything is relative to parent's properties). Than when the resize mode is set to SizeRootObjectToView, it will work.
@viewer.setResizeMode(QQuickView::SizeRootObjectToView);@But changing my entire UI will be time consuming, and I am thinking to leave it like it is now, and only the find a strategy to resize QQuickView in a PreserveAspectRatio way.
Any sugestions from where I should start?
-
the easiest way is to react on resizeEvent, when the main window is resized, I just keep the new size and emit a signal scaledsizeChanged which later will be used inside the myitem.
qtquick2applicationviewer.cpp
@Q_PROPERTY(QRectF scaledsize READ scaledsize WRITE setScaledsize NOTIFY scaledsizeChanged)
...
void QtQuick2ApplicationViewer::resizeEvent(QResizeEvent *e)
{
QQuickView::resizeEvent(e);m_size.setHeight(e->size().height()); m_size.setWidth(e->size().width()); emit scaledsizeChanged();
}
@
in main qml I will react on this signal, and call the resize method from myitem.main.qml
@import QtQuick 2.0
import com.mycompany.qmlcomponents 1.0MyItem {
id:parentItemConnections { target: ApplicationViewer onScaledsizeChanged: resizePreserveAspectRatio(ApplicationViewer.scaledsize) } //initial size width: 640; height: 480@
myitem.cpp
@void MyItem::resizePreserveAspectRatio(const QRectF &r)
{
setTransformOrigin(TopLeft);
setScale((r.height()/height()<r.width()/width())?(r.height()/height()):(r.width()/width()));
}
@In this way QuickView will scale accordingly with the space available in the main Window.
but still, is there a way of doing this, using an existing API?