I have divided my central QWidget into two sides: Left and Right. The left side contains the logo, and the right side widget contains the sign-in box. Because I want the sign-in box to be a fixed size and a certain distance from the right-side margin, I've included a QSpacerItem to achieve the desired behavior. However, after adding the QSpacer to the right frame, my logo gets pushed off-center. To counterbalance that, I added a QSpacer to the left-side frame too. When I run the code, everything works as expected, but when I expand the screen, the logo gets pushed to the right. I've added a second QSpacer to the right of the logo to sandwich it in, but I still can't get the logo centered
When the spacer is only added to the right_side_frame:

GUI after sandwiching the logo between two spacers:

#Login Windowfrom PyQt6.QtWidgets import *from PyQt6.QtGui import QPixmapfrom PyQt6.QtCore import Qtfrom ColorWidget import Colorclass MainWindow(QMainWindow): def __init__(self): super().__init__() # Window Properties self.setWindowTitle('login window') self.setMinimumSize(1050, 650) #Main Layouts main_layout = QHBoxLayout() left_side_frame = QWidget() right_side_frame = QWidget() main_layout.addWidget(left_side_frame) main_layout.addWidget(right_side_frame) #Left Layout left_layout = QHBoxLayout() left_h_spacer = QSpacerItem(200, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum) left_layout.addItem(left_h_spacer) #label logo_label = QLabel('logo') logo_label.setAlignment(Qt.AlignmentFlag.AlignCenter) left_layout.addWidget(logo_label) left_side_frame.setLayout(left_layout) right_side_spacer = QSpacerItem(200, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum) left_layout.addItem(right_side_spacer) #Right Layout right_layout = QHBoxLayout() #Sign-in Box signin_box = Color('white') signin_box.setObjectName('signin_box') signin_box.setFixedSize(450,550) Spacer = QSpacerItem(200, 20, QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Expanding) right_layout.addItem(Spacer) right_layout.addWidget(signin_box) right_side_frame.setLayout(right_layout) right_side_frame.setContentsMargins(0, 50, 80, 50) # Set main widget canvas = QWidget() canvas.setObjectName('canvas') canvas.setLayout(main_layout) self.setCentralWidget(canvas)if __name__ == '__main__': app = QApplication([]) with open('style.css', 'r') as f: app.setStyleSheet(f.read()) window = MainWindow() window.show() app.exec()stylesheet:
#canvas { background-image: url('Images/test2.jpg'); background-position: center;}#sign_in_label { color: black; font-size: 24px;}#email_label, #password_label, #register_label { color: grey; font-size: 12px;}I somewhat get the logo centered by using this values:
left_h_spacer = QSpacerItem(500, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum) left_layout.addItem(left_h_spacer)right_side_spacer = QSpacerItem(300, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum) left_layout.addItem(right_side_spacer)my screen size is 1512, 982