PDA

View Full Version : QToolBox Colors



rooney
7th July 2006, 21:06
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! ;)

jpn
17th July 2006, 17:39
Adapt the idea from this:


#include <QtGui>

static void setToolBoxButtonColor(QToolBox* toolBox, int index, QColor color)
{
int i = 0;
foreach (QAbstractButton* button, toolBox->findChildren<QAbstractButton*>())
{
// make sure only toolbox button palettes are modified
if (button->metaObject()->className() == QString("QToolBoxButton"))
{
if (i == index)
{
// found correct button
QPalette p = button->palette();
p.setColor(QPalette::Button, color);
button->setPalette(p);
break;
}
i++;
}
}
}

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QToolBox* toolBox = new QToolBox;

toolBox->addItem(new QLineEdit("HAHA"), "LineEdit");
toolBox->addItem(new QTextEdit("HOHO"), "TextEdit");
toolBox->addItem(new QLabel("MUAHAHA"), "Label");

setToolBoxButtonColor(toolBox, 0, Qt::red);
setToolBoxButtonColor(toolBox, 1, Qt::green);
setToolBoxButtonColor(toolBox, 2, Qt::blue);

toolBox->show();

a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
return a.exec();
}


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.

pavanbarot
3rd September 2010, 14:57
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 ??

pavanbarot
3rd September 2010, 15:37
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 */
}