根据选项卡内容调整QTabWidget大小时出现问题(PyQT)

2022-05-05 00:00:00 python pyqt pyside qt

问题描述

我知道有一些关于QTabWidget调整大小的答案(Link 1、Link 2和其他);但是,我无法让它在我的应用程序中正常工作。

其想法是创建一个根据QTabWidget大小调整大小的窗口。目前,我有两个页签,第一个的大小比另一个小。

当我按下第二个选项卡时,窗口大小会按预期增加,但如果我返回到第一个选项卡,窗口不会缩小。我尝试更改每个选项卡的QSizePolicy以及调整选项卡和窗口的大小,但没有更改结果。

我如何才能解决窗口不缩小的问题?代码如下:


import os
import os.path
import sys

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (
    QSizePolicy,
    QApplication,
    QLabel,
    QMainWindow,
    QTabWidget,
    QDesktopWidget,
    QRadioButton,
    QLineEdit,
    QVBoxLayout,
    QHBoxLayout,
    QGridLayout,
    QWidget,
)

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupMainUi()
    
    def setupMainUi(self):
        
        self.centralWidget = QWidget()
        self.setCentralWidget(self.centralWidget)
        main_window_layout = QVBoxLayout(self.centralWidget)
        self.centralWidget.setLayout(main_window_layout)

        self.tabs = QTabWidget()
        self.tabs.addTab(self.loadTab1(), "Tab1")
        self.tabs.addTab(self.loadTab2(), "Tab2")

        self.tabs.currentChanged.connect(self.updateSizes)
        self.updateSizes(0)
        
        main_window_layout.addWidget(self.tabs)

    def updateSizes(self, index):
            
        for i in range(self.tabs.count()):
            if i != index:
                self.tabs.widget(i).setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
        
        self.tabs.widget(index).setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
        self.tabs.widget(index).resize(self.tabs.widget(index).minimumSizeHint())
        self.tabs.widget(index).adjustSize()
        self.resize(self.minimumSize())
        self.adjustSize()
   
    def loadTab1(self):
        
        self.ticketTab = QWidget()
        self.ticketTab.setEnabled(False)

        date_layout = QGridLayout()
        self.ticket = QLabel("Number:", self)
        self.ticket_number = QLineEdit()
        self.ticket_number.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        date_layout.addWidget(self.ticket, 0, 0)
        date_layout.addWidget(self.ticket_number, 0, 1)
        
        self.ticketTab.setLayout(date_layout)
        
        return self.ticketTab 
    
    def loadTab2(self):
        
        self.familyTab = QWidget()
        self.familyTab.setEnabled(False)
        
        family_default_layout = QVBoxLayout()
        self.family_default_checkbox = QRadioButton("Default")
        self.family_default_checkbox.setChecked(True)
        self.family_default_checkbox.setLayoutDirection(Qt.RightToLeft) 
        family_default_layout.addWidget(self.family_default_checkbox, alignment = Qt.AlignRight)
        
        family_layout = QHBoxLayout() 
        self.family_label = QLabel("Number of Families:", self)
        self.family_number = QLineEdit()
        self.family_number.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.family_number.textChanged.connect(lambda:self.checkInput(self.family_number))
        family_layout.addWidget(self.family_label, alignment=Qt.AlignLeft | Qt.AlignVCenter)
        family_layout.addWidget(self.family_number, alignment=Qt.AlignLeft | Qt.AlignVCenter)
        family_default_layout.addLayout(family_layout)
        
        temp_family_layout = QHBoxLayout() 
        self.family_label_1 = QLabel("Number of Families:", self)
        self.family_number_1 = QLineEdit()
        self.family_number_1.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        temp_family_layout.addWidget(self.family_label_1, alignment=Qt.AlignLeft | Qt.AlignVCenter)
        temp_family_layout.addWidget(self.family_number_1, alignment=Qt.AlignLeft | Qt.AlignVCenter)
        family_default_layout.addLayout(temp_family_layout)
        
        min_sub_family_layout = QHBoxLayout() 
        self.min_subfamily_label = QLabel("Minimum Number of SubFamilies:", self)
        self.min_subfamily_number = QLineEdit()
        self.min_subfamily_number.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        min_sub_family_layout.addWidget(self.min_subfamily_label, alignment=Qt.AlignLeft | Qt.AlignVCenter)
        min_sub_family_layout.addWidget(self.min_subfamily_number, alignment=Qt.AlignLeft | Qt.AlignVCenter)
        family_default_layout.addLayout(min_sub_family_layout)
        
        max_sub_family_layout = QHBoxLayout() 
        self.max_subfamily_label = QLabel("Maximum Number of SubFamilies:", self)
        self.max_subfamily_number = QLineEdit()
        self.max_subfamily_number.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        max_sub_family_layout.addWidget(self.max_subfamily_label, alignment=Qt.AlignLeft | Qt.AlignVCenter)
        max_sub_family_layout.addWidget(self.max_subfamily_number, alignment=Qt.AlignLeft | Qt.AlignVCenter)
        family_default_layout.addLayout(max_sub_family_layout)
        
        self.familyTab.setLayout(family_default_layout)
        
        return self.familyTab
    
os.chdir("../..")

app = QApplication(sys.argv)
win = MainWindow()

qr = win.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
win.move(qr.topLeft()) 
win.move(win.pos().x(), win.pos().y() - 180)

win.show()
sys.exit(app.exec())

很抱歉问了这个简单的问题。就在最近,我还在探索PyQT。


解决方案

QTabWidget基于始终使用所有页面中最大尺寸提示的QStackedWidget。

为了避免这种情况,您可以覆盖sizeHintminimumSizeHint,并确保它只返回当前小工具提示。

然后,由于调整大小需要一些内部事件处理,您可以通过调用processEvents()和然后调用adjustSize来确保应用程序执行此操作。

class ResizingTabWidget(QTabWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.currentChanged.connect(self.updateGeometry)

    def minimumSizeHint(self):
        return self.sizeHint()

    def sizeHint(self):
        current = self.currentWidget()
        if not current:
            return super().sizeHint()
        return current.sizeHint()


class MainWindow(QMainWindow):
    # ...
    def setupMainUi(self):
        # ...
        self.tabs = ResizingTabWidget()
        # ...

    def updateSizes(self):
        QApplication.processEvents()
        self.adjustSize()
请注意,您不需要在__init__中调用updateSizes
此外,请注意,从用户体验的角度来看,这不是一个非常好的方法,因为许多人认为不断调整自身大小的用户界面令人讨厌,并且经常会让用户感到困惑。

相关文章