Created
August 28, 2017 00:54
-
-
Save eyllanesc/4a821d2956b5e2ee841f3eb156da078d to your computer and use it in GitHub Desktop.
solution by: https://stackoverflow.com/questions/45909147/qt-c-dynamically-style-an-individual-tab-in-qtabwidget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QApplication> | |
#include <QMainWindow> | |
#include <QTabWidget> | |
#include <QTabBar> | |
#include <QStylePainter> | |
#include <QStyleOptionTab> | |
#include <QTimer> | |
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); | |
} | |
} | |
}; | |
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); | |
} | |
}; | |
#include "main.moc" | |
int main(int argc, char **argv) | |
{ | |
QApplication app(argc, argv); | |
TabWidgetAlert tabs; | |
tabs.addTab(new QWidget(),"TAB 1"); | |
tabs.addTab(new QWidget(),"TAB 2"); | |
tabs.addTab(new QWidget(),"TAB 3"); | |
tabs.setAlert(1); | |
tabs.show(); | |
return app.exec(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#------------------------------------------------- | |
# | |
# Project created by QtCreator 2017-08-27T16:40:21 | |
# | |
#------------------------------------------------- | |
QT += core gui | |
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets | |
TARGET = TabWidgetAlert | |
TEMPLATE = app | |
# The following define makes your compiler emit warnings if you use | |
# any feature of Qt which as been marked as deprecated (the exact warnings | |
# depend on your compiler). Please consult the documentation of the | |
# deprecated API in order to know how to port your code away from it. | |
DEFINES += QT_DEPRECATED_WARNINGS | |
# You can also make your code fail to compile if you use deprecated APIs. | |
# In order to do so, uncomment the following line. | |
# You can also select to disable deprecated APIs only up to a certain version of Qt. | |
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 | |
SOURCES += \ | |
main.cpp | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment