在QTabWidget中动态地为单个选项卡设置样式。

3
如何单独且动态地访问一个选项卡(而不是其内容或选项卡中的小部件),以便进行样式设置,比如更改背景颜色或添加图形效果?
一个应用场景可能是通过让选项卡在另一种颜色下闪烁(就像Windows任务栏中的窗口想要获得焦点时那样),来提醒用户某个选项卡需要他们的注意。
有一个函数可以改变文本颜色,为什么不能有更多的功能呢?
样式表可以用于访问所选/第一个...选项卡,但无法通过索引访问特定的选项卡。
有些人谈到了子类化QTabBar,但我不知道这将如何实现期望的解决方案。
如果这是可能的,那么如何实现呢?

你可以展示一张你想要得到的图片。 - eyllanesc
1个回答

4
为了访问每个选项卡的每种样式,您需要覆盖其 paintEvent() 方法。
通用的方法应该具有以下结构:
void paintEvent(QPaintEvent *event){
    QStylePainter painter(this);
    QStyleOptionTab opt;

    for(int index = 0; index < count(); index++)
    {
        initStyleOption(&opt,index);
        /*Here make the changes*/
        painter.drawControl(QStyle::CE_TabBarTabShape, opt);
        painter.drawControl(QStyle::CE_TabBarTabLabel,opt);
    }
}

在这部分中,我将展示一个创建QTabWidget的示例,在该示例中,它会显示一个闪烁的选项卡,并且只有在我们单击该选项卡时才会停止闪烁。 TabBarAlert:
class TabBarAlert : public QTabBar
{
    int indexAlert = -1;
    QColor mColor;
    Q_OBJECT
public:
    TabBarAlert(QWidget *parent = Q_NULLPTR):QTabBar(parent)
    {
        mColor = Qt::red;
    }
    void setIndexAlert(int index){
        if(indexAlert == index)
            return;
        indexAlert = index;
        update();
    }

    int getIndexAlert() const{
        return indexAlert;
    }

    QColor getColor() const{
        return mColor;
    }
    void setColor(const QColor &color){
        if(color == mColor)
            return;
        mColor = color;
        update();
    }

protected:
    void paintEvent(QPaintEvent *event){

        if(indexAlert> -1 && indexAlert < count()){
            QStylePainter painter(this);
            QStyleOptionTab opt;

            for(int i = 0;i < count();i++)
            {
                initStyleOption(&opt,i);

                if(indexAlert == i)
                    opt.palette.setColor(QPalette::Button, mColor);
                painter.drawControl(QStyle::CE_TabBarTabShape, opt);
                painter.drawControl(QStyle::CE_TabBarTabLabel,opt);
            }
        }
        else{
            QTabBar::paintEvent(event);
        }
    }

};

TabWidgetAlert:

class TabWidgetAlert : public QTabWidget
{
    TabBarAlert *tb;
    QTimer *timer;
    bool on = false;
    int indexAlert = -1;

    Q_OBJECT
public:
    TabWidgetAlert(QWidget *parent = Q_NULLPTR):QTabWidget(parent)
    {
        tb = new TabBarAlert(this);
        setTabBar(tb);
        tb->setColor(Qt::black);

        /*
        *Disable the alert if the current tab matches the alert tab.
        */
        connect(this, &TabWidgetAlert::currentChanged, [this](int index){
            if(index == tb->getIndexAlert()){
                tb->setIndexAlert(-1);
                on = false;
                timer->stop();
           }
        });

        timer = new QTimer(this);

        /*
        *Create the blink
        */
        connect(timer, &QTimer::timeout, [this](){
            tb->setIndexAlert(on? indexAlert: -1);
            on = !on;
        });
    }

    void setAlert(int index){
        indexAlert = index;
        timer->start(100);
    }
};

完整示例可在以下链接中找到。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接