void QTabBar::changeEvent(QEvent *e){ Q_D(QTabBar); if (e->type() == QEvent::StyleChange) { d->elideMode = Qt::TextElideMode(style()->styleHint(QStyle::SH_TabBar_ElideMode, 0, this)); d->useScrollButtons = !style()->styleHint(QStyle::SH_TabBar_PreferNoArrows, 0, this); } d->refresh(); QWidget::changeEvent(e);}
connect(this, SIGNAL(currentChanged(int)), this, SLOT(slot_setCurrentIndex(int)));//...void TabWidget::slot_setCurrentIndex(int index){ Q_UNUSED(index) QEvent event(QEvent::ModifiedChange); QApplication::sendEvent(tabBar(), &event);}
class MyTabBar : public QTabBar{ Q_OBJECT public: MyTabBar (QWidget * parent = 0) : QTabBar(parent) { button = QRect(); curItemRect = QRect(); } protected: void paintEvent(QPaintEvent *e) { if (e->region().contains(curItemRect)){ const QSize s = size(); QResizeEvent *re = new QResizeEvent(s, s); resizeEvent(re); delete re; } QTabBar::paintEvent(e); if (e->region().contains(curItemRect)) drawButton(button); } void resizeEvent(QResizeEvent *e) { QTabBar::resizeEvent(e); curItemRect = tabRect(currentIndex()); button = QRect(curItemRect.right() - 35, curItemRect.top(), 35, curItemRect.height()); } QSize tabSizeHint(int index) const { QSize size = QTabBar::tabSizeHint(index); if (index == currentIndex()){ size.setWidth(size.width() + 40); } return size; } virtual void drawButton(const QRect &rect) { QPainter painter(this); painter.drawText(rect, Qt::AlignHCenter | Qt::AlignVCenter, QString("(X)")); } private: QRect button; QRect curItemRect;};