class TabBarButton : public QAbstractButton{public: TabBarButton() : QAbstractButton(), hovered(false), pressed(false) { } void paintEvent(QPaintEvent *e) { QPainter p(this); QStyleOptionTabBarBaseV2 opt; opt.init(this); // hardcoded document Mode opt.documentMode = true; int overlap = style()->pixelMetric(QStyle::PM_TabBarBaseOverlap, &opt, this); QRect rect; // hardcoded north position rect.setRect(0, size().height() - overlap, size().width(), overlap); opt.rect = rect; qApp->style()->drawPrimitive(QStyle::PE_FrameTabBarBase, &opt, &p, this); int w = iconSize().width(); int h = iconSize().height(); QIcon::Mode mode = pressed ? QIcon::Selected : QIcon::Normal; icon().paint(&p, (width() - w)/2, (height() - h)/2, w, h, Qt::AlignCenter, mode); } QSize sizeHint() const { return iconSize(); } void enterEvent(QEvent *) { hovered = true; update(); } void leaveEvent(QEvent *) { hovered = false; update(); } void mousePressEvent(QMouseEvent *e) { pressed = true; update(); QAbstractButton::mousePressEvent(e); } void mouseReleaseEvent(QMouseEvent *e) { pressed = false; update(); QAbstractButton::mouseReleaseEvent(e); }private: bool hovered; bool pressed;};
C++ (Qt)QToolButton::setStyleSheet( "border: 0px" );
{... newTabButton = new TabBarButton(); newTabButton->setIcon(QIcon(":/images/icons/addtab.png")); newTabButton->setIconSize(QSize(32,32)); connect(newTabButton, SIGNAL(clicked()), q, SLOT(newTab())); tabWidget = new MyTabWidget; tabWidget->setDocumentMode(true); tabWidget->setMovable(true); tabWidget->setTabsClosable(true); tabWidget->setUsesScrollButtons(true); tabWidget->setCornerWidget(newTabButton); q->setCentralWidget(tabWidget); connect(tabWidget, SIGNAL(currentChanged(int)), this, SLOT(onCurrentChanged(int))); connect(tabWidget, SIGNAL(tabCloseRequested(int)), q, SLOT(closeTab(int))); connect(tabWidget, SIGNAL(tabBarDoubleClicked()), q, SLOT(newTab()));...}