【问题标题】:dynamically style an individual tab in QTabWidget在 QTabWidget 中动态设置单个选项卡的样式
【发布时间】:2018-02-05 03:24:18
【问题描述】:

我知道,这个问题已经在这里和其他网站上讨论过,但还没有真正的解决方案,虽然我认为,我不是唯一一个遇到这个问题的人:

如何单独和动态访问单个选项卡(而不是其内容和选项卡中的小部件)以进行样式设置,例如更改背景颜色或为其添加图形效果? 应用程序可以通知用户一个选项卡需要他的注意,方法是让它以另一种颜色闪烁(如在窗口任务栏中,如果窗口想要获得焦点)。 有一个改变文字颜色的功能,为什么不更多呢? 样式表可用于访问选定的、第一个等选项卡,但不能通过其索引访问特定的选项卡。 有些人谈到子类化 QTabBar,但我不知道,这将如何导致所需的解决方案。 有没有可能实现这一点,如果有,请提供一个例子。

【问题讨论】:

  • 你可以展示你想要得到的图像。

标签: c++ qt qt5 qtabwidget qtabbar


【解决方案1】:

要访问每个QTabBar 选项卡的每种样式,您必须覆盖它的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);
    }
};

完整的例子可以在以下link找到

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多