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

Problem resizing QTabWidget according to tab content (PyQT)(根据选项卡内容调整QTabWidget大小时出现问题(PyQT))
本文介绍了根据选项卡内容调整QTabWidget大小时出现问题(PyQT)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有一些关于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
此外,请注意,从用户体验的角度来看,这不是一个非常好的方法,因为许多人认为不断调整自身大小的用户界面令人讨厌,并且经常会让用户感到困惑。

这篇关于根据选项卡内容调整QTabWidget大小时出现问题(PyQT)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)