2

我有一个Qwizard2 页。

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

第一页小于第二页。

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

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

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

1 回答 1

3

如果要使窗口居中,则必须在可见时进行,在这种情况下currentIdChanged必须使用信号,但是当调用插槽时,即使新窗口也不可见,因此我们稍后将使用 aQTimer更新它。

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())
        )
于 2018-08-09T14:30:14.310 回答