0

I want to change the font color of a text in an UI, but it's seems that there is nothing in the text command doc to do this. It's possible to change the background color, but nothing about the font itself.

I searched around the internet and found this code to change a button text color using PyQt (source).

import maya.OpenMayaUI as omUI
from PyQt4 import QtGui
import sip
bt = sip.wrapinstance(long(omUI.MQtUtil.findControl(_the_button_name_)), QtGui.QPushButton)
bt.setStyleSheet('QPushButton {color: yellow}')

So, I have two questions:

First, what should I use, instead of QPushButton, to edit the color of a text control, and two, the button color here is changed to 'yellow' and I'd like to change it to a custom color value, is there a way to also do this?

Thanks in advance!

UKDP
  • 226
  • 5
  • 21

1 Answers1

1

when using the setStyleSheet function you are changing the properties of the style with a syntax similar to CSS, so if we want to use a specific color we can pass the values as rgb or hex code as shown below:

pb.setStyleSheet('QPushButton {color: rgb(1, 1, 240)}')
pb.setStyleSheet('QPushButton {color: #0101F0}')

You can also apply to any widget without telling you to set the widget as shown below:

some_widget.setStyleSheet('color: #0101F0')

For text use the following:

lb = sip.wrapinstance(long(omUI.MQtUtil.findControl(_the_label_n‌​ame_)), QtGui.QLabel)
lb.setStyleSheet('color: #0101F0')
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thank you, this answer the second part of my question :) Do you know how to apply this to a text control instead of a button control? – UKDP Sep 05 '17 at 14:22
  • @UKDP You've updated my answer, but I'm a bit confused: what do you call text control, to a QLabel; QLineEdit, QTextEdit? – eyllanesc Sep 05 '17 at 14:27
  • That's the thing... I don't know which widget Maya use to display text in UIs, and I don't even know if there is a way to get that information other than by testing... – UKDP Sep 05 '17 at 14:33
  • Run the following please: `lb = sip.wrapinstance(long(omUI.MQtUtil.findControl(_the_button_name_)), QtGui.QLabel) print(lb)`, Change `_the_button_name_` appropriately to the text id, and tell me what you get please. – eyllanesc Sep 05 '17 at 14:37
  • Perfect! It's a `QLabel` in the end. Thank you! Here is the output: `` – UKDP Sep 05 '17 at 14:48
  • Of course, could you please edit it with the correct widget? For the other people who would look for the same thing. Thanks again – UKDP Sep 05 '17 at 14:50