Qt4 选项卡上的不同关闭按钮
我正在使用QTabWidget,我想知道我是否可以使用不同的图标来关闭选项卡上的按钮?我认为style和setCornerWidget可能不适合这种情况Qt4 选项卡上的不同关闭按钮,qt4,qtabbar,Qt4,Qtabbar,我正在使用QTabWidget,我想知道我是否可以使用不同的图标来关闭选项卡上的按钮?我认为style和setCornerWidget可能不适合这种情况 谢谢 我认为这在QTabWidget上是不可能的。您可以使用QTabBar,其中可以使用QTabBar::setTabButton将您自己设计的小部件设置到选项卡位置。选项卡上的默认关闭按钮是您正在使用的QStyle的一部分 从Qt来源: case PE_IndicatorTabClose: { if (d->tabBarclo
谢谢 我认为这在QTabWidget上是不可能的。您可以使用QTabBar,其中可以使用QTabBar::setTabButton将您自己设计的小部件设置到选项卡位置。选项卡上的默认关闭按钮是您正在使用的
QStyle
的一部分
从Qt来源:
case PE_IndicatorTabClose: {
if (d->tabBarcloseButtonIcon.isNull()) {
d->tabBarcloseButtonIcon.addPixmap(QPixmap(
QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-closetab-16.png")),
QIcon::Normal, QIcon::Off);
d->tabBarcloseButtonIcon.addPixmap(QPixmap(
QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-closetab-down-16.png")),
QIcon::Normal, QIcon::On);
d->tabBarcloseButtonIcon.addPixmap(QPixmap(
QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-closetab-hover-16.png")),
QIcon::Active, QIcon::Off);
}
案例PE_指示灯关闭:{
如果(d->tabBarcloseButtonIcon.isNull()){
d->tabBarcloseButtonIcon.addPixmap(QPixmap(
QLatin1String(:/trolltech/styles/commonstyle/images/standardbutton-closetab-16.png”),
QIcon::正常,QIcon::关闭);
d->tabBarcloseButtonIcon.addPixmap(QPixmap(
QLatin1String(:/trolltech/styles/commonstyle/images/standardbutton-closetab-down-16.png”),
QIcon::正常,QIcon::打开);
d->tabBarcloseButtonIcon.addPixmap(QPixmap(
QLatin1String(:/trolltech/styles/commonstyle/images/standardbutton-closetab-hover-16.png”),
QIcon::活动,QIcon::关闭);
}
从外观上看,您必须对QStyle进行子类化并覆盖PE_IndicatorTabClose
并返回不同的QIcon路径。使用
太好了。但我想我可能需要为选项卡ID传递一个值。
QTabBar::close-button {
image: url(close.png)
}
QTabBar::close-button:hover {
image: url(close-hover.png)
}
#include <QProxyStyle>
class AppStyle : public QProxyStyle
{
Q_OBJECT
public:
AppStyle(QStyle *style = 0) : QProxyStyle(style) {}
void drawPrimitive(QStyle::PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
{
if (element == PE_IndicatorTabClose)
{
int size = proxy()->pixelMetric(QStyle::PM_SmallIconSize);
QIcon::Mode mode = option->state & State_Enabled ?
(option->state & State_Raised ? QIcon::Active : QIcon::Normal)
: QIcon::Disabled;
if (!(option->state & State_Raised)
&& !(option->state & State_Sunken)
&& !(option->state & QStyle::State_Selected))
mode = QIcon::Disabled;
QIcon::State state = option->state & State_Sunken ? QIcon::On : QIcon::Off;
QPixmap pixmap = QIcon(":myclose.png").pixmap(size, mode, state);
proxy()->drawItemPixmap(painter, option->rect, Qt::AlignCenter, pixmap);
}
else
{
QProxyStyle::drawPrimitive(element,option,painter,widget);
}
}
};
QApplication app(argc, argv);
app.setStyle(new AppStyle(app.style()));