0

我有一个垂直拆分器,顶部有一个 QTabWidget,下面有一个 QPlainTextEdit 小部件(用作日志记录窗口)。在实际应用程序中,选项卡充满了 QWidget,包含一个 matplotlib 画布和一个带有一些控制元素的 QFrame:

    QSplitter
      QPlainTextEdit
      QVBoxLayout
        QTabWidget
          QWidget
            QVBoxLayout
              FigureCanvas (QSizePolicy.Expanding, QSizePolicy.Expanding)
              QFrame (optional)

我希望应用程序在选项卡和日志记录窗口之间以 4:1 的良好垂直比例开始。但是,mysplitter.setStretchFactor(4,1)在这里使用不起作用,因为sizeHint()QTabWidget 的 仅为 (4,4),导致 QPlainTextEditsizeHint() = (256,192)吞噬了几乎所有可用的垂直空间。作为一种解决方法,我目前正在为 QPlainTextWidget 设置一个固定的高度,但我知道这个小部件不是罪魁祸首。

我想我需要摆弄 sizePolicies 或各个选项卡的布局/大小,但到目前为止我还没有成功。我附上了一个 MWE,完整的代码可在https://github.com/chipmuenk/pyFDA/blob/master/pyfda/pyfdax.py获得:

# -*- coding: utf-8 -*-
from __future__ import print_function
from PyQt5.QtWidgets import (QWidget, QTabWidget, QPlainTextEdit, QSplitter, 
                             QMainWindow, QVBoxLayout, QApplication)
from PyQt5.QtGui import QFontMetrics
from PyQt5 import QtCore

#------------------------------------------------------------------------------
class TabWidgets(QTabWidget):
    def __init__(self, parent):
        super(TabWidgets, self).__init__(parent)
        self.wdg1 = QWidget(self)
        self.wdg2 = QWidget(self)
        self._construct_UI()
#------------------------------------------------------------------------------
    def _construct_UI(self):
        """ Initialize UI with tabbed subplots """
        self.tabWidget = QTabWidget(self)
        self.tabWidget.addTab(self.wdg1, 'Wdg 1')
        self.tabWidget.addTab(self.wdg2, 'Wdg 2')

        layVMain = QVBoxLayout()
        layVMain.addWidget(self.tabWidget)
        self.setLayout(layVMain)
        # When user has switched the tab, call self.current_tab_redraw
        self.tabWidget.currentChanged.connect(self.current_tab_redraw)
#------------------------------------------------------------------------------

    def current_tab_redraw(self):
        pass
        #self.tabWidget.currentWidget().resize()

class MWin(QMainWindow):
    """
    Main window consisting of a tabbed widget and a status window.
    QMainWindow is used as it understands GUI elements like central widget
    """
    def __init__(self, parent=None):
        super(QMainWindow,self).__init__()
 #---------------------------------------------------------------       
        statusWin = QPlainTextEdit(self)  # status window
        tabWin    = TabWidgets(self) # tabbed window
        print('size status win: {0}'.format(statusWin.sizeHint()))
        print('size_tab win: {0}'.format(tabWin.sizeHint()))
        mSize = QFontMetrics(statusWin.font())
        rowHt = mSize.lineSpacing()
        # fixed height for statusWin needed as the sizeHint of tabWin is very small
        statusWin.setFixedHeight(4*rowHt+4)
        # add status window underneath plot Tab Widgets:
        spltVMain = QSplitter(QtCore.Qt.Vertical)
        spltVMain.addWidget(tabWin)
        spltVMain.addWidget(statusWin)
        # relative initial sizes of subwidgets, this doesn't work here
        spltVMain.setStretchFactor(4,1)        

        spltVMain.setFocus()
        # make spltVMain occupy the main area of QMainWindow and set inheritance
        self.setCentralWidget(spltVMain)   
#----------------------------------------------------------------------------
def main():
    import sys
    app = QApplication(sys.argv)

    mainw = MWin(None)
    mainw.resize(300,400)
    app.setActiveWindow(mainw) 
    mainw.show()

    sys.exit(app.exec_())

if __name__ == "__main__":
    main()
4

1 回答 1

1

我找到了一个简单的解决方法:将拆分器设置为绝对单位而不是比率就可以了。说明拆分器小部件的总高度,使解决方案适用于不同的分辨率等。下面的代码片段显示了更新的__init__()部分:

def __init__(self, parent=None):
    super(QMainWindow,self).__init__()
    #---------------------------------------------------------------       
    statusWin = QPlainTextEdit(self)  # status window
    tabWin    = TabWidgets(self) # tabbed window
    print('size status win: {0}'.format(statusWin.sizeHint()))
    print('size_tab win: {0}'.format(tabWin.sizeHint()))
    # fixed height for statusWin no longer needed here
    # mSize = QFontMetrics(statusWin.font())
    # rowHt = mSize.lineSpacing()
    # statusWin.setFixedHeight(4*rowHt+4)
    # add status window underneath plot Tab Widgets:
    spltVMain = QSplitter(QtCore.Qt.Vertical)
    spltVMain.addWidget(tabWin)
    spltVMain.addWidget(statusWin)
    # relative initial sizes of subwidgets, this doesn't work here
    # spltVMain.setStretchFactor(4,1)
    # Use absolute values instead:
    spltVMain.setSizes([spltVMain.size().height() * 0.8, 
                        spltVMain.size().height() * 0.2])

    spltVMain.setFocus()
    # make spltVMain occupy the main area of QMainWindow and set inheritance
    self.setCentralWidget(spltVMain)   
#-----------------------------------------------------------------------
于 2017-12-16T07:55:10.123 回答