mirror of
https://gitee.com/drabel/LibQQt.git
synced 2025-01-04 10:18:44 +08:00
update
This commit is contained in:
parent
37c32018af
commit
17823a8861
@ -51,6 +51,8 @@ MainWindow::MainWindow(QWidget* parent) :
|
||||
tab->setIconStyle(QQtTabBar::IconStyle_Left_And_RightText);
|
||||
tab->setTabPixmap(0, "./skin/default/bt_setting.png", "./skin/default/bt_setting_press.png");
|
||||
tab->setTabPixmap(1, "./skin/default/bt_user.png", "./skin/default/bt_user_press.png");
|
||||
ui->tw0->localTabBar()->setTabTextColor(0, QColor(44, 55, 66));
|
||||
ui->tw0->localTabBar()->setTabTextColor(1, QColor(44, 55, 66));
|
||||
}
|
||||
|
||||
|
||||
|
@ -23,6 +23,33 @@ void QQtTabBar::setIconStyle(QQtTabBar::IconStyle iconStyle)
|
||||
}
|
||||
}
|
||||
|
||||
void QQtTabBar::setTextFont(QFont textFont)
|
||||
{
|
||||
if (this->textFont != textFont)
|
||||
{
|
||||
this->textFont = textFont;
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
void QQtTabBar::setTextColor(QColor textColor)
|
||||
{
|
||||
if (this->textColor != textColor)
|
||||
{
|
||||
this->textColor = textColor;
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
void QQtTabBar::setSelectedTextColor(QColor selectedTextColor)
|
||||
{
|
||||
if (this->selectedTextColor != selectedTextColor)
|
||||
{
|
||||
this->selectedTextColor = selectedTextColor;
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
void QQtTabBar::tabPixmap(int index, QImage& icon, QImage& iconSel)
|
||||
{
|
||||
if (index < 0 || index + 1 > count() || index + 1 > iconList.size())
|
||||
@ -49,51 +76,50 @@ void QQtTabBar::setTabPixmap(int index, const QString& icon, const QString& icon
|
||||
void QQtTabBar::paintEvent(QPaintEvent* e)
|
||||
{
|
||||
Q_UNUSED(e)
|
||||
QStylePainter p(this);
|
||||
QPainter p(this);
|
||||
drawPicture(&p);
|
||||
drawText(&p);
|
||||
}
|
||||
|
||||
void QQtTabBar::drawPicture(QPainter* p)
|
||||
{
|
||||
for (int index = 0; index < count(); index++)
|
||||
{
|
||||
//TODO:
|
||||
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
|
||||
QStyleOptionTabV3 opt;
|
||||
#else
|
||||
QStyleOptionTab opt;
|
||||
#endif
|
||||
initStyleOption(&opt, index);
|
||||
opt.shape = verticalTabs() ? QTabBar::RoundedWest : QTabBar::RoundedNorth;
|
||||
|
||||
QRect tabRectValue = tabRect(index);
|
||||
QRect iconRect = tabRect(index);
|
||||
if (iconStyle == IconStyle_Cover_And_BottomText)
|
||||
tabRectValue = tabRect(index);
|
||||
iconRect = tabRect(index);
|
||||
else if (iconStyle == IconStyle_Left_And_RightText)
|
||||
tabRectValue = QRect(tabRectValue.left(), tabRectValue.top(),
|
||||
tabRectValue.height(), tabRectValue.height());
|
||||
iconRect = QRect(iconRect.left(), iconRect.top(),
|
||||
iconRect.height(), iconRect.height());
|
||||
|
||||
if (iconList.size() > index)
|
||||
{
|
||||
p->save();
|
||||
int sel = currentIndex() == index ? BTN_PRESS : BTN_NORMAL;
|
||||
p.drawItemPixmap(tabRectValue, Qt::AlignCenter, QIcon(iconList[index][sel]).pixmap(rect().size(), QIcon::Normal, QIcon::On));
|
||||
p->drawPixmap(iconRect, QIcon(iconList[index][sel]).pixmap(rect().size(), QIcon::Normal, QIcon::On));
|
||||
/*
|
||||
* 失真不明显,使用以下方法
|
||||
*/
|
||||
//QImage image(iconList[index][sel]);
|
||||
//p.drawItemPixmap(tabRectValue, Qt::AlignLeft |Qt::AlignTop, QPixmap::fromImage(image.scaled(tabRectValue.size(), Qt::KeepAspectRatio)));
|
||||
p->restore();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//opt.palette.setColor(QPalette::Normal, QPalette::Text, QColor(255, 132, 0));
|
||||
opt.palette.setColor(QPalette::Normal, QPalette::Text, tabTextColor(index));
|
||||
opt.palette.setCurrentColorGroup(QPalette::Active);
|
||||
opt.state |= QStyle::State_Sunken;
|
||||
|
||||
tabRectValue = tabRect(index);
|
||||
void QQtTabBar::drawText(QPainter* p)
|
||||
{
|
||||
for (int index = 0; index < count(); index++)
|
||||
{
|
||||
QRect tabTextRect = tabRect(index);
|
||||
//-rect.height()/20 上移
|
||||
verticalTabs() ? tabRectValue.adjust(0, 0, 0, 0) : tabRectValue.adjust(0, 0, 0, 0);
|
||||
verticalTabs() ? tabTextRect.adjust(0, 0, 0, 0) : tabTextRect.adjust(0, 0, 0, 0);
|
||||
|
||||
if (iconStyle == IconStyle_Cover_And_BottomText)
|
||||
tabRectValue = tabRect(index);
|
||||
tabTextRect = tabRect(index);
|
||||
else if (iconStyle == IconStyle_Left_And_RightText)
|
||||
tabRectValue = QRect(tabRectValue.left() + tabRectValue.height(), tabRectValue.top(),
|
||||
tabRectValue.width() - tabRectValue.height(), tabRectValue.height());
|
||||
tabTextRect = QRect(tabTextRect.left() + tabTextRect.height(), tabTextRect.top(),
|
||||
tabTextRect.width() - tabTextRect.height(), tabTextRect.height());
|
||||
|
||||
int flags = Qt::AlignCenter;
|
||||
if (iconStyle == IconStyle_Cover_And_BottomText)
|
||||
@ -102,10 +128,14 @@ void QQtTabBar::paintEvent(QPaintEvent* e)
|
||||
flags = Qt::AlignCenter;
|
||||
|
||||
//pline() << objectName() << rect;
|
||||
//if on board text is normal, this is right. otherwise the palette is right
|
||||
p->save();
|
||||
if (index == currentIndex())
|
||||
p.drawItemText(tabRectValue, flags, opt.palette, true, opt.text, QPalette::Text);
|
||||
p->setPen(selectedTextColor);
|
||||
else
|
||||
p.drawItemText(tabRectValue, flags, opt.palette, true, opt.text, QPalette::BrightText);
|
||||
p->setPen(textColor);
|
||||
p->drawText(tabTextRect, flags, tabText(index));
|
||||
p->restore();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -25,11 +25,22 @@ public:
|
||||
IconStyle getIconStyle() const { return iconStyle; }
|
||||
void setIconStyle(IconStyle iconStyle);
|
||||
|
||||
QFont getTextFont() const { return textFont; }
|
||||
void setTextFont(QFont textFont);
|
||||
|
||||
QColor getTextColor() const { return textColor; }
|
||||
void setTextColor(QColor textColor);
|
||||
|
||||
QColor getSelectedTextColor() const { return selectedTextColor; }
|
||||
void setSelectedTextColor(QColor selectedTextColor);
|
||||
|
||||
void tabPixmap(int index, QImage& icon, QImage& iconSel);
|
||||
void setTabPixmap(int index, const QString& icon = QString(), const QString& iconSel = QString());
|
||||
|
||||
protected:
|
||||
void paintEvent(QPaintEvent*);
|
||||
virtual void paintEvent(QPaintEvent*);
|
||||
virtual void drawPicture(QPainter* p);
|
||||
virtual void drawText(QPainter* p);
|
||||
|
||||
private:
|
||||
inline bool verticalTabs();
|
||||
@ -39,6 +50,9 @@ public slots:
|
||||
private:
|
||||
QList<TBtnIconTable> iconList;
|
||||
IconStyle iconStyle;
|
||||
QFont textFont;
|
||||
QColor textColor;
|
||||
QColor selectedTextColor;
|
||||
};
|
||||
|
||||
#endif // QQTTABBAR_H
|
||||
|
Loading…
x
Reference in New Issue
Block a user