qt - How to change text alignment in QTabWidget in C++? -
this same question in: how change text alignment in qtabwidget?
i tried port python code c++ doesn't seem work.
here header file:
#include <qtabbar> class horizontaltabwidget : public qtabbar { q_object public: explicit horizontaltabwidget(qwidget *parent = 0); protected: void paintevent(qpaintevent *); qsize sizehint() const; };
here source file:
void horizontaltabwidget::paintevent(qpaintevent *) { for(int index = 0; index < count(); index++) { qpainter * painter = new qpainter(this); painter->begin(this); painter->setpen(qt::blue); painter->setfont(qfont("arial", 10)); qrect tabrect = tabrect(index); painter->drawtext(tabrect, qt::alignvcenter | qt::textdontclip, tabtext(index)); painter->end(); } } qsize horizontaltabwidget::sizehint() const { return qsize(130, 130); }
i use creating newtabwidget class inherits qtabwidget. in constructor of newtabwidget use:
settabbar(new horizontaltabwidget);
this done able use tabwidget because settabbar protected. here get:
what missing?
edit: want create icons on top , labels under icons (as in qt creator):
the problem should in paint method; check if example below work you, should draw tabs same way qtcreator does. i've reused original tab style qstyleoptiontabv3 majority of paint work , rendered icon , tab's text on top of image produced:
class testtabbar : public qtabbar { public: explicit testtabbar(qwidget* parent=0) : qtabbar(parent) { seticonsize(qsize(80, 80)); } protected: qsize tabsizehint(int) const { return qsize(80, 80); } void paintevent(qpaintevent *) { qstylepainter p(this); (int index = 0; index < count(); index++) { qstyleoptiontabv3 tab; initstyleoption(&tab, index); qicon tempicon = tab.icon; qstring temptext = tab.text; tab.icon = qicon(); tab.text = qstring(); p.drawcontrol(qstyle::ce_tabbartab, tab); qpainter painter; painter.begin(this); qrect tabrect = tabrect(index); tabrect.adjust(0, 8, 0, -8); painter.drawtext(tabrect, qt::alignbottom | qt::alignhcenter, temptext); tempicon.paint(&painter, 0, tabrect.top(), tab.iconsize.width(), tab.iconsize.height(), qt::aligntop | qt::alignhcenter); painter.end(); } } }; class testtabwidget : public qtabwidget { public: explicit testtabwidget(qwidget *parent = 0) : qtabwidget(parent) { settabbar(new testtabbar()); } };
tabwidget init:
testtabwidget* test = new testtabwidget(this); test->setgeometry(20, 20, 300, 200); test->addtab(new qwidget(), qicon("icon0.png"), "test0"); test->addtab(new qwidget(), qicon("icon1.png"), "test1"); test->settabposition(qtabwidget::west);
this worked fine on ubuntu, hope gonna work you, regards
Comments
Post a Comment