Move QMessageBox to bottom-right corner of the screen
-
I want to move QMessageBox to bottom-right corner of the screen, following is my code,but doesn't work as expected , anyone can explain why? How to fix the problem here ?
@
import sysfrom PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * def moveToBottomRight(msgBox): msgBox.addButton("重计", QMessageBox.ActionRole) msgBox.addButton("停计", QMessageBox.ActionRole) msgBox.addButton('继续', QMessageBox.ActionRole) screenGeometry = QApplication.desktop().availableGeometry() screenGeo = screenGeometry.bottomRight() msgGeo = msgBox.frameGeometry() msgGeo.moveBottomRight(screenGeo) msgBox.move(msgGeo.topLeft()) def main(): app = QApplication(sys.argv) msgBox = QMessageBox() moveToBottomRight(msgBox) msgBox.show() sys.exit(app.exec_()) if __name__ == '__main__': main()
@
when putting moveToBottomRight(msgBox) after msgBox.show(), it works as I want ,but msgBox show in the origin position at first ,then immediately move to the destination position ,how to let it show at the destination position directly without firstly showing in the origin position ?
-
Which system you are on?
Try to use an absolute position instead and see if it works.
As far as I understand geometry of the widget is unknown until it is shown.
So your code works even the result you get is unexpected.from QT documentation:
X11 Peculiarities
On X11, a window does not have a frame until the window manager decorates it. This happens asynchronously at some point in time after calling QWidget::show() and the first paint event the window receives, or it does not happen at all. Bear in mind that X11 is policy-free (others call it flexible). Thus you cannot make any safe assumption about the decoration frame your window will get. Basic rule: There's always one user who uses a window manager that breaks your assumption, and who will complain to you.
Furthermore, a toolkit cannot simply place windows on the screen. All Qt can do is to send certain hints to the window manager. The window manager, a separate process, may either obey, ignore or misunderstand them. Due to the partially unclear Inter-Client Communication Conventions Manual (ICCCM), window placement is handled quite differently in existing window managers.
X11 provides no standard or easy way to get the frame geometry once the window is decorated. Qt solves this problem with nifty heuristics and clever code that works on a wide range of window managers that exist today. Don't be surprised if you find one where QWidget::frameGeometry() returns wrong results though.