中心调整大小的Qwizard页面

时间:2018-08-09 13:55:19

标签: python pyqt pyqt5 qwizard qwizardpage

我有一个Qwizard,共有2页。

打开向导后,它位于屏幕中间的中心。

第一页小于第二页。

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

在较小的屏幕上,这意味着按钮在屏幕的底部,并且除非用户向上移动窗口,否则看不到。

我尝试使用标准的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__':

1 个答案:

答案 0 :(得分:2)

如果要使窗口居中,则必须在可见时进行操作,在这种情况下,必须使用currentIdChanged信号,但是当调用插槽时,即使新窗口也不可见,因此我们将使用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())
        )