PDA

View Full Version : [PyQt] QTabWidget resize problem



Gad82
6th February 2013, 12:15
Hi everyone,

I'm trying to develop a custom QtabWidget which can auto-hide by clicking on a button placed in the widget tab. Here is a sample code:



from PyQt4.QtCore import *
from PyQt4.QtGui import *

class HidingTabWidget(QTabWidget):
def __init__(self, parent):
super(QTabWidget, self).__init__(parent)
self.show_tab = True
self.max_height = 1000000

def OnHideShow(self, event):
self.show_tab = not self.show_tab
if self.show_tab:
self.setFixedHeight(self.h)
self.setMinimumHeight(2*self.tb_h)
self.setMaximumHeight(self.max_height)
else:
self.h = self.height()
self.setFixedHeight(self.tb_h)
print self.height()

def AddTab(self, widget, label):
self.addTab(widget, label)
index = self.indexOf(widget)
self.tabBar().setTabText(index, label)
button = QPushButton(self)
button.setMaximumSize(16,16)
self.tabBar().setTabButton(index, QTabBar.RightSide, button)
button.clicked.connect(self.OnHideShow)
self.tb_h = self.tabBar().height()-7
self.setMinimumHeight(2*self.tb_h)
self.setMaximumHeight(self.max_height)


class Panel(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self)

l = QVBoxLayout(self)
splitter = QSplitter(self)
splitter.setOrientation(Qt.Vertical)
frame1 = QFrame(splitter)
frame1.setMinimumHeight(50)
tab_table = HidingTabWidget(splitter)

l.addWidget(splitter)
tab_table.AddTab(QTableView(), "Table")

self.setLayout(l)
self.resize(600,400)

if __name__ == "__main__" :
import sys, os

a = QApplication(sys.argv)
dia = Panel()
dia.show()
a.exec_()


If you click on the tab button, the widget collapses as I expect and its size cannot be changed. If you then re-click it, the widget original size is restored. The problem arises if I try to resize the widget when it is collapsed. If you try that and then re-click the tab button, the height of the widget is set to its minimum value, even if self.height() returns the correct height value. Is there anything I am missing?

Thanks in advance for your help!

norobro
6th February 2013, 18:43
You could save and restore the splitter state: QSplitter::restoreState()