I currently have a QTabWidget and some layout. Now I want all the tabs of my QtabWidget to occupy the width of the QtabWidget so I have something like this. The following method expands the two tabs in my Qtabwidget so they are the same size as the QtabWidget
void Contacts::AdjustTabs( int pos, int index )
{
std::string orig_sheet = ui.tabWidget->styleSheet().toStdString();
QString new_style = QString("QTabBar::tab { width: %1px; } ").arg((ui.tabWidget->size().width() /ui.tabWidget->count()));
std::string t = new_style.toStdString();
orig_sheet = orig_sheet + t;
ui.tabWidget->setStyleSheet(orig_sheet.c_str());
}
The above method works fine. Now the problem starts when the QTabwidget is set in a Qsplitter layout along with some other layout.At startup construction the above method does not seem to work for instance I have something like this
Someclass::SomeConstructor()
{
ui.setupUi(this);
.........
QObject::connect(ui.splitter,SIGNAL(splitterMoved(int,int)),this,SLOT(AdjustTabs(int,int)));
AdjustTabs(0,0);
}
Now when the form appears the horizontal size of the tabs remain unchanged (they do not expand - which is wrong). However when i move the splitter the tabs expand and everything is fine. My question is how can I make my tabs expand during form load ? Why arent they expanding ?
I think trying to explicitly adjust the tabs' sizes whenever a resize event occurs isn't the best approach. If things are set up properly, the appropriate size adjustments should naturally occur due to the default behavior of the QSplitter and QTabWidget's internal layout managers. Here is a very simple example of what I mean; note that there is no code in this program to explicitly handle resizing of anything, and yet all the tabs resize correctly as the splitter is moved and/or the window is resized:
So the question becomes -- why aren't your tabs resizing themselves automatically? Perhaps there you have their sizePolicy property set to Fixed, rather than Preferred or Ignored?