如何修复“ QWizardPage中的错误布局”

时间:2019-05-12 11:09:26

标签: python pyqt5

我正在构建QWizard。 QWizardPage中有几个小部件。因此,我将所有小部件都放在了滚动区域中。但是它破坏了QWizardPage的布局。

from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5.QtCore import pyqtProperty
from PyQt5 import QtCore, QtWidgets

import simplewizard_rc

class SimpleWizard(QtWidgets.QWizard):
    def __init__(self, parent=None):
        super(SimpleWizard, self).__init__(parent)
        self.addPage(Page1(self))
        self.addPage(Page2(self))
        self.setWizardStyle(QtWidgets.QWizard.ModernStyle)
        self.setPixmap(QtWidgets.QWizard.LogoPixmap, QtGui.QPixmap(":/image/logo.png"))
        self.setWindowTitle("PyQt5 Wizard Example")

class Page1(QtWidgets.QWizardPage):
    def __init__(self, parent=None):
        super(Page1, self).__init__(parent)
        self.setTitle(self.tr("Hello"))
        self.setPixmap(QtWidgets.QWizard.WatermarkPixmap, QtGui.QPixmap(":/images/watermark.png"))

    def initializePage(self):
        scrolllayout = QtWidgets.QVBoxLayout()

        scrollwidget = QtWidgets.QWidget()
        scrollwidget.setLayout(scrolllayout)

        scroll = QtWidgets.QScrollArea()
        scroll.setWidgetResizable(True)
        scroll.setWidget(scrollwidget)

        for i in range(8):
            grouptext = QtWidgets.QTextEdit()
            scrolllayout.addWidget(grouptext)

        label1 = QtWidgets.QLabel("Label1")
        label2 = QtWidgets.QLabel("Label2")
        label3 = QtWidgets.QLabel("Label3")

        hLayout = QtWidgets.QVBoxLayout()
        hLayout.addWidget(label1)
        hLayout.addWidget(label2)
        hLayout.addWidget(label3)

        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(scroll)
        layout.addLayout(hLayout)
        self.setLayout(layout)


class Page2(QtWidgets.QWizardPage):
    def __init__(self, parent=None):
        super(Page2, self).__init__(parent)
        self.label1 = QtWidgets.QLabel()
        self.label2 = QtWidgets.QLabel()
        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(self.label1)
        layout.addWidget(self.label2)
        self.setLayout(layout)

    def initializePage(self):
        self.label1.setText("Example text")
        self.label2.setText("Example text")

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    wizard = SimpleWizard()
    wizard.show()
    sys.exit(app.exec_())

代码有效,但是小部件的高度比平常大。我的代码有什么问题? 如果QScrollArea控件仅包含一个QTextEdit控件,则整个布局都没有问题。 One QTextEdit in the QScrollArea 但是,如果QScrollArea包含三个QTextEdit控件,则图像下方将出现一些空白。 8 QTextEdit in the QScrollArea 我想要的效果是,当QScroolArea包含多个QTextEdits时,左侧图像下方将没有空白区域。

0 个答案:

没有答案