Results 1 to 4 of 4

Thread: QToolBox Colors

  1. #1
    Join Date
    Jun 2006
    Posts
    3
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default QToolBox Colors

    Hi,

    I am trying to format a QToolBox.
    Can anyone tell me a way to set a different background colors to the different tabs (headers) of a QToolBox?

    Your help greatly appreciated!

  2. #2
    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: QToolBox Colors

    Adapt the idea from this:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. static void setToolBoxButtonColor(QToolBox* toolBox, int index, QColor color)
    4. {
    5. int i = 0;
    6. foreach (QAbstractButton* button, toolBox->findChildren<QAbstractButton*>())
    7. {
    8. // make sure only toolbox button palettes are modified
    9. if (button->metaObject()->className() == QString("QToolBoxButton"))
    10. {
    11. if (i == index)
    12. {
    13. // found correct button
    14. QPalette p = button->palette();
    15. p.setColor(QPalette::Button, color);
    16. button->setPalette(p);
    17. break;
    18. }
    19. i++;
    20. }
    21. }
    22. }
    23.  
    24. int main(int argc, char *argv[])
    25. {
    26. QApplication a(argc, argv);
    27.  
    28. QToolBox* toolBox = new QToolBox;
    29.  
    30. toolBox->addItem(new QLineEdit("HAHA"), "LineEdit");
    31. toolBox->addItem(new QTextEdit("HOHO"), "TextEdit");
    32. toolBox->addItem(new QLabel("MUAHAHA"), "Label");
    33.  
    34. setToolBoxButtonColor(toolBox, 0, Qt::red);
    35. setToolBoxButtonColor(toolBox, 1, Qt::green);
    36. setToolBoxButtonColor(toolBox, 2, Qt::blue);
    37.  
    38. toolBox->show();
    39.  
    40. a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    41. return a.exec();
    42. }
    To copy to clipboard, switch view to plain text mode 

    Edit: This will naturally work only if items toolbox items are added by QToolBox::addItem(). Using QToolBox::insertItem() in arbitrary order will also make toolbutton's children laid in same order and the color is set for a wrong button. QObject docs promise that:
    The first child added is the first object in the list and the last child added is the last object in the list, i.e. new children are appended at the end.
    Attached Images Attached Images
    Last edited by jpn; 17th July 2006 at 16:49. Reason: A side note
    J-P Nurmi

  3. The following 3 users say thank you to jpn for this useful post:

    gfunk (17th July 2006), markcole (24th May 2007), pavanbarot (3rd September 2010)

  4. #3
    Join Date
    Jul 2010
    Location
    Ahmedabad,Gujarat,India
    Posts
    25
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: QToolBox Colors

    Thanks it's working great.... i have question regarding this topic please reply?

    Hi Please help me??
    how i give a flat button style for All QToolBox button ??

  5. #4
    Join Date
    Jul 2010
    Location
    Ahmedabad,Gujarat,India
    Posts
    25
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: QToolBox Colors

    no need to write code use ui file with style sheet like below code:
    #toolBox my control name
    -----------------------------------------------------------------------------------
    #toolBox::tab:selected { font: bold; color: #4FA600;height: 26px;} /* selected tab */
    #toolBox::tab:!selected { font: bold; color: rgb(85,85,84);height: 26px;} /* non-selected tab */
    #toolBox{
    border: 0px transparent;
    }

    #toolBox::tab:QToolButton{
    border: 1px transparent;
    border-top-left-radius: 6px;
    border-top-right-radius: 6px;
    background-color: rgb(234, 234, 234); /* Button background color no need to write long code */
    }

Similar Threads

  1. Fonts, Colors, and QStyle
    By Jimmy2775 in forum Qt Programming
    Replies: 1
    Last Post: 28th September 2006, 07:26
  2. QToolBox and style
    By Maxilys in forum KDE Forum
    Replies: 6
    Last Post: 26th March 2006, 15:07
  3. Qt Designer 4.0.1 Preview Colors
    By jtyler in forum Qt Tools
    Replies: 2
    Last Post: 14th February 2006, 15:47
  4. Draw a rectangle alternating two colors with qPainter
    By SkripT in forum Qt Programming
    Replies: 12
    Last Post: 24th January 2006, 23:12
  5. Custom Qtoolbox : some problems
    By dr23 in forum Qt Programming
    Replies: 1
    Last Post: 6th January 2006, 13:01

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.