Results 1 to 17 of 17

Thread: QTabWidget customization

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QTabWidget customization

    You only have to change a row and add one:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class CustomTabStyle : public QProxyStyle
    4. {
    5. public:
    6. QSize sizeFromContents(ContentsType type, const QStyleOption *option,
    7. const QSize &size, const QWidget *widget) const
    8. {
    9. QSize s = QProxyStyle::sizeFromContents(type, option, size, widget);
    10. if (type == QStyle::CT_TabBarTab)
    11. s.transpose();
    12. return s;
    13. }
    14.  
    15. void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
    16. {
    17. if (element == CE_TabBarTabLabel)
    18. {
    19. if (const QStyleOptionTab *tab = qstyleoption_cast<const QStyleOptionTab *>(option))
    20. {
    21. QStyleOptionTab opt(*tab);
    22. opt.shape = QTabBar::RoundedNorth;
    23. QProxyStyle::drawControl(element, &opt, painter, widget);
    24. return;
    25. }
    26. }
    27. QProxyStyle::drawControl(element, option, painter, widget);
    28. }
    29. };
    30.  
    31. int main(int argc, char *argv[])
    32. {
    33. QApplication a(argc, argv);
    34.  
    35. QTabBar tb;
    36. tb.setStyle(new CustomTabStyle);
    37. tb.setShape(QTabBar::RoundedWest);
    38. tb.addTab("Tab 1");
    39. tb.addTab("Tab 2");
    40. tb.show();
    41.  
    42. return a.exec();
    43. }
    To copy to clipboard, switch view to plain text mode 

  2. The following 2 users say thank you to Lykurg for this useful post:

    Big_Stone88 (3rd April 2018), Momergil (19th February 2014)

  3. #2
    Join Date
    Jul 2013
    Location
    India
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTabWidget customization

    Hi Lykurg,

    Is this possible I use your solution to rotate tab bar text from vertical to horizontal. if possible means how?. I attached my output as image. see this,Screenshot from 2013-09-26 12:51:53.jpg

  4. #3
    Join Date
    Jul 2013
    Location
    Switzerland
    Posts
    4
    Thanked 4 Times in 1 Post
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QTabWidget customization

    Here is a working example of the QTabWidget based on Lykurg CustomTabStyle class.
    Works like charm with QT5.0 && QT5.1

    tabdialog.pro:
    Qt Code:
    1. QT += widgets
    2.  
    3. SOURCES = main.cpp
    To copy to clipboard, switch view to plain text mode 

    main.cpp:
    Qt Code:
    1. #include <QtGui>
    2. #include <QWidget>
    3. #include <QDialog>
    4. #include <QApplication>
    5. #include <QtWidgets>
    6.  
    7. class CustomTabStyle : public QProxyStyle
    8. {
    9. public:
    10. QSize sizeFromContents(ContentsType type, const QStyleOption *option,
    11. const QSize &size, const QWidget *widget) const
    12. {
    13. QSize s = QProxyStyle::sizeFromContents(type, option, size, widget);
    14. if (type == QStyle::CT_TabBarTab)
    15. s.transpose();
    16. return s;
    17. }
    18.  
    19. void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
    20. {
    21. if (element == CE_TabBarTabLabel)
    22. {
    23. if (const QStyleOptionTab *tab = qstyleoption_cast<const QStyleOptionTab *>(option))
    24. {
    25. QStyleOptionTab opt(*tab);
    26. opt.shape = QTabBar::RoundedNorth;
    27. QProxyStyle::drawControl(element, &opt, painter, widget);
    28. return;
    29. }
    30. }
    31. QProxyStyle::drawControl(element, option, painter, widget);
    32. }
    33. };
    34.  
    35. int main(int argc, char *argv[], char *envp[])
    36. {
    37. QApplication app(argc, argv);
    38.  
    39. QTabWidget *tabWidget = new QTabWidget;
    40.  
    41. tabWidget->setTabPosition(QTabWidget::West);
    42. tabWidget->tabBar()->setStyle(new CustomTabStyle);
    43.  
    44. tabWidget->addTab(new QWidget, "General");
    45. tabWidget->addTab(new QWidget, "Permissions");
    46. tabWidget->addTab(new QWidget, "Applications");
    47.  
    48. QVBoxLayout *mainLayout = new QVBoxLayout;
    49. mainLayout->addWidget(tabWidget);
    50.  
    51. tabWidget->show();
    52.  
    53. return app.exec();
    54. }
    To copy to clipboard, switch view to plain text mode 

    to compile (! M$):
    Qt Code:
    1. qmake -o Makefile tabdialog.pro
    2. make
    To copy to clipboard, switch view to plain text mode 

    output:
    horiz-tab-dialog.png

  5. The following 4 users say thank you to ataraxic for this useful post:

    Big_Stone88 (3rd April 2018), d_stranz (26th November 2017), Maximus2 (15th October 2013), Momergil (19th February 2014)

  6. #4
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QTabWidget customization

    Thanks for this. But I'm having some problems related to size of icon; resizing of tab width, height; setting the color of tab
    In tried like this, but I think that's not the proper way to change the size
    Qt Code:
    1. QSize sizeFromContents(ContentsType type, const QStyleOption *option,
    2. const QSize &size, const QWidget *widget) const
    3. {
    4. QSize s = QProxyStyle::sizeFromContents(type, option, QSize(40, 120), widget); // instead of size, I'm hardcoding the value with QSize(40, 120)
    5. if (type == QStyle::CT_TabBarTab)
    6. s.transpose();
    7. return s;
    8. }
    To copy to clipboard, switch view to plain text mode 
    1. I'm setting 64x64 pix icon, but still it's being displayed as a small icon. How do I set it to show original size of icon ?
    2. How to give custom width & height to the tab ?
    3. How to set the color of tab

    Kindly help. Thank you.
    Last edited by rawfool; 26th February 2014 at 12:07.

  7. #5
    Join Date
    Feb 2016
    Posts
    21
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QTabWidget customization

    Hallo

    I know, this is an old Thread but i found it now ;-)

    On MAC (Sierra QT5.6) i can't see the Text.
    Any ideas?
    thx Chris

Similar Threads

  1. QTabWidget remove a page at the page's request
    By thomaspu in forum Qt Programming
    Replies: 2
    Last Post: 29th December 2007, 20:45
  2. add a label in a QTabWidget
    By sabeesh in forum Qt Programming
    Replies: 2
    Last Post: 1st August 2007, 07:36
  3. Adding QGroupBox to QTabWidget
    By mclark in forum Newbie
    Replies: 2
    Last Post: 17th January 2007, 16:27
  4. QTabWidget Placement and Display
    By mclark in forum Newbie
    Replies: 1
    Last Post: 16th January 2007, 20:34
  5. Corner widget in QTabWidget doesn't show up
    By ePharaoh in forum Qt Programming
    Replies: 2
    Last Post: 6th April 2006, 17:02

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
  •  
Qt is a trademark of The Qt Company.