我找不到关闭请求关闭的标签的方法。我只找到了如何关闭当前窗口小部件和当前选项卡。有任何想法吗?我无法在互联网上找到线索。
self.tabs.tabCloseRequested.connect(self.removeTab)
def removeTab(self):
tab = self.tabs.currentWidget()
tab.deleteLater()
答案 0 :(得分:3)
QTabWidget
类有一个removeTab方法,它会删除一个标签,但不会删除其内容小部件。因此,您还需要明确删除内容窗口小部件:
self.tabs.tabCloseRequested.connect(self.removeTab)
...
def removeTab(self, index):
widget = self.tabs.widget(index)
if widget is not None:
widget.deleteLater()
self.tabs.removeTab(index)
答案 1 :(得分:0)
我可以发给你代码,但有点困惑。
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import codecs
import os
class Central_widget(QWidget):
def __init__(self, setted_lang):
super(Central_widget, self).__init__()
#tab list
self.tab_list = {}
self.cont_list = {}
self.label_list = {}
self.label_num = 0
self.tab_num = 0
#main page contenent
lesson = codecs.open('lessons\lekcja.html', 'r', encoding='utf-8' )
self.read = lesson.read()
read2 = self.read.encode('utf8')
print read2
#General widget
self.general = QWidget(self)
self.general.resize(900, 700)
#leftWidget
self.leftWidget=QWidget(self.general)
self.leftWidget.resize(160 ,657)
#tab master
self.tabmaster(self.general)
def tabmaster(self, general):
#left widget
self.scrollarea()
#right widget
self.tabs = QTabWidget(self.general)
self.tabs.setTabsClosable(True)
self.tabs.setTabEnabled(self.tab_num, True)
self.tabs.resize(710, 640)
self.tabs.move(170, 9)
#actual open tabs
self.actual_numtabs = self.tabs.indexOf(self.tabs.currentWidget())
#homepage tab
self.new_tab('hey')
def new_tab(self, text):
#tab_name - tab_value dictionary
if self.tab_num == 0:
name_new_tab = 'Homepage'
else: name_new_tab = 'Karta '+str(self.tab_num)
value_new_tab = QWidget()
self.tab_list[name_new_tab] = value_new_tab
#tab_content and cont_value
tab_content = 'cont '+str(self.tab_num)
cont_value = QTextBrowser(self.tab_list[name_new_tab])
self.cont_list[tab_content] = cont_value
#set size, text, add widget to tab
self.tabs.addTab(self.tab_list[name_new_tab], str(name_new_tab))
self.cont_list[tab_content].setFixedSize(704, 614)
if self.tab_num == 0:
self.cont_list[tab_content].setText(self.read)
self.tabs.setCurrentWidget(self.tab_list[name_new_tab])
else: self.cont_list[tab_content].setText(text)
#close tab
self.tabs.tabCloseRequested.connect(self.removeTab)
self.tab_num += 1
def removeTab(self, index):
#remove tab from widget
widget = self.tabs.widget(index)
if widget is not None:
widget.deleteLater()
self.tabs.removeTab(index)
def scrollarea(self):
#scroll area
layoutleft = QVBoxLayout(self.leftWidget)
s=QScrollArea()
layoutleft.addWidget(s)
widleft = QWidget(self)
vbox=QVBoxLayout(widleft)
nl=QHBoxLayout()
nl.addWidget(QLabel(u"Lista lekcji dostępnych:", self))
vbox.addLayout(nl)
nl=QHBoxLayout()
vbox.addLayout(nl)
vbox.setSpacing(2)
vbox.insertSpacing(1, 20)
#list of lessons
for filename in os.listdir('lessons'):
name = os.path.splitext(filename)
label_var = name[0]
value = QPushButton(label_var)
self.label_list[label_var] = value
self.label_list[label_var].clicked.connect(self.openlesson)
self.label_list[label_var].setObjectName(label_var)
_l=QHBoxLayout()
_l.addWidget(self.label_list[label_var])
vbox.addLayout(_l)
s.setWidget(widleft)
def openlesson(self):
a = self.sender()
print a.objectName()
lesson = codecs.open('lessons\\'+str(a.objectName())+'.html', 'r', encoding='utf-8' )
read = lesson.read()
self.new_tab(read)