Results 1 to 13 of 13

Thread: QTabWidget with no text and icon

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #8
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    20
    Thanked 6 Times in 5 Posts

    Red face Re: QTabWidget with no text and icon

    I am just trying to help
    Ok, here's the code:

    First you need to make a custom QTabBar class. A sample code is as follows:

    Header file (.h):
    Qt Code:
    1. #include <QTabBar>
    2. #include <QIcon>
    3. #include <QPainter>
    4.  
    5. class StatusTabBar : public QTabBar
    6. {
    7. Q_OBJECT
    8. public:
    9. StatusTabBar ();
    10. virtual ~StatusTabBar () {}
    11.  
    12. protected:
    13. QSize tabSizeHint (int) const { return QSize(70,27);}
    14. void paintEvent(QPaintEvent *);
    15. };
    To copy to clipboard, switch view to plain text mode 

    So, you need to create a custom tab bar class by deriving from QTabBar and then overwriting the tabSizeHint (use this to specify the size of the tabs) and paintEvent (use this for custom drawing). Here's the code from .cpp file:

    Qt Code:
    1. #include <QTabBar>
    2. #include <QBitmap>
    3. #include <QIcon>
    4. #include <QStylePainter>
    5. #include <QStyleOptionTabV2>
    6.  
    7. #include "statustabbar.h"
    8.  
    9. StatusTabBar::StatusTabBar(): QTabBar()
    10. {
    11. }
    12.  
    13. void StatusTabBar::paintEvent(QPaintEvent *event)
    14. {
    15. QStylePainter painter(this);
    16.  
    17. for(int i = 0; i < 3; ++i)
    18. {
    19. initStyleOption(&option, i);
    20. //printf("tab text: %s\n", option.text.toLatin1().data());
    21. //painter.drawControl(QStyle::CE_TabBarTab, option);
    22. painter.drawItemPixmap(option.rect, 0, QPixmap("selected.png"));
    23. painter.drawItemText(option.rect, 0, palette(), 1, option.text);
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

    Here, i have 3 tabs in my application, so i hard-coded the loop to run 3 times, but i it's possible to get this value from Qt. You should read more about QStylePainter and QStyleOptionTabV2 class in the Qt documentation. QStylePainter::drawItemPixmap can be used for adding a pixmap to the tabs. option.rect contains the rectangle for the entire tab, so if you want to draw the icon at a particular location, you can specify the appropriate rect. QStylePainter::drawControl can be used for drawing various parts of the tab. You can read more about it the Qt documentation.

    Also, if you want to see more drawing code and how Qt draws the default tab bar, check out the paintEvent function in qtabbar.cpp in the Qt source code (qt-source/src/gui/widgets/qtabbar.cpp)

    Edit: If you use custom drawing you may not be able to use style sheets. I am not sure it'll work but you can try.

    I hope this helps.
    Last edited by montylee; 11th February 2010 at 18:22.

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

    martinn (12th February 2010), posixprogrammer (25th April 2011)

Similar Threads

  1. Button icon and text
    By electronicboy in forum Qt Programming
    Replies: 5
    Last Post: 9th October 2009, 23:27
  2. [QTabWidget] horizontal text on left side
    By Boy in forum Qt Programming
    Replies: 6
    Last Post: 16th May 2008, 09:52
  3. Text under the Icon
    By QiT in forum Newbie
    Replies: 3
    Last Post: 1st April 2007, 18:42
  4. Qlabel with (icon and text) HowTo ?
    By QiT in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2006, 09:14
  5. Icon Text Alignment
    By nupul in forum Newbie
    Replies: 3
    Last Post: 1st May 2006, 05:47

Tags for this Thread

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.