更改QCheckBox文本颜色 [英] Changing QCheckBox text color
问题描述
我发现了同样的问题:
如何在Qt中更改QCheckBox文本标签颜色?
但是不幸的是,在Mac上没有它适用于我。
在Linux和Windows上,默认情况下,QWidget(QLabel,QCheckBox,QRadioButton)的文本是白色的。
在mac上,它是黑色的。不幸的是这会导致我的屏幕上的问题,因为文本是不可读的(我有黑色背景)。
我已经派生了QCheckBox类,所以在构造函数中: p>
class MPUBLIC MythCheckBox:public QCheckBox
{
Q_OBJECT
public:
MythCheckBox(QWidget * parent = 0,const char * name =MythCheckBox)
:QCheckBox(parent)
{
setObjectName
#ifdef Q_OS_MAC
// setStyleSheet(QCheckBox {color:white;}; QCheckBox :: indicator {color:black;});
QPalette p = palette();
p.setColor(QPalette :: WindowText,Qt :: white);
setPalette(p);
#endif
};
如果我使用样式表:
setStyleSheet(QCheckBox {color:white;}; QCheckBox :: indicator {color:black;});
那么我的文本是白色的,但是复选标记本身不可见...
如果我使用第二种方法:
QPalette p = palette
p.setColor(QPalette :: WindowText,Qt :: white);
setPalette(p);
复选框文本确实变为白色,复选标记本身仍为黑色,看起来像是可行的。
但是如果我把焦点移动到QCheckBox,文本会再次变黑,它将永远保持黑色。
我也试过:
QPalette p = palette();
p.setColor(QPalette :: Active,QPalette :: WindowText,Qt :: white);
p.setColor(QPalette :: Inactive,QPalette :: WindowText,Qt :: white);
setPalette(p);记住你,我只是在mac上得到这个奇怪的行为;如果我在Linux中尝试相同的代码(不同的颜色,如红色),那么一切都像我想要的那样。
任何关于如何改变颜色的想法
解决方案对于一些小部件,你必须禁用原生外观(外观)。例如QPushButton( http://doc.qt.io/qt-4.8/stylesheet) -reference.html )。所以,对于复选框,你可以尝试设置边框。所以复选框的样式如下:
QCheckBox {
border:none;
color:white;
}
适用于我。
I found the same question there:
how to change QCheckBox text label color in Qt?
But unfortunately, none of it work for me on a mac.
On Linux and Windows, by default the text of a QWidget (QLabel, QCheckBox, QRadioButton) is white.
On the mac, it's black. Unfortunately this cause issues in my screen as the text is unreadable (I have black background)..
I have derived the QCheckBox class so in the constructor you get:
class MPUBLIC MythCheckBox: public QCheckBox
{
Q_OBJECT
public:
MythCheckBox(QWidget *parent = 0, const char *name = "MythCheckBox")
: QCheckBox(parent)
{
setObjectName(name);
#ifdef Q_OS_MAC
// setStyleSheet("QCheckBox { color : white; }; QCheckBox::indicator { color:black; }");
QPalette p = palette();
p.setColor(QPalette::WindowText, Qt::white);
setPalette(p);
#endif
};
If I use stylesheets like:
setStyleSheet("QCheckBox { color : white; }; QCheckBox::indicator { color:black; }");
then my text is white as I want, but the checkmark itself becomes invisible...
If I use the 2nd method:
QPalette p = palette();
p.setColor(QPalette::WindowText, Qt::white);
setPalette(p);
The checkbox text does become white, and the checkmark itself is still black, it looks like it works.
But if I ever move the focus to the QCheckBox, the text becomes black again, and it will remain black forever.
I tried also:
QPalette p = palette();
p.setColor(QPalette::Active, QPalette::WindowText, Qt::white);
p.setColor(QPalette::Inactive, QPalette::WindowText, Qt::white);
setPalette(p);
Mind you, I only get this weird behavior on the mac; if I try the same code in Linux (with different colors like red), then everything behaves like I want it to.
Any ideas on how to change the color of a QCheckBox's text, and only the text?
解决方案 For some widgets you have to disable a native look (appearance). For example QPushButton (http://doc.qt.io/qt-4.8/stylesheet-reference.html). So, for checkbox you can try to set borders. So checkbox's style looks like:
QCheckBox {
border: none;
color: white;
}
Works for me.
这篇关于更改QCheckBox文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!