Background color of a specific tab of QTabBar (QTabWidget)
-
@Goutye
To bring some new explanation, here is the way to access a specific tab of a QTabBar official code source:const QTabBarPrivate::Tab &tab = d->tabList.at(tabIndex);
More information can be found here: http://code.woboq.org/qt5/qtbase/src/widgets/widgets/qtabbar.cpp.html
The problem is
QTabBarPrivate
is... obviously private. I cannot usesetProperty
on this specific tab because I don't have any access toQTabBarPrivate
functions. The structure ofQTabBarPrivate::tab
is private as well so unable to modify anything.Is there a way to deal with this case? I just want a background-color for the tabs that received a new message. Particularly complicated as I can see.
EDIT: As well, the children of a QTabBar are the QCloseButton added to the 2 QToolButton necessary for browsing the tabs when there are too many of them. I am quite stuck.
-
Looks like you would need to re-implement paintEvent and there you will be able to change the color of the tabs that needs it
-
@SGaist Thank you again for your answer!
That's exactly what I wanted to try. However, regarding the source code of
QTabBar::paintEvent(QPaintEvent *)
, it seems really complicated to re-implement exactly the whole system because of the private state ofQTabBarPrivate
.
So then, I will have to re-implement a large part of the wholeQPaintEvent
to get a result close to the original.
I will try tomorrow to see if I can get something working, but it is my first time using aQPaintEvent
at this depth, so it will take certainly a lot of time. -
Have you considered change text color instead?
Another work around may be creating custom widget for such tab.
void QTabBar::setTabButton ( int index, ButtonPosition position, QWidget * widget ) -
@alex_malyu Thank you for your answer!
My specifications required to change the color of the background, that is why I will not change the text color instead.Regarding the setTabButton, the problem is the button will be place on the left or right inside the tab and will not take the whole place.
-
I succeeded in implementing the trick. However, a new problem just appeared. A kind of gray bar is drawn under the tab on the top appeared.
https://i.gyazo.com/55a99a8e4261252d88081c1b0bf35e25.png
I searched a bit and find that it is due to the
p.drawPrimitive(QStyle::PE_FrameTabBarBase, optTabBase);
of the original paintEvent.The strategy I use to draw the notified tab is the following one:
- I call the QTabBar::paintEvent(event) to have the original tab drawn. (The weird gray bar appears at this step)
- I draw the notified tabs over the existent tabs and redraw the dragged tab.
Is there a way to fix this weird bar? I will continue to search and post the whole code after.
-
What OS are you running ?
-
@SGaist Currently Windows, this bar doesn't appear on a normal QTabBar.
-
It could be a simple question of matching colors. Take a look at the QCommonStyle for the drawing part
-
Thanks for the answer. I was really busy until now. That doesn't seem to be related to QCommonStyle, but if it is, I do not know how to figure it out.
However, for the moment, I have something that's working well because we changed the designed for something more "material design".
Thanks for the answers!