居中调整大小的 Qwizard 页面

Center resized Qwizard page

我有一个 Qwizard 有 2 页。

当向导打开时,它位于屏幕中间。

第一页比第二页小。

单击下一步并显示第二页时,它会将额外的项目添加到第一页的底部,但不会重新居中页面。

在较小的屏幕上,这意味着按钮在屏幕底部不可见,除非用户将 window 向上移动才能看到。

我尝试使用标准的 QT 小部件居中,但 Qwizard 似乎对此没有反应。

如何重新居中向导的第二页以便显示整个小部件?

import sys

from PyQt5.QtCore import Qt, QPoint, QIODevice, QDateTime, QSize, QObject, QProcess, pyqtSignal, QThread, QEvent, QTimer, QBasicTimer
from PyQt5.QtWidgets import  QStyle, QWidget, QMainWindow, QCompleter, QProgressBar, QFileDialog, QApplication, qApp, QLineEdit, QLabel, QComboBox, QWizard, QWizardPage, QPushButton, QVBoxLayout, QShortcut, QMessageBox, QDesktopWidget, QHBoxLayout
from PyQt5.QtGui import QPainter, QFont, QIcon, QPixmap, QPalette, QLinearGradient, QColor, QBrush, QCursor


class Wizard(QWizard):
    # Initilisation of the UI and Wizard
    def __init__(self, parent=None):
        super(Wizard, self).__init__(parent)


        self.addPage(EnterToken(self))           
        self.addPage(EnterCode(self))                

class EnterToken(QWizardPage):
    def __init__(self, parent=None):
        super(EnterToken, self).__init__(parent)

        # Spacer blank label
        self.spacer = QLabel()

        # Enter Token Widgets
        self.label1 = QLabel()
        self.enter_token_box = QLineEdit()

        # empty text box for button
        self.empty = QLineEdit()

        self.btn = QPushButton('Enter Token')

        # layout options
        layout = QVBoxLayout()

        layout.addWidget(self.spacer)

        layout.addWidget(self.label1)
        layout.addWidget(self.enter_token_box)
        layout.addWidget(self.btn)

        self.setLayout(layout)


    def _EnterToken(self):
        """ Method for processing user input after the button is pressed"""
        text = self.enter_token_box.text()


class EnterCode(QWizardPage):
    """ Sensor Code Entry """

    def __init__(self, parent=None):
        super(EnterCode, self).__init__(parent)

        # Spacer Label
        self.spacer = QLabel()

        self._five_digit = QLineEdit(self)
        self.code_combo = QComboBox(self)
        self.label1 = QLabel()

        self.lineedit1 = QLineEdit(self)
        self.lineedit2 =QLineEdit(self)
        self.lineedit3 = QLineEdit(self)
        self.lineedit4 = QLineEdit(self)
        self.lineedit5 = QLineEdit(self)
        self.lineedit6 = QLineEdit(self)
        self.lineedit7 = QLineEdit(self)
        self.lineedit8 = QLineEdit(self)
        self.lineedit9 = QLineEdit(self)
        self.lineedit10 = QLineEdit(self)


        self.code_combo_list = [
            'Years', 'Months', 'Weeks', 'Days', 'Hours', 'Years', 'Months', 'Weeks', 'Days', 'Hours']
        for x in self.code_combo_list:
            self.code_combo.addItem(x)

        # num of logs combo box
        self.enter_num_logs = QLineEdit(self)
        self.num_logs_combo = QComboBox(self)
        self.logs_label = QLabel()


        self.num_logs_combo_list = [
            'Years', 'Months', 'Weeks', 'Days', 'Hours', 'Years', 'Months', 'Weeks', 'Days', 'Hours']
        for x in self.num_logs_combo_list:
            self.num_logs_combo.addItem(x)

        # ~buttons
        self.btn = QPushButton('Download Data')

        layout = QVBoxLayout()

        layout.addWidget(self.spacer)
        layout.addWidget(self.label1)
        layout.addWidget(self.code_combo)
        layout.addWidget(self._five_digit)

        layout.addWidget(self.lineedit1)
        layout.addWidget(self.lineedit2)
        layout.addWidget(self.lineedit3)
        layout.addWidget(self.lineedit4)
        layout.addWidget(self.lineedit5)
        layout.addWidget(self.lineedit6)
        layout.addWidget(self.lineedit7)
        layout.addWidget(self.lineedit8)
        layout.addWidget(self.lineedit9)
        layout.addWidget(self.lineedit10)


        layout.addWidget(self.logs_label)
        layout.addWidget(self.num_logs_combo)
        layout.addWidget(self.enter_num_logs)

        layout.addWidget(self.btn)

        self.setLayout(layout)



if __name__ == '__main__':

如果你想使 window 居中,你必须在它可见时进行,在这种情况下,必须使用 currentIdChanged 信号,但是当插槽被调用时,即使是新的 window 不可见,所以我们稍后将使用 QTimer 更新它。

class Wizard(QWizard):
    def __init__(self, parent=None):
        super(Wizard, self).__init__(parent)
        self.addPage(EnterToken(self))           
        self.addPage(EnterCode(self))
        self.currentIdChanged.connect(self.onCurrentIdChanged)

    def onCurrentIdChanged(self):
        QTimer.singleShot(0, self.center_widget)

    def center_widget(self):
        self.window().setGeometry(
            QStyle.alignedRect(
                Qt.LeftToRight, 
                Qt.AlignCenter,
                self.window().size(), 
                QApplication.desktop().availableGeometry())
        )