简体   繁体   English

如何自定义 QTabWidget 选项卡?

[英]How to custom QTabWidget tab?

The problem is:问题是:

Customer panel is derived from QTabWidget and has some tabs with some widget's客户面板源自 QTabWidget,并有一些带有一些小部件的选项卡

in which operator (person) can edit data.其中操作员(人)可以编辑数据。 When data is changed in any widget当任何小部件中的数据发生更改时

I check it, and i set bold font in QLabel that is "buddy" for this widget.我检查它,并在 QLabel 中设置了粗体字体,该字体是此小部件的“伙伴”。 I'm doing this by setting:我通过设置来做到这一点:

QFont fontBold; fontBold.setBold(true); 
widget->setFont(fontBold)

So far so good.到目前为止一切顺利。

Next I wanted to have tabs in which there is modified and unsaved data to be also marked with bold font.接下来,我希望有一些标签,其中有修改和未保存的数据也用粗体标记。 Even when user switches to other tab, and he leaves unsaved data this tab should remain marked with bold font until he comes back and save the data.即使当用户切换到其他选项卡,并且他留下未保存的数据时,该选项卡也应保持用粗体标记,直到他回来保存数据。

That's the problem.那就是问题所在。

If I set setFont for widget, or QTabBar - all text, or all tabs are marked bold.如果我为小部件或 QTabBar 设置 setFont - 所有文本或所有选项卡都标记为粗体。

In my class that is derived from QTabWidget I have:在我从 QTabWidget 派生的类中,我有:

QTabBar *tabBar() const { return QTabWidget::tabBar(); }

So I can access the tabBar and for example use setTabTextColor - to mark this tab with different color - that's some kind of solution, but other "buddy"labels are marked with bold font, so tabText should be bold to.所以我可以访问 tabBar 并例如使用 setTabTextColor - 用不同的颜色标记这个选项卡 - 这是某种解决方案,但其他“伙伴”标签用粗体标记,所以 tabText 应该加粗。

If I use setStyleSheet I can make bold font, but there is also a problem:如果我使用 setStyleSheet 我可以制作粗体,但也有一个问题:

this->tabBar()->setStyleSheet("QTabBar::tab { font:bold }");

This sets all tabs with bold text, if I use Pseudo-States like active, selected, etc. - it changes when user switch to other tab, and I need to keep this tab bold until data is saved.这将所有选项卡设置为粗体文本,如果我使用伪状态,如活动、选定等 - 它会在用户切换到其他选项卡时发生变化,我需要保持此选项卡粗体直到数据被保存。

I could use setProperty and then make styleSheet for widget with particular property, but the real problem is, that I can't, or don't know how to access one tab in QTabBar (for example by knowing it's index number)我可以使用 setProperty 然后为具有特定属性的小部件制作 styleSheet,但真正的问题是,我不能,或者不知道如何访问 QTabBar 中的一个选项卡(例如,通过知道它的索引号)

I have seen, that in QT3 there was QTab* QTabBar::tab(int) which gives access to particular tab, but this is no longer available.我已经看到,在 QT3 中有 QTab* QTabBar::tab(int) 可以访问特定选项卡,但这不再可用。 I read http://qt.nokia.com/doc/4.5/stylesheet-examples.html and http://qt.nokia.com/doc/4.5/stylesheet-reference.html and I didn't find solution.我阅读了http://qt.nokia.com/doc/4.5/stylesheet-examples.htmlhttp://qt.nokia.com/doc/4.5/stylesheet-reference.html ,但没有找到解决方案。

How to access particular tab in QTabBar (by its index number) or how to set font:bold with styleSheet for particular tab which preserve bold font when user switches to other tabs?如何访问 QTabBar 中的特定选项卡(通过其索引号)或如何使用 styleSheet 为特定选项卡设置 font:bold 以在用户切换到其他选项卡时保留粗体字体?

Thanks in advance.提前致谢。

To perform this task, we must overwrite the paintEvent() method to create a class that inherits QTabBar .要执行此任务,我们必须覆盖paintEvent()方法以创建一个继承QTabBar的类。

class TabBar: public QTabBar{
    QVector<int> mUnSaved;
public:
    void setUnsaved(int index){
        if(index >= count() || index < 0)
            return;
        mUnSaved << index;
        update();
    }
    void setSaved(int index){
        if(!mUnSaved.contains(index))
            return;
        mUnSaved.remove(mUnSaved.indexOf(index));
        update();
    }
protected:
    void paintEvent(QPaintEvent */*event*/){

        QStylePainter painter(this);
        QStyleOptionTab opt;

        for(int i = 0;i < count();i++)
        {
            initStyleOption(&opt,i);
            painter.save();
            if(mUnSaved.contains(i)){
                painter.setFont(QFont("Times", 10, QFont::Bold));
            }
            painter.drawControl(QStyle::CE_TabBarTabShape, opt);
            painter.drawControl(QStyle::CE_TabBarTabLabel,opt);
            painter.restore();
        }
    }
};

Then we use it in the following example然后我们在下面的例子中使用它

class TabWidget : public QTabWidget
{
    TabBar *mTabBar;
public:
    TabWidget(QWidget *parent=0):QTabWidget(parent){
        mTabBar = new TabBar;
        setTabBar(mTabBar);
        for(int i=0; i < 5; i++){
            QString text = QString("Tab %1").arg(i);
            addTab(new QLabel(text, this), text);
        }
        mTabBar->setUnsaved(1);
        mTabBar->setUnsaved(3);
        mTabBar->setUnsaved(4);
        mTabBar->setSaved(3);
        mTabBar->setSaved(10);
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    a.setStyle("fusion");
    TabWidget w;
    w.show();
    return a.exec();
}

Output:输出:

在此处输入图片说明

The complete example can be found in the following link完整的示例可以在以下链接中找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM