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. [solved] Retrieve window geometry
Forum Updated to NodeBB v4.3 + New Features

[solved] Retrieve window geometry

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 1 Posters 951 Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hi,

    I'm hacking around the missing support for (Wacom) graphics tablets in QtQuick. This is my component:

    @
    import QtQuick 2.4
    import domain.program 1.6 // TabletMessage

    Item {
    id: tabletArea

    property real rootX: 0 // myWindow.x
    property real rootY: 0 // myWindow.y
    
    signal messageReceived( TabletMessage msg )
    
    // private
    
    function handleTabletMessage( msg ) {
        var xx = msg.globalPos().x - rootX
        var yy = msg.globalPos().y - rootY
        var pos = mapFromItem( null, xx, yy )
        msg.setPos( Qt.point( pos.x, pos.y ) )
        messageReceived( msg )
    }
    
    Component.onCompleted: tabletEventFilter.messageReceived.connect( handleTabletMessage )
    

    }
    @

    The component needs to know the x and y screen coordinates of the window that it's a child of. Currently I supply it with this info by setting the properties rootX and rootY from its parent item. That's pretty ugly.

    Is there a QtQuick component that can give me the requested coordinates directly? Something like QtQuick.Window's "Screen" item but for the current window?

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      I found a solution. QQuickItem has access to its parent window via:

      @
      QQuickWindow * QQuickItem::​window() const
      @

      So I've written a mini class, that's derived from QQuickItem and gives QML code access to the window's screen coordinates:

      @
      #ifndef WINDOWINFOITEM_H
      #define WINDOWINFOITEM_H

      #include <QQuickItem>
      #include <QQuickWindow>

      class WindowInfoItem : public QQuickItem
      {
      Q_OBJECT
      public:
      WindowInfoItem();
      ~WindowInfoItem();

      Q_INVOKABLE int globalX() const { return window()->x(); }
      Q_INVOKABLE int globalY() const { return window()->y(); }
      

      };

      #endif // WINDOWINFOITEM_H
      @

      The component from my first post now looks like this:

      @
      import QtQuick 2.4
      import domain.program 1.6 // TabletMessage, WindowInfoItem

      WindowInfoItem {
      id: tabletArea

      signal messageReceived( TabletMessage msg )
      
      // private
      
      function handleTabletMessage( msg ) {
          var xx = msg.globalPos().x - globalX()
          var yy = msg.globalPos().y - globalY()
          var pos = mapFromItem( null, xx, yy )
          msg.setPos( Qt.point( pos.x, pos.y ) )
          messageReceived( msg )
      }
      
      Component.onCompleted: tabletEventFilter.messageReceived.connect( handleTabletMessage )
      

      }
      @

      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