Results 1 to 11 of 11

Thread: How to change color of Tab in QTabWidget?

  1. #1
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Question How to change color of Tab in QTabWidget?

    How to change color of Tab in QTabWidget? and how to change shape of TAB, the shape other than available in Qt.

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to change color of Tab in QTabWidget?

    You will need to subclass QTabWidget and QTabBar. In the reimplemented version of QTabBar you can changed the color of any tab.

    Use the protected function - setTabBar() of QTabWidget to set your custom tab.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to change color of Tab in QTabWidget?

    If you want to change the colour of all tabs to the same value, you can avoid subclassing and just change the palette of the tab bar. To change the shape, you have to reimplement the paint event and do the drawing yourself.

  4. #4
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: How to change color of Tab in QTabWidget?

    changing palette not chnaging color

    see the code after subclassing
    TabWidget::TabWidget(QWidget *parent): QTabWidget(parent),mousePressFlag(false)
    {
    bar=tabBar();
    QPalette palette;
    bar->installEventFilter(this);

    palette.setColor(QPalette::Active,QPalette::Button ,QColor(0,0,255));
    bar->setPalette(palette);
    bar->setAutoFillBackground(true);
    };

    see the trolltech reply:
    > 1. How to change color of Tab in QTabWidget?
    You can subclassing the style you are using and re-implement
    drawControl() function. In this function you could fill the rectangle
    that the tab covers and then call drawControl() function from the base
    class.

    how to do this? any idea?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to change color of Tab in QTabWidget?

    Quote Originally Posted by rajesh
    changing palette not chnaging color

    see the code after subclassing
    TabWidget::TabWidget(QWidget *parent): QTabWidget(parent),mousePressFlag(false)
    {
    bar=tabBar();
    QPalette palette;
    bar->installEventFilter(this);

    palette.setColor(QPalette::Active,QPalette::Button ,QColor(0,0,255));
    bar->setPalette(palette);
    bar->setAutoFillBackground(true);
    };
    I would try something like:
    Qt Code:
    1. TabWidget::TabWidget(QWidget *parent) : QTabWidget(parent){
    2. QPalette pal = tabBar()->palette;
    3. pal.setColor(QPalette::Window,QColor(0,0,255));
    4. tabBar()->setPalette(pal);
    5. }
    To copy to clipboard, switch view to plain text mode 

    see the trolltech reply:
    > 1. How to change color of Tab in QTabWidget?
    You can subclassing the style you are using and re-implement
    drawControl() function. In this function you could fill the rectangle
    that the tab covers and then call drawControl() function from the base
    class.

    how to do this? any idea?
    Subclass one of QStyle subclasses, reimplement drawControl() by changing the colour palette used for drawing the control and force that style on your application. But I think it might be easier to subclass QTabBar as already suggested.

  6. #6
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: How to change color of Tab in QTabWidget?

    Qt Code:
    1. TabWidget::TabWidget(QWidget *parent) : QTabWidget(parent)
    2. {
    3. QPalette pal = tabBar()->palette();
    4. pal.setColor(QPalette::Window,QColor(0,0,255));
    5. tabBar()->setPalette(pal);
    6. }
    To copy to clipboard, switch view to plain text mode 

    not working
    Last edited by jacek; 17th July 2006 at 12:58. Reason: changed [ html ] tags to [ code ] tags

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to change color of Tab in QTabWidget?

    I think QWindowsXPStyle might not respect the palette.
    Even setting red palette (for each and every color role) to a tab widget:
    Qt Code:
    1. tabWidget->setPalette(QPalette(Qt::red));
    To copy to clipboard, switch view to plain text mode 
    Only tab bar background gets red color, see the attached screenshot.
    Attached Images Attached Images
    J-P Nurmi

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to change color of Tab in QTabWidget?

    This:
    Qt Code:
    1. #include <QApplication>
    2. #include <QTabWidget>
    3. #include <QPalette>
    4. #include <QTabBar>
    5.  
    6. class MyTabWidget : public QTabWidget {
    7. public:
    8. MyTabWidget(QWidget *p=0) : QTabWidget(p){
    9. QPalette pal = tabBar()->palette();
    10. pal.setColor(QPalette::Window, QColor(255,0,0));
    11. pal.setColor(QPalette::Button, QColor(255,255,0));
    12. pal.setColor(QPalette::WindowText, QColor(0,0,255));
    13. pal.setColor(QPalette::ButtonText, QColor(255,0,255));
    14. pal.setColor(QPalette::Base, QColor(0, 200, 0));
    15. pal.setColor(QPalette::AlternateBase, QColor(0, 255, 255));
    16. pal.setColor(QPalette::Text, Qt::white);
    17. tabBar()->setPalette(pal);
    18. }
    19. };
    20.  
    21. int main(int argc, char **argv){
    22. QApplication app(argc, argv);
    23. MyTabWidget tw;
    24. tw.addTab(new QWidget(), "Tab1");
    25. tw.addTab(new QWidget(), "Tab2");
    26. tw.show();
    27. return app.exec();
    28. }
    To copy to clipboard, switch view to plain text mode 

    gives the result illustrated by the attachment on Plastique. Run it with -style <stylename> to check other styles, if you want.
    Attached Images Attached Images

  9. #9
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: How to change color of Tab in QTabWidget?

    I created project using qmake -tp vc xxx.pro
    and compling using VC++ on windows xp.
    so, hot to set -style <stylename> ?

    wysota code changing only text color.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to change color of Tab in QTabWidget?

    Quote Originally Posted by rajesh
    I created project using qmake -tp vc xxx.pro
    and compling using VC++ on windows xp.
    so, hot to set -style <stylename> ?
    Run it from the command line and give it the above mentioned parameter, like:
    xxx.exe -style plastique

    wysota code changing only text color.
    Not only (inactive tabs are coloured and active one has a yellow border), but the point is it doesn't work like expected. It probably takes the tab colour from the main widget associated with the tab, so you might try to change the colour of that and see if the tab changes too.

  11. #11
    Join Date
    Oct 2013
    Posts
    1
    Qt products
    Qt3

    Default Re: How to change color of Tab in QTabWidget?

    It's not working for me!!!

Similar Threads

  1. Change background color of QPushButton
    By gtthang in forum Qt Programming
    Replies: 3
    Last Post: 26th November 2013, 10:23
  2. Can't change the text display of a QTreeview item.
    By johnny_sparx in forum Qt Programming
    Replies: 3
    Last Post: 2nd June 2006, 01:03
  3. When itemChanged is emitted, can the change type be detected?
    By johnny_sparx in forum Qt Programming
    Replies: 3
    Last Post: 7th April 2006, 17:29
  4. Corner widget in QTabWidget doesn't show up
    By ePharaoh in forum Qt Programming
    Replies: 2
    Last Post: 6th April 2006, 17:02
  5. QTableView change color of current Cell
    By raphaelf in forum Qt Programming
    Replies: 4
    Last Post: 4th March 2006, 11:22

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.