Text color of Qt widgets is not working/ incorrect

Hi all, I am having some trouble with setting of the text color with the use of PyQt widgets in Maya.

First, I am getting different text color results when running my script in Maya and ‘outside’ of Maya…
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)
                color = QtCore.Qt.black if text in qmenu_items else QtCore.Qt.red
                self.tab_bar.setTabTextColor(index, color)


if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    w = Example()
    w.show()
    sys.exit(app.exec_())

An example content of the menu_options.txt:

water
tea
coffee
soda

Say if I run the tool externally and

  • added in tabs - water and tea tabs
  • open up text file and remove water from the file contents
  • go back to tool and re-clicking on the button, the text color of the existing water tab will be colored red (where the default color is black if running the tool outside of Maya)

However, when performing the above same process in Maya, one of the obvious issue is that the default text color is white instead of black, even so, the text color of the existing water tab does not change in color, it still remains as white.

Is this a Maya ‘thing’? Or do I need to do something in Maya context for it to reflect the colors correctly?

Any insights for this, anyone?

Hey Teh_Ki, have your tried using the setStyleSheet method?

Hi agune15,

I did tried once, but it is setting towards all the tabs in the QTabBar, instead of towards the individual affected tabs, which is why I used setTabTextColor flag eventually, seeing that it is part of the QTabBar…

Oh, what a shame it didn’t work… Don’t forget to post the solution if you come up with it, it will also clear my doubts :joy:

Can’t tell if the use of setStylesheet does indeed did not works or I may have simply code it wrong.

Even so, wondering if anyone has done or encountered anything similar to this issue of mine before?