Python Forum
[PyQt] self.pos().x() and self.pos().x() always return -3,-30 on PyQT5
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] self.pos().x() and self.pos().x() always return -3,-30 on PyQT5
#1
Hi,

I am using the code below to experiment with window size and psoition.
I notice that irrespective of where I move the window self.pos().x() and self.pos().x() always return -3,-30
Is this a bug?
I am running this on Ubuntu 22.04

import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget,
                             QVBoxLayout, QTextEdit, QPushButton)


class MainWindow(QMainWindow):

    def __init__(self):
        QMainWindow.__init__(self)
        self.resize(400, 200)

        centralWidget = QWidget()
        self.setCentralWidget(centralWidget)

        self.textEdit = QTextEdit()
        self.btn = QPushButton("get the screen position of `QMainWindow`")
        self.btn.clicked.connect(self.btnClicked)

        layoutV = QVBoxLayout(centralWidget)
        layoutV.addWidget(self.textEdit)
        layoutV.addWidget(self.btn)

        self.textEdit.append("Start:")
        self.textEdit.append("pos.x=`{}`, pos.y=`{}`"
                             "".format(self.pos().x(), self.pos().y()))
        self.textEdit.append("geometry.x=`{}`, geometry.y=`{}`"
                             "".format(self.geometry().x(), self.geometry().y()))
        self.textEdit.append("--------------------------------------")

    def btnClicked(self):
        self.textEdit.append("pos.x=`{}`, pos.y=`{}`"
                             "".format(self.pos().x(), self.pos().y()))
        self.textEdit.append("geometry.x=`{}`, geometry.y=`{}`"
                             "".format(self.geometry().x(), self.geometry().y()))

    def moveEvent(self, event):    # QMoveEvent
        print("x=`{}`, y=`{}`".format(event.pos().x(), event.pos().y()))
        super(MainWindow, self).moveEvent(event)

    def resizeEvent(self, event):  # QResizeEvent
        print("w=`{}`, h=`{}`".format(
            event.size().width(), event.size().height()))
        super(MainWindow, self).resizeEvent(event)


app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
Reply
#2
Works fine in LMDE5
PyQt5 5.15.7

Output:
Start: pos.x=`0`, pos.y=`0` geometry.x=`0`, geometry.y=`0` -------------------------------------- pos.x=`532`, pos.y=`248` geometry.x=`532`, geometry.y=`276` pos.x=`550`, pos.y=`251` geometry.x=`550`, geometry.y=`279` pos.x=`163`, pos.y=`401` geometry.x=`163`, geometry.y=`429` pos.x=`770`, pos.y=`185` geometry.x=`770`, geometry.y=`213`
Reply
#3
Moving the window does not appear to send a Move event. Changing window focus does send a Move event (or something that gets mapped to a QWidget.moveEvent. Resize events work as expected. My guess is this is an attribute of the ubuntu window manager. I am running the default desktop (Think it is GNOME. Shows up as ubuntu.desktop). You could install a different desktop and see if it fixes your problem.
Reply
#4
Thank you for your replies.
The problem only occurs when running on wayland, so I think the issue is at that level rather than the window manager level. if I switch to X11 then self.pos().x(), self.pos().y() return the expected positions.
I probably switched to wayland after upgrading from 20.04 to 22.04 and I do remember at that time beginning to see a lot of Wayland warnings.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Huge code problems (buttons(PyQt5),PyQt5 Threads, Windows etc) ZenWoR 0 2,823 Apr-06-2019, 11:15 PM
Last Post: ZenWoR

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020