pyqt5 does not center the window

1

Good afternoon I'm trying to center my app with the following code:

 from PyQt5.QtWidgets import QMainWindow,QApplication,QDialog, QDesktopWidget
from PyQt5 import uic, QtCore, QtWidgets
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5 import uic
from PyQt5 import QtCore, Qt, QtGui
import sys


class Principal(QDialog):
    def __init__(self):
        QDialog.__init__(self)
        uic.loadUi("12.ui",self)

        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        self.setSizeGripEnabled(True)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground,True)
        self.frame.resize(2000,1000)
        self.frame_2.resize(self.frame.width()-1900,self.frame.height())
        self.center()

    def mousePressEvent(self,event):
        if event.button() == QtCore.Qt.LeftButton:
            self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()
            event.accept()

    def mouseMoveEvent(self,event):
        if event.buttons() == QtCore.Qt.LeftButton:
            self.move(event.globalPos() - self.dragPosition)
            event.accept()

    def center(self):
        qRect = self.frameGeometry()
        centerPoint = QDesktopWidget().availableGeometry().center()
        print(centerPoint)
        qRect.moveCenter(centerPoint)
        self.move(qRect.topLeft())

app = QApplication([])
p = Principal()
p.show()
p.resize(1000,600)
app.exec_()

but does not focus the app .. any ideas?

    
asked by Revsky01 01.07.2018 в 23:32
source

1 answer

0

The problem is simple, you are resizing the window after centering it, that causes the bottomRight point to move and you can not see that effect, the solution is to center after making all the changes:

from PyQt5 import uic, QtCore, QtWidgets


class Principal(QtWidgets.QDialog):
    def __init__(self):
        QtWidgets.QDialog.__init__(self)
        uic.loadUi("12.ui",self)

        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        self.setSizeGripEnabled(True)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground,True)
        self.frame.resize(2000,1000)
        self.frame_2.resize(self.frame.width()-1900,self.frame.height())
        self.resize(1000,600)
        self.center()

    def mousePressEvent(self,event):
        if event.button() == QtCore.Qt.LeftButton:
            self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()
            event.accept()

    def mouseMoveEvent(self,event):
        if event.buttons() == QtCore.Qt.LeftButton:
            self.move(event.globalPos() - self.dragPosition)
            event.accept()

    def center(self):
        qRect = self.frameGeometry()
        centerPoint = QtWidgets.QDesktopWidget().availableGeometry().center()
        print(centerPoint)
        qRect.moveCenter(centerPoint)
        self.move(qRect.topLeft())


if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    p = Principal()
    p.show()
    sys.exit(app.exec_())
    
answered by 02.07.2018 / 02:27
source