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.0
MyItem {
id:parentItem
Connections {
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?
↧