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

    Go ahead blaming me because of a typo. You also seem to not have understand the relation between tags like "Guru" and time of posting. Beside, I didn't gave me the title "Guru". It is just related on how many post you do here. Even if they are crap. So please do not blame me for things I am not responsible for. But anyway go on, dish the dirt, I do not care.

    Or you can start spending the time solving your problem. As I am not very willing to help you after such comment (problem is the language not necessarily the content), but for the sake of peace I give a starting point. You just have to change it for your needs:

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

    To protect me against an other shit storm about how a Guru can write such a code: It is not the most elegant solution - I know -, but it is easy, quick and working.

    EDIT: For a good starting point of reading: http://qt-project.org/doc/qt-4.8/style-reference.html (section Tabs)

  2. The following user says thank you to Lykurg for this useful post:

    CodeFreak (3rd August 2018)

  3. #2
    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

    Quote Originally Posted by Lykurg View Post
    Go ahead blaming me because of a typo.
    Exellent...
    That's what I was speaking about. Nice, neat, right to the point, no bullshit.
    Thanks for help!

    Sorry for the flames before. They were just because there are alot of >/dev/null posts around
    that are really annoying.
    Last edited by ataraxic; 16th July 2013 at 21:53.

  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

    There is another useful thread related to this question (west qtabwidget with horizontal text):

    http://www.qtforum.org/article/28672...b-positon.html

    Quote Originally Posted by Lykurg View Post
    Go ahead blaming me because of a typo
    Hello Lykurg,
    After trying your solution as is, I've got the following:

    west-tab.png

    Can you explain pls howto make horizontal text tabs and keep west qtab?

    Thank you!
    Last edited by ataraxic; 21st July 2013 at 10:53.

  5. #4
    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 

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

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

  7. #5
    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

  8. #6
    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

  9. 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)

  10. #7
    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.

  11. #8
    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.