Text Color for tabs in QTabBar are not changing

Text Color for tabs in QTabBar are not changing

Anonymous
Not applicable
867 Views
0 Replies
Message 1 of 1

Text Color for tabs in QTabBar are not changing

Anonymous
Not applicable

I am trying to check existing tabs in a QTabBar against the list of items within a QMenu. And if the existing tabs contains item(s) that are not found in the QMenu, that said tab is to be highlighted in red.
Basically I am targeting the text color change towards individual tabs. 

 

I am using the `setTabTextColor` found under QTabBar docs, it does not seems to be changing the text color if I execute it in Maya. However if I run the code externally, it does change the tab color to red.

 

And one of the major difference I have noticed between running it in Maya and externally is the initial color that the text is colored. In Maya, it is shown as white, whereas it is black if run externally.

 

Wondering if I am doing something wrong here?

 

This is my code:

import functools

class Example(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Example, self).__init__()
        central_widget = QtGui.QWidget()
        self.setCentralWidget(central_widget)
        vlay = QtGui.QVBoxLayout(central_widget)
        hlay = QtGui.QHBoxLayout()
        vlay.addLayout(hlay)
        vlay.addStretch()

        self.add_button = QtGui.QToolButton()
        self.tab_bar = QtGui.QTabBar(self)
        self.add_button.setIcon(QtGui.QIcon('add.png'))

        self.qmenu = QtGui.QMenu(self.add_button)
        self.add_button.setMenu(self.qmenu)
        self.add_button.setPopupMode(QtGui.QToolButton.InstantPopup)

        self.qmenu.aboutToShow.connect(self.set_menu)

        hlay.addWidget(self.add_button)
        hlay.addWidget(self.tab_bar)

    @QtCore.pyqtSlot()
    def set_menu(self):
        with open('/Desktop/menu_options.txt') as f:
            menu_options = f.read().splitlines()
        self.qmenu.clear()
        self.tabs_precheck()

        for opt in menu_options:
            self.qmenu.addAction(opt, functools.partial(self.set_new_tab, opt))

    def get_all_tabs(self):
        all_existing_tabs = {}
        for index in range(self.tab_bar.count()):
            all_existing_tabs[index] = self.tab_bar.tabText(index)

        return all_existing_tabs

    def set_new_tab(self, opt):
        all_tabs = self.get_all_tabs()

        if not opt in all_tabs.values():
            self.tab_bar.addTab(opt)

    def tabs_precheck(self):
        with open('/Desktop/menu_options.txt') as f:
            qmenu_items = f.read().splitlines()
        if qmenu_items:
            for index in range(self.tab_bar.count()):
                text = self.tab_bar.tabText(index)

                # If running the code externally, uncomment the following and
                # comment out line 60.
                # color = QtCore.Qt.black if text in qmenu_items else QtCore.Qt.red

                color = QtCore.Qt.white if text in qmenu_items else QtCore.Qt.red
                self.tab_bar.setTabTextColor(index, color)

 

 

0 Likes
868 Views
0 Replies
Replies (0)