如何向 QTabWidget 添加新标签 [英] How to add a new tab to QTabWidget

查看:95
本文介绍了如何向 QTabWidget 添加新标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 QtTabWidget,上面挂着 1 个(或 2 个)小部件(我的主页小部件),例如:

I have a QtTabWidget with 1 (or 2) widgets (my main page widget) hanging off of it like:

330     for i in range(1):
331         win = MainWindow()
332         tabs.addTab(win, QIcon('running.png'), "Test-%d" % i)
333     tabs.show()
334     print("tab count = %d" % tabs.count())

AFAIK,tabs"现在是win"的父级.我还有一个带有添加选项卡"和删除选项卡"的工具栏.删除/退出处理程序正在工作,就像

AFAIK, 'tabs' is now a parent to 'win'. I also have a tool bar with a "Add Tab" and "Remove Tab". The remove/quit handler is working and is like

254     # ----------------------- quitHandler() ------------------------
255     def quitHandler(self):
256         if ( self.parentWidget().count() == 1 ):
257             self.statusBar.setText('I am the last one, you can not kill me ....')
258             return
259         tab = self.parentWidget().currentWidget()
260         self.close()
261         self.parentWidget().removeWidget(tab)
262 

但我的添加标签"处理程序写为

But my "Add Tab" handler written as

263     # ----------------------- newTabHandler() ------------------------
264     def newTabHandler(self):
265         count = self.parentWidget().count()
266         if ( count > 10 ):
267             self.statusBar.setText('I only support 10 tabs ....')
268             return
269         win = MainWindow()
270         self.parentWidget().addWidget(win)
271         self.parentWidget().show()
272         print(self.parentWidget().count())

未呈现新选项卡.但我看到标签计数正在增加..这是日志

is not rendering the new tab. But I see that tab count is increasing .. here is the log

medi@medi:~/proto/python/d1> ./utg
tab count = 1
2
3
4
5

感谢帮助.

根据要求,这里是显示问题陈述的最少代码:

As requested, here is a minimal code to show the problem statement:

  1 #!/usr/bin/python3
  2 
  3 import sys
  4 import os
  5 
  6 from PyQt5 import (QtCore, QtWidgets, QtGui)
  7 from PyQt5.QtGui import (QIcon)
  8 from PyQt5.QtWidgets import (QMainWindow, QPushButton, QLabel, QLineEdit, QTextEdit)
  9 from PyQt5.QtWidgets import (QWidget, QHBoxLayout, QVBoxLayout, QGroupBox, QGridLayou    t)
 10 from PyQt5.QtWidgets import (QFormLayout, QSizePolicy, QAction, QToolBar)
 11 from PyQt5.QtCore import (QSize, QProcess)
 12 
 13 class MainWindow(QMainWindow):
 14     def __init__(self):
 15         super().__init__()
 16         self.setWindowTitle('tabs mgmnt test')
 17         self.setGeometry(50, 50, 600, 600)
 18 
 19         self.toolbar = QToolBar('My Main Tool Bar')
 20         self.addToolBar(self.toolbar)
 21         newTabAct = QAction('New Tab', self)
 22         self.toolbar.addAction(newTabAct)
 23         newTabAct.triggered.connect(self.newTabHandler)
 24         
 25    # ----------------------- newTabHandler() ------------------------
 26     def newTabHandler(self):
 27         print("before new tab, tab-count = %d" % self.parentWidget().count() )
 28         win = MainWindow()
 29         self.parentWidget().addWidget(win)
 30         self.parentWidget().show()
 31         print("after new tab, tab-count = %d" % self.parentWidget().count() )
 32 
 33 # ================================= main() ==========================
 34 if (__name__ == "__main__"):
 35     app = QtWidgets.QApplication(sys.argv)
 36     tabs = QtWidgets.QTabWidget()
 37     win = MainWindow()
 38     tabs.addTab(win, "Tab-1" )
 39     tabs.show()
 40     sys.exit ( app.exec_() )
 41 

正如所见,在 main() 和第 36 行中,我创建了一个 QTabWidget 和一个窗口,并将窗口挂到选项卡上.所以我相信,tabs"是父项,win"是子项.然后第 26 行的 'newTabHandler()' 应该向选项卡添加另一个选项卡.但是这个方法是 MainWindow 的一个实例方法,并没有看到 main() 本地的tabs".所以我通过parentWidget()遍历父子关系.另请注意,第 27 和 31 行的 print() 语句报告正确的制表符计数.但我没有看到呈现的新选项卡.也许我错过了针对某个对象的 show() .这是我在第 30 行所做的.但没有渲染新选项卡,我只看到一个选项卡,即在 main() 中创建的选项卡.

As seen, in main() and line 36 I create a QTabWidget and a window and hang the window to tabs.So I believe, 'tabs' is parent and 'win' is a child. Then the 'newTabHandler()' on line 26 should add another tab to tabs. But this method is an instance method of MainWindow and does not see 'tabs' which is local to main(). So I traverse the parent-child relation via parentWidget(). Also note that the print() statements on line 27 and 31 are reporting correct tab count. But I do not see the new tab rendered. Maybe I am missing a show() against some object. which I do on line 30. Yet no rendering of new tab and I only see one tab, the one created in main().

推荐答案

要理解问题,您必须在 newTabHandler 中打印 parentWidget():

To understand the problem you must print the parentWidget() in newTabHandler:

def newTabHandler(self):
    print(self.parentWidget())

并且您会注意到,每次尝试添加标签时都会得到:

and you will notice that every time you try to add a tab you get:

<PyQt5.QtWidgets.QStackedWidget object at 0x7f3e4e2680d0>
<PyQt5.QtWidgets.QStackedWidget object at 0x7f3e4e2680d0>
<PyQt5.QtWidgets.QStackedWidget object at 0x7f3e4e2680d0>
...

所以 parentWidget() 不是 QTabWidget 而是 QStackedWidget.

So parentWidget() is not the QTabWidget but a QStackedWidget.

说明:

QTabWidget 是 QTabBar + QStackedWidget,每次使用 addTab() 添加小部件时,小部件都会添加到 QStackedWidget 并在 QTabBar 中创建一个新选项卡,因此小部件的父级是 QStackedWidget.

A QTabWidget is a QTabBar + QStackedWidget, and every time you add a widget using addTab() the widget is added to the QStackedWidget and a new tab is created in the QTabBar, so accordingly the parent of the widget is the QStackedWidget.

解决方案:

考虑到上述情况,解决方案是使用 QStackedWidget 的 parentWidget() 也就是 QTabWidget,或者在您提供的代码中作为 QTabWidget 是窗口然后使用 window() 方法:

Considering the above, the solution is to use the parentWidget() of the QStackedWidget which is the QTabWidget, or in the code that you provide as the QTabWidget is the window then use the window() method:

def newTabHandler(self):
    tab_widget = self.parentWidget().parentWidget()
    #              QStackedLayout    QStackedWidget
    # or
    # tab_widget = self.window()
    print(tab_widget)
    count = tab_widget.count()
    win = MainWindow()
    tab_widget.addTab(win, "Tab-{}".format(count + 1))

这篇关于如何向 QTabWidget 添加新标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆