放在QMainWindow的Central Widget中的QTabWidget伸展太多,覆盖了菜单栏

时间:2014-09-02 19:37:02

标签: python windows pyqt5

这就是QtabWidget被放入QMainWindow的Central Widget中,它伸展得太多,覆盖了菜单栏。至少我没有看到制作布局有什么问题,这很简单,但它不适合我。我知道导入很糟糕:)我正在使用最新的PyQt5 for Windows。有什么想法吗?

from PyQt5.Qt import *


class MainWindow(QMainWindow):

def __init__(self, parent=None):
    super(MainWindow, self).__init__(parent)
    self.setBaseSize(800, 600)
    self.controllerMonitors = []
    self.setWindowTitle("Window")
    sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
    sizePolicy.setHorizontalStretch(0)
    sizePolicy.setVerticalStretch(0)
    sizePolicy.setHeightForWidth(self.sizePolicy().hasHeightForWidth())
    self.setSizePolicy(sizePolicy)
    self.setMinimumSize(QSize(800, 450))

    #--------
    # Menu
    #--------
    self.menuBar = QMenuBar(self)
    self.menuBar.setGeometry(QRect(0, 0, 800, 26))

    self.menu = QMenu(self.menuBar)
    self.menu.setTitle("Файл")

    self.actionSettings = QAction(self)
    self.actionSettings.setText("Настройки")
    self.actionExit = QAction(self)
    self.actionExit.setText("Изход")

    self.menu.addAction(self.actionSettings)
    self.menu.addAction(self.actionExit)

    self.menuBar.addAction(self.menu.menuAction())
    self.menu.addAction(self.actionSettings)
    self.menu.addAction(self.actionExit)

    self.actionExit.triggered.connect(self.close)

    #---------
    # Toolbar
    #---------
    # self.mainToolBar = QToolBar(self)
    # self.mainToolBar.setObjectName("mainToolBar")
    # self.addToolBar(Qt.TopToolBarArea, self.mainToolBar)

    #----------------
    # Central widget
    #----------------
    centralWidget = QWidget(self)
    centralWidgetLayout = QVBoxLayout(centralWidget)
    centralWidget.setLayout(centralWidgetLayout)

    # sizePolicy1 = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
    # sizePolicy1.setHorizontalStretch(0)
    # sizePolicy1.setVerticalStretch(0)
    # sizePolicy1.setHeightForWidth(centralWidget.sizePolicy().hasHeightForWidth())
    # centralWidget.setSizePolicy(sizePolicy1)
    centralWidget.setContextMenuPolicy(Qt.NoContextMenu)

    tabContainer = QTabWidget(centralWidget)

    tab1 = QWidget(tabContainer)
    te = QTextEdit(tab1)
    tab1layout = QVBoxLayout(tab1)
    tab1.setLayout(tab1layout)
    tab1layout.addWidget(te)

    tabContainer.addTab(tab1, "Tab 1")
    tabContainer.setCurrentIndex(0)
    centralWidgetLayout.addWidget(tabContainer)

    self.setCentralWidget(centralWidget)

和main.py

import sys
from PyQt5.QtWidgets import QApplication


from MainWindow import MainWindow


def main(argv):
    app = QApplication(argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec())

if __name__=="__main__":
    main(sys.argv)

1 个答案:

答案 0 :(得分:0)

问题不在于tab-widget:它与菜单栏有关。

QMainWindow class已经有一个built-in method用于创建一个男士酒吧,所以您不需要自己创建一个。 (选项卡小部件覆盖示例中的菜单栏的原因是因为您没有将其添加到布局中)。

示例中菜单的设置代码可以简化为:

    #--------
    # Menu
    #--------

    self.menu = self.menuBar().addMenu("Файл")
    self.actionSettings = self.menu.addAction("Настройки")
    self.actionExit = self.menu.addAction("Изход")
    self.actionExit.triggered.connect(self.close)

    #---------