PDA

View Full Version : QTabbar with rich text / html tab text



Berryblue031
18th May 2012, 09:30
Does anybody have experience with getting the Tabs in a QTabbar to render html/rich text?
I couldn't find any references on the web for this and could use a push in the right direction.

Thank you :)

Berryblue031
21st May 2012, 09:46
Ok I couldn't find an ideal solution to this problem, so I implemented a quick and dirty one that I thought I would share.

QTabbar actually supports having two widgets on each tab, one on the right, and one on the left ( this is how close buttons are made ), so instead of setting the text on the tabs I write the text to a label (which supports html) and force it to take up the width of the whole tab.




class RichTextTabBar : public QTabBar
{
Q_OBJECT
public:
void setTabText(int index, const QString& text)
{
QLabel* label = new QLabel(text);
label->setFixedSize(mTabWidth, mTabHeight);
label->setStyleSheet("padding-top:0px; padding-bottom:0px; padding-left:5px; padding-right:5px;");
setTabButton(index, QTabBar::LeftSide, label);
}

private:
int mTabWidth;
int mTabHeight;
};

class RichTextTabWidget : public QTabWidget
{
Q_OBJECT
public:
RichTextTabWidget(QWidget* parent = 0)
: QTabWidget(parent)
{
setTabBar(new RichTextTabBar());
}

void setTabText(int index, const QString &label)
{
tabBar()->setTabText(index, label);
}

private:
RichTextTabBar* tabBar() const;
};