Jan-18-2021, 04:45 AM
(This post was last modified: Jan-18-2021, 04:45 AM by deanhystad.)
You should use layout managers instead of manually positioning the widget. Qt for C++ is so cloe to Python Qt that all help for C++ is applicable to Qt5 or PySide2/6
import sys import PySide2.QtCore as QtCore import PySide2.QtGui as QtGui import PySide2.QtWidgets as QtWidgets from PySide2.QtCore import Qt font = "Arial" stylesheet = 'background-color:black; color:antiquewhite;' class DateTimeDisplay(QtWidgets.QWidget): def __init__(self, *args, **kvargs): super().__init__(*args, **kvargs) self.time = QtWidgets.QLabel( '12:00 AM', font=QtGui.QFont(font, 60)) self.date = QtWidgets.QLabel( 'Wednesday, December 31, 2020', font=QtGui.QFont(font, 30)) layout = QtWidgets.QVBoxLayout(self) layout.addWidget(self.time) layout.addWidget(self.date) self.update() def update(self): """Call to update date and time display""" text = QtCore.QTime.currentTime().toString(Qt.DefaultLocaleShortDate) self.time.setText(text) text = QtCore.QDate.currentDate().toString(Qt.DefaultLocaleLongDate) self.date.setText(text) class Window(QtWidgets.QMainWindow): def __init__(self, *args, **kvargs): super().__init__(*args, **kvargs) content = QtWidgets.QWidget() datetime_display = DateTimeDisplay() layout = QtWidgets.QGridLayout(content) layout.addWidget(QtWidgets.QLabel('Date/Time'), 0, 0) layout.addWidget(datetime_display, 0, 1) layout.addWidget( QtWidgets.QLabel('Resize window and date time widget stays in upper right'), 1, 0, 1, 2) layout.setColumnStretch(0, 1) layout.setRowStretch(1, 1) self.setCentralWidget(content) update = QtCore.QTimer(self) update.timeout.connect(datetime_display.update) update.start(1000) App = QtWidgets.QApplication(sys.argv) window = Window() window.setStyleSheet(stylesheet) window.show() App.exit(App.exec_())