QTabWidget, setting colour of tab itself ?
-
@JonB , from ui_...h file:
overall_tab = new TabBar(); overall_tab->setObjectName(QString::fromUtf8("overall_tab"));
Some where nearer the end of file in the function retranslateUi there is a call to:
tab_widget->setTabText(tab_widget->indexOf(overall_tab), QApplication::translate("::wsStatusPage", "&Overall", 0, QApplication::UnicodeUTF8));
Its all a bit fragmented...is there anything I can do to the UI file that will help me to identify the tab when the constructor is called?
I'm reworking this code, as it isn't well written and doesn't follow the correct way of doing things, hopefully this will lead to a solution.
-
@SPlatten said in QTabWidget, setting colour of tab itself ?:
is there anything I can do to the UI file that will help me to identify the tab when the constructor is called?
Nope. As you can see, the tab text is not set until that
tab_widget->setTabText()
is called, much later than construction. That's how it implements the instructions from the.ui
file.At least in terms of the tab's text/title, which is what you say you want to know.
And once again anyway (not that it helps): "the constructor" of what are you asking about?
QTabWidget
?QTabBar
? Tabs are not even constructed until after both of these anyway. -
@SPlatten said in QTabWidget, setting colour of tab itself ?:
I have edited the XML file and changed instances of QTabWidget to TabWidget and the tabs from QWidget to TabBar
class TabBar : public QTabBar
BTW, what is going on here? You confuse me (not for the first time!). A
QTabWidget
is supposed to have oneQTabBar
and a number ofQWidget
s for the widgets/contents/tabs of each page; one page per item in the tab bar. You have changed so that yourQTabWidget
has aQTabBar
as each of its widgets/pages/tabs?? -
@JonB , to be honest, I'm confused by the implementation I have downloaded, I was looking online for an implementation that allows me to set the colours of the tabs and I found the source I'm using, this does appear to use a TabBar for each tab, please take a look at the source I posted in my earlier posts.
I'm looking at it again as I may have goofed it up.
-
@J.Hilk, @JonB , @JoeCFD, so I've finally got everything the way it should be and in my debug output I can see that various bits are being called, but it doesn't do what it is supposed to do, I'm not seeing the tabs in a different colour.
The modified classes:
typedef QMap<QString, QVariant> mpFaults; class TabBar : public QTabBar { private: mpFault* Faults_; protected: void paintEvent(QPaintEvent* evt) { int intTabs(count()), intPossibleFaults(Faults_->count()); QStylePainter painter(this); QStyleOptionTab opt; const char* cpszLog((QString("TabBar::paintEvent: %1 possibleFaults:%2\r\n") .arg(intTabs).arg(intPossibleFaults)).toLatin1().data()); TabBar::logToFile(cpszLog); if ( intTabs > 0 && intPossibleFaults == 0 ) { for ( int intTab=0; intTab<intTabs; intTab++ ) { QString strText(tabText(intTab)); Faults_->insert(strText, QVariant(false)); } } for( int intTab=0; intTab<intTabs; intTab++ ) { initStyleOption(&opt, intTab); const char* cpszLog((QString("TabBar::tab: %1\r\n") .arg(opt.text)).toLatin1().data()); TabBar::logToFile(cpszLog); mpFaults::iterator itTab(Faults_->find(opt.text)); if ( itTab != Faults_->end() ) { QVariant varValue(itTab.value()); bool blnState(varValue.toBool()); const char* cpszLog((QString("TabBar::blnState: %1\r\n") .arg(blnState)).toLatin1().data()); TabBar::logToFile(cpszLog); if ( blnState == true ) { const char* cpszLog((QString("TabBar::ColourSet: %1\r\n") . arg(kErrorColour)).toLatin1().data()); opt.palette.setColor(QPalette::Button, QColor(kErrorColour)); } } painter.drawControl(QStyle::CE_TabBarTabShape, opt); painter.drawControl(QStyle::CE_TabBarTabLabel, opt); } } public: static void logToFile(const char* cpszMsg) { FILE* fp(fopen("/usr/local/cgu/paint.txt", "at")); if ( fp ) { fputs(cpszMsg, fp); fclose(fp); } } TabBar(QWidget* pParent = 0) : QTabBar(pParent) { TabBar::logToFile("TabBar::TabBar!!\r\n"); Faults_ = new mpFaults(); } bool setFault (QString& strText, bool blnState) { mpFaults::iterator itTab(Faults_->find(strText)); const char* cpszLog((QString("TabBar::setFault(%1,%2)\r\n") .arg(strText).arg(blnState)).toLatin1().data()); TabBar::logToFile(cpszLog); if ( itTab != Faults_->end() ) { itTab.value() = QVariant(blnState); return true; } return false; } }; class TabWidget : public QTabWidget { public: TabWidget(QWidget* pParent = 0) : QTabWidget(pParent) { setTabBar(new TabBar()); } bool setFault(QString& strText, bool blnState) { TabBar* pTabBar(dynamic_cast<TabBar*>(tabBar())); if ( pTabBar ) { return pTabBar->setFault(strText, blnState); } return false; } };
I see all the various debug messages in the log file, but tab is not showing as red which is the string literal assigned to kErrorColour:
const char_t kErrorColour[] = "red";
Logged to file:
TabBar::paintEvent: 10 possibileFaults:10 TabBar::tab: &Overall TabBar::blnState: 1 TabBar::ColourSet: red
-
@SPlatten Probably. 4.8 is ancient code. Nobody here will try 4.8 for you. It is amazing that the people still have patience to use it. Poor you.
Let me check my code to see if you can use tabbar to change the color. But my code is much more customized.
-
- 4.8 qtabbar_cpp source code is here. copy line 1569 to 1648 to your paint event. Not sure if this will work.
https://dreamswork.github.io/qt4/qtabbar_8cpp_source.html
painting of tabs is done at line 1622. - Look at lines 1594 and 1595. These lines call initStyleOption() to create QStyleOptionTabV3 tab for each tab which is a subclass of QStyleOption
https://dreamswork.github.io/qt4/classQTabBar.html#a41b394d892263b6b5a0705fb979f3c8e
and each QStyleOption has a public palette with background color. You can change it. Try to print out the background color to see what it is. - you can find QStyleOptionTabV3 and QStyleOptionTab here
https://dreamswork.github.io/qt4/qstyleoption_8h_source.html
This way may be simpler. Good luck.
- 4.8 qtabbar_cpp source code is here. copy line 1569 to 1648 to your paint event. Not sure if this will work.