1

Environment setup:

Windows 11 WSL2 - Ubuntu 22.04 Python 3.11.2 PySide6 6.4.3 Single Monitor (2560x1440)

main_file.py:

from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine

app = QGuiApplication()
engine = QQmlApplicationEngine()

engine.load("qmls/mainscreen.qml")

app.exec()

qmls/mainscreen.qml

import QtQuick
import QtQuick.Controls

Window {
    id: root
    visible: true
    width: 320
    height: 480

    Component.onCompleted: {
        var x_calc = (root.Screen.width / 2) - (root.width / 2)
        var y_calc = (root.Screen.height / 2) - (root.height / 2)

        root.x = x_calc
        root.y = y_calc
    }

    TextInput{
    width: 580
    height: 300
    horizontalAlignment: TextInput.AlignHCenter
    verticalAlignment: TextInput.AlignVCenter
    }
}

I tried every piece of code here and it isn't working, every time i run the app the screen is in a different position.

lmalmeida
  • 135
  • 2
  • 14
  • for what you need `Screen.virtual*`? Do you use some virtual desktop? Try to log every value you use. Try to use [Screen](https://doc.qt.io/qt-6/qml-qtquick-screen.html) item instead. In fact, there is no reason why the screen size has changed from time to time. – folibis Apr 02 '23 at 12:19
  • I followed your suggestion and edited the main post to code look simpler and easier to debug. printing `x_calc` returns me `1120`, `y_calc` returns me `480`. printing `x.root` returns me `0`, `y.root` returns me `0` – lmalmeida Apr 02 '23 at 12:40
  • I'm just closing and running again: Look how weird: https://imgur.com/AElV2xN – lmalmeida Apr 02 '23 at 12:44
  • Looks very strange.I tested on Ubuntu 18.04 and the window always displayed in the same place in the screen center. Maybe try to use hardcoded values for x,y just to check that works. – folibis Apr 02 '23 at 13:12
  • I tried hardcoded values and print keeps me showing that the values are `0` – lmalmeida Apr 02 '23 at 14:03
  • 1
    @lmalmeida I was able to reproduce what you said with Windows 11 / WSL. Setting 'x' and 'y' appears to do nothing. Reading 'x' and 'y' also does not reflect the changing positions of the window should the user drag it elsewhere. The window is placed in a random place every time the application is run. This could be a "feature" of WSL. – Stephen Quan Apr 03 '23 at 00:21
  • To do that, you need to setup a full X11 desktop environment by installing an X11 server such as VcXsrv. But I recommend to setup a full Linux VM instead of struggling with WSL. – relent95 Apr 03 '23 at 00:49

0 Answers0