选项卡上的不同关闭按钮

发布于 2024-10-09 15:52:27 字数 101 浏览 0 评论 0原文

我正在使用 QTabWidget,我想知道是否可以使用不同的图标来关闭选项卡上的按钮?我认为 style 和 setCornerWidget 可能不适用于这种情况。

谢谢!

I am using QTabWidget, and I would like to know if I can use different icons for close buttons on tabs? I think style and setCornerWidget may not work for this case.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情栀口红 2024-10-16 15:52:27

setStyleSheet() 与

 QTabBar::close-button {
     image: url(close.png)
 }
 QTabBar::close-button:hover {
     image: url(close-hover.png)
 }

http://doc.qt.io/qt-4.8/stylesheet-examples.html #customizing-qtabwidget-and-qtabbar

Use setStyleSheet() with

 QTabBar::close-button {
     image: url(close.png)
 }
 QTabBar::close-button:hover {
     image: url(close-hover.png)
 }

http://doc.qt.io/qt-4.8/stylesheet-examples.html#customizing-qtabwidget-and-qtabbar

秋风の叶未落 2024-10-16 15:52:27

我认为 QTabWidget 不可能做到这一点。您可以使用 QTabBar,其中可以使用 QTabBar::setTabButton 将您自己设计的小部件设置到选项卡位置。

I don't think that this is possible with a QTabWidget. You could use a QTabBar where you can use QTabBar::setTabButton to set a widget of your own design into the tab position.

小红帽 2024-10-16 15:52:27
#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);
        }
    }
};

在main.cpp中:

QApplication app(argc, argv);
app.setStyle(new AppStyle(app.style()));
#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);
        }
    }
};

in main.cpp:

QApplication app(argc, argv);
app.setStyle(new AppStyle(app.style()));
盗琴音 2024-10-16 15:52:27

选项卡上的默认关闭按钮是您正在使用的 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);
        }

从外观上看,您必须继承 QStyle 并覆盖 PE_IndicatorTabClos​​e 并返回不同的 QIcon 路径。

The default close buttons on tabs are part of the QStyle you are using.

From the Qt sources:

    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);
        }

From what it looks, you have to subclass QStyle and override PE_IndicatorTabClose and return a different QIcon path.

夜清冷一曲。 2024-10-16 15:52:27

例如,如果您想将图标更改为透明背景的图标,并更改悬停时的背景图标:

QTabBar::close-button
{
    image: url(:icons/close.svg)
}

QTabBar::close-button:hover
{
    background: #A0A0A0
}

For instance, if you want to change the icon to one with transparent background, and change the background icon on hovering:

QTabBar::close-button
{
    image: url(:icons/close.svg)
}

QTabBar::close-button:hover
{
    background: #A0A0A0
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文