[solved] Retrieve window geometry


  • Moderators

    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?


  • Moderators

    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 )
    

    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.