Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved QResizeEvent alternative for QQuickItem

    QML and Qt Quick
    2
    4
    1607
    Loading More Posts
    • 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.
    • E
      Eligijus last edited by

      Hello,

      Is there an alternative to QResizeEvent for QQuickItem?

      1 Reply Last reply Reply Quote 0
      • sierdzio
        sierdzio Moderators last edited by

        Is that what you mean?

        Window {
          onWidthChanged: console.log("New width: " + width)
        }
        

        However, QResizeEvent comes from QtGui - QML windows also respond to it. You can probably catch the event on C++ side if you need to.

        (Z(:^

        E 1 Reply Last reply Reply Quote 0
        • E
          Eligijus @sierdzio last edited by

          @sierdzio I should have specified that I need to catch it on c++ side. I have tried overriding event method and catching QResizeEvent but it was never captured.
          Code:

          bool Test::event(QEvent *ev)
          {
              if (ev->type() == QEvent::Resize) {
                  QResizeEvent *resizeEvent = static_cast<QResizeEvent *>(ev);
                  qDebug() << resizeEvent;
          
                  return true;
              }
          
              return QObject::event(ev);
          }
          

          method to change size:

          Q_INVOKABLE void incSize();
          
          void Test::incSize()
          {
              setWidth(width() - 1);
              setHeight(height() - 1);
          }
          

          qml side, button changes Test's size.

              Test {
                  id: tsX
                  width: 100.2
                  height: 100.2
                  y: 2
              }
          
              Button {
                  x: 150
                  y: 100
                  text: "check2"
                  MouseArea {
                      anchors.fill: parent
                      onClicked: { tsX.incSize() }
                  }
              }
          
          1 Reply Last reply Reply Quote 0
          • sierdzio
            sierdzio Moderators last edited by

            Resize events happen for windows and widgets, not QQuickItems (I assume Test is a QQuickItem). See the event list from the documentation.

            If you want to catch this from C++ side, you need to connect to height/widthChanged() signal, or reimplenent geometryChanged() slot.

            (Z(:^

            1 Reply Last reply Reply Quote 2
            • First post
              Last post