PyQt5/PyQt

Why can't SplashScreen display in the middle of the screen after adding a specific layout?

LK007CX opened this issue · 2 comments

Environment :

  • OS: Win 10
  • Python 3.8.3
  • PyQt5 5.9.2

I tried to modify the splash screen example to use a custom layout and add a label to display text.
But I found that after adding its own layout, qsplacscreen can't be displayed in the center of the screen.
What can be done to solve this problem?

I have pycharm on the left half of win10. The right border of Pycharm indicates the middle line of the screen. You can see that splash screen is not centered.

image
The source code is as follows.

#!/usr/bin/python3
# -*- coding: UTF-8 -*-
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *


class SplashScreen(QSplashScreen):

    def __init__(self, *args, **kwargs):
        super(SplashScreen, self).__init__(*args, **kwargs)
        self.logoLabel = QLabel(self, objectName="logoLabel")
        self.initUI()

    def initUI(self):
        self.logoLabel.setText("测试文字测试文字测试文字测试文字")
        layout = QVBoxLayout()
        layout.addWidget(self.logoLabel)
        self.setLayout(layout)

    def finish(self, widget):
        super(SplashScreen, self).finish(widget)


if __name__ == '__main__':
    import sys
    import cgitb

    cgitb.enable(1, None, 5, '')
    app = QApplication(sys.argv)
    splash = SplashScreen()
    splash.show()

    def create_window():
        app.w = QWidget()
        QTimer.singleShot(6000, lambda: (
                                         app.w.show(),
                                         splash.finish(app.w)))
    create_window()
    sys.exit(app.exec_())

Looking forward to your reply!

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Created on 2020/12/17
@author: Irony
@site: https://pyqt.site https://github.com/PyQt5
@email: 892768447@qq.com
@file: test
@description: 
"""

__Author__ = 'Irony'
__Copyright__ = 'Copyright (c) 2020'
__Version__ = 'Version 1.0'

# !/usr/bin/python3
# -*- coding: UTF-8 -*-
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *


class SplashScreen(QSplashScreen):

    def __init__(self, *args, **kwargs):
        super(SplashScreen, self).__init__(*args, **kwargs)
        self.logoLabel = QLabel(self, objectName="logoLabel")
        self.initUI()

    def initUI(self):
        self.logoLabel.setText("测试文字测试文字测试文字测试文字")
        layout = QVBoxLayout(self)
        layout.addWidget(self.logoLabel)

    def finish(self, widget):
        super(SplashScreen, self).finish(widget)

    def mousePressEvent(self, event):
        event.ignore()


if __name__ == '__main__':
    import sys
    import cgitb

    cgitb.enable(1, None, 5, '')
    app = QApplication(sys.argv)
    splash = SplashScreen()
    splash.show()
    splash.raise_()


    def create_window():
        app.w = QWidget()
        QTimer.singleShot(6000, lambda: (
            app.w.show(),
            splash.finish(app.w)))


    create_window()
    sys.exit(app.exec_())