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. Issues with Retrieving Coordinates in Frameless Qt Window
QtWS25 Last Chance

Issues with Retrieving Coordinates in Frameless Qt Window

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
windowcoordinateqml
1 Posts 1 Posters 159 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.
  • S Offline
    S Offline
    Saviz
    wrote on 12 Aug 2024, 04:35 last edited by
    #1

    I’m working on a project where I need to create a frameless window in Qt that acts as a selection area, allowing users to drag and resize it to specify a region. The goal is to obtain the x, y, width, and height properties of this selection area.

    Here's the code snippet I’m using to create the frameless window and make it draggable and resizable:

    Window {
        id: root
    
        signal selected(int x, int y, int width, int height)
        signal canceled()
    
        width: 300
        height: 400
    
        flags: Qt.Window | Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint
    
        modality: Qt.ApplicationModal
        opacity: 0.5
    
        DragHandler {
            id: dragHandler_1
    
            onActiveChanged: {
                if(active) {
                    startSystemMove()
                }
            }
        }
    
        MouseArea {
            id: mouseArea_1
    
            property int edges: 0
            property int edgeOffest: 5
    
            anchors.fill: parent
    
            hoverEnabled: true
            acceptedButtons: Qt.LeftButton
    
    
            function setEdges(x, y) {
                edges = 0;
    
                if(x < edgeOffest) {
                    edges |= Qt.LeftEdge
                }
    
                if(x > (width - edgeOffest)) {
                    edges |= Qt.RightEdge
                }
    
                if(y < edgeOffest) {
                    edges |= Qt.TopEdge;
                }
    
                if(y > (height - edgeOffest)) {
                    edges |= Qt.BottomEdge;
                }
            }
    
            cursorShape: {
                return !mouseArea_1.containsMouse ? Qt.ArrowCursor:
                       edges == 3 || edges == 12 ? Qt.SizeFDiagCursor :
                       edges == 5 || edges == 10 ? Qt.SizeBDiagCursor :
                       edges & 9 ? Qt.SizeVerCursor :
                       edges & 6 ? Qt.SizeHorCursor : Qt.ArrowCursor;
            }
    
            onPositionChanged: {
                setEdges(mouseX, mouseY)
            }
    
            onPressed: {
                setEdges(mouseX, mouseY)
    
                if(edges && containsMouse) {
                    startSystemResize(edges)
                }
            }
        }
    
        Component.onCompleted: {
            var screenWidth = Screen.availableWidth
            var screenHeight = Screen.availableHeight
    
            root.x = (screenWidth - root.width) / 2
            root.y = (screenHeight - root.height) / 2
    
            show()
        }
    
    
        RowLayout {
            id: rowLayout_1
    
            anchors.fill: parent
            spacing: 7
    
            Button {
                id: button_1
    
                Layout.preferredWidth: 120
                Layout.preferredHeight: 35
    
                text: qsTr("Select")
    
                onClicked: {
                    console.log(root.x)
                    console.log(root.y)
    
                    // Figure out why the x and y are not correct.
                    root.selected(root.x, root.y, root.width, root.height)
                    root.destroy(0) // Destroy window since it won't be needed anymore.
                }
            }
    
            Button {
                id: button_2
    
                Layout.preferredWidth: 120
                Layout.preferredHeight: 35
    
                text: qsTr("Cancel")
    
                onClicked: {
                    root.canceled() // Just in case we need it later.
                    root.destroy(0) // Destroy window since it won't be needed anymore.
                }
            }
        }
    }
    

    Below is the relevant portion of my main.qml file that creates this component:

    Button {
            text: qsTr("Open selection area")
            
            onClicked: {
                    showMinimized(); // Minimize the ApplicationWindow to enable user to select area
                    
                    var component = Qt.createComponent("./../components_custom/SelectionArea.qml");
                    var selectionArea = component.createObject(root); // root >> ApplicationWindow
                    
                    selectionArea.selected.connect(onSelected)
            }
    }
    

    I'm encountering an issue where, despite moving the window around, I always get x and y as 0 when I press the select button and trigger the onClick() event. I have tried various approaches to resolve this issue, but nothing seems to work. I'm running Qt 6.7.2 on KDE Linux version 6.1.3 with Wayland, so I’m wondering if Wayland might be affecting the behavior.

    Any help would be greatly appreciated!

    1 Reply Last reply
    0

    1/1

    12 Aug 2024, 04:35

    • Login

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