Issues with Retrieving Coordinates in Frameless Qt Window
-
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!