Results 1 to 9 of 9

Thread: Center TabBar horizontally on the TabWidget

  1. #1
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Center TabBar horizontally on the TabWidget

    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.
    Last edited by forrestfsu; 12th March 2007 at 15:06.

  2. #2
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Center TabBar horizontally on the TabWidget

    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....ubElement-enum

  3. #3
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Center TabBar horizontally on the TabWidget

    Thanks for the tip. I'm trying to do something like this, but unfortunately it is not centering it:

    Qt Code:
    1. int CenterTab::styleHint(StyleHint hint, const QStyleOption *option, const QWidget *widget, QStyleHintReturn *returnData) const
    2. {
    3. switch (hint)
    4. {
    5. case SH_TabBar_Alignment:
    6. return int(Qt::AlignHCenter);
    7. default:
    8. return QWindowsStyle::styleHint(hint, option, widget, returnData);
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    Any pointers?

  4. #4
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Center TabBar horizontally on the TabWidget

    If you are using a QTabWidget I would try out
    Qt Code:
    1. void QTabWidget::setCornerWidget ( QWidget * widget, Qt::Corner corner = Qt::TopRightCorner )
    To copy to clipboard, switch view to plain text mode 
    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.

  5. #5
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Center TabBar horizontally on the TabWidget

    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?

  6. #6
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Center TabBar horizontally on the TabWidget

    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:
    Qt Code:
    1. class CenterTab : public QWidget
    2. {
    3. QOBJECT
    4. public:
    5. CenterTab(int width, int height);
    6. protected:
    7. QSize sizeHint() const;
    8. private:
    9. int width;
    10. int height;
    11. }
    12.  
    13. CenterTab::CenterTab(int width, int height)
    14. {
    15. width = width;
    16. height = height;
    17. }
    18.  
    19. QSize CenterTab::sizeHint () const
    20. {
    21. return QSize(width, height);
    22. }
    To copy to clipboard, switch view to plain text mode 

    Then within your QTabWidget constructor:
    Qt Code:
    1. centerTab = new CenterTab(100, 25); // You can use any width and height you like
    2. this->setCornerWidget(centerTab, Qt::TopLeftCorner);
    To copy to clipboard, switch view to plain text mode 

    Thanks for the help guys...
    Last edited by forrestfsu; 13th March 2007 at 22:04.

  7. #7
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Center TabBar horizontally on the TabWidget

    Ha, great. I got it...the above works as long as you change:

    Qt Code:
    1. width= width;
    2. height=height;
    To copy to clipboard, switch view to plain text mode 

    to:
    Qt Code:
    1. this->width= width;
    2. this->height=height;
    To copy to clipboard, switch view to plain text mode 

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

  8. #8
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Center TabBar horizontally on the TabWidget

    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)...
    Current Qt projects : QCodeEdit, RotiDeCode

  9. #9
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Center TabBar horizontally on the TabWidget

    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.

Similar Threads

  1. how to put the tabbar at the left in a tabwidget
    By qtopiahooo in forum Qt Programming
    Replies: 1
    Last Post: 14th December 2006, 06:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.