PDA

View Full Version : Center TabBar horizontally on the TabWidget



forrestfsu
12th March 2007, 14:41
I'm trying to center a tabBar horizontally on a tabWidget.

Example:

instead of
| TAB 1 | TAB 2 | Tab 3 |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

I desire
XXXXXXXXXXXXXXXXXX| TAB 1 | TAB 2 | Tab 3 |XXXXXXXXXXXXXXXXXX

I should also state that I'm already using a certain style...so I can't simply switch to the Mac-style (which I believe by default aligns tab horizontally centered).

Any pointers are appreciated.

guilugi
12th March 2007, 17:01
Maybe you can tweak the TabBar position using a custom QStyle.
I know there are some elements you can reimplement in there :

http://doc.trolltech.com/4.2/qstyle.html#SubElement-enum

forrestfsu
12th March 2007, 20:27
Thanks for the tip. I'm trying to do something like this, but unfortunately it is not centering it:



int CenterTab::styleHint(StyleHint hint, const QStyleOption *option, const QWidget *widget, QStyleHintReturn *returnData) const
{
switch (hint)
{
case SH_TabBar_Alignment:
return int(Qt::AlignHCenter);
default:
return QWindowsStyle::styleHint(hint, option, widget, returnData);
}
}


Any pointers?

spud
12th March 2007, 23:40
If you are using a QTabWidget I would try out


void QTabWidget::setCornerWidget ( QWidget * widget, Qt::Corner corner = Qt::TopRightCorner )

If you set one expanding widget in each corner, that might center the tabs. If you are not using QTabWidget, you can still have a look at the source code for the above function and do it yourself.

forrestfsu
13th March 2007, 17:29
spud, i've been looking into your advice...but I can't seem to understand how to make an expandable widget...can you explain a bit more please?

forrestfsu
13th March 2007, 21:40
I'm trying to do something similar to what I demonstrate below...however the size hint is not correctly being shown:

Create a CenterTab class which will be used as the TopLeftCorner widget:


class CenterTab : public QWidget
{
QOBJECT
public:
CenterTab(int width, int height);
protected:
QSize sizeHint() const;
private:
int width;
int height;
}

CenterTab::CenterTab(int width, int height)
{
width = width;
height = height;
}

QSize CenterTab::sizeHint () const
{
return QSize(width, height);
}


Then within your QTabWidget constructor:


centerTab = new CenterTab(100, 25); // You can use any width and height you like
this->setCornerWidget(centerTab, Qt::TopLeftCorner);


Thanks for the help guys...

forrestfsu
14th March 2007, 13:33
Ha, great. I got it...the above works as long as you change:



width= width;
height=height;


to:


this->width= width;
this->height=height;


It was a silly mistake. Thanks for all the help!

fullmetalcoder
14th March 2007, 14:02
Your code won't center tabs unless there is only a fixed number. You'd better compute the size hint of your center widget according to the number of tabs (or better to their actual size)...

forrestfsu
14th March 2007, 14:26
thanks fullmetalcoder...I have already taken that into account in my code. I just gave an example of what I did above incase anyone else runs into the same problem.