python 2.7 - Add plus button to TabWidget pyqt4 -
the following code gives tab interface can dynamically add tabs
import sys, random pyqt4 import qtcore, qtgui class tabcontainer(qtgui.qwidget): def __init__(self): super(tabcontainer, self).__init__() self.next_item_is_table = false self.initui() def initui(self): self.setgeometry( 150, 150, 650, 350) self.tabwidget = qtgui.qtabwidget(self) vbox = qtgui.qvboxlayout() vbox.addwidget(self.tabwidget) self.setlayout(vbox) self.pages = [] self.add_page() self.show() def create_page(self, *contents): page = qtgui.qwidget() vbox = qtgui.qvboxlayout() c in contents: vbox.addwidget(c) page.setlayout(vbox) return page def create_table(self): rows, columns = random.randint(2,5), random.randint(1,5) table = qtgui.qtablewidget( rows, columns ) r in xrange(rows): c in xrange(columns): table.setitem( r, c, qtgui.qtablewidgetitem( str( random.randint(0,10) ) ) ) return table def create_list(self): list = qtgui.qlistwidget() columns = random.randint(2,5) c in xrange(columns): qtgui.qlistwidgetitem( str( random.randint(0,10) ), list ) return list def create_new_page_button(self): btn = qtgui.qpushbutton('create new page!') btn.clicked.connect(self.add_page) return btn def add_page(self): if self.next_item_is_table: self.pages.append( self.create_page( self.create_table(), self.create_new_page_button() ) ) self.next_item_is_table = false else: self.pages.append( self.create_page( self.create_list(), self.create_new_page_button() ) ) self.next_item_is_table = true self.tabwidget.addtab( self.pages[-1] , 'page %s' % len(self.pages) ) self.tabwidget.setcurrentindex( len(self.pages)-1 ) app = qtgui.qapplication(sys.argv) ex = tabcontainer() sys.exit(app.exec_())
but want add new tab button next tab bar similar browser... searched wasn't able find way add plus button...is there property in tabwidget or need add new button on there..?
the source above code : dynamically change view of tabs in qtabwidget (pyqt)
you set corner-widget on tab-bar:
self.tabbutton = qtgui.qtoolbutton(self) self.tabbutton.settext('+') font = self.tabbutton.font() font.setbold(true) self.tabbutton.setfont(font) self.tabwidget.setcornerwidget(self.tabbutton) self.tabbutton.clicked.connect(self.add_page)
Comments
Post a Comment