Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QQuickView custom resize to behave in a PreserveAspectRatio - mode
QtWS25 Last Chance

QQuickView custom resize to behave in a PreserveAspectRatio - mode

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 1 Posters 3.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    danut.iurascu
    wrote on last edited by
    #1

    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?

    regards,
    Dănuț

    1 Reply Last reply
    0
    • D Offline
      D Offline
      danut.iurascu
      wrote on last edited by
      #2

      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?

      regards,
      Dănuț

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved