2012-10-21 135 views
1

我想从纯文本中获取文本颜色。我可以用charFormat()获得fontWeight和其他格式,但是当我调试前景色时,它被设置为无色!如果您使用的Qt4如何从qplaintextedit获取文本颜色?

QTextCursor c = textCursor(); 
QTextCharFormat result = c.charFormat(); 

if (result.fontWeight() == QFont::Bold) 
    qDebug() << "bold text"; //worked 
else if (result.fontWeight() == QFont::Normal) 
    qDebug() << "normal text"; //worked 

if (result.foreground().color() == Qt::green) 
    qDebug() << "green"; //not worked !! 
else if (result.foreground().color() == Qt::blue) 
    qDebug() << "blue"; //not worked !! 
else 
    qDebug() << "no color !!"; 

TNX

+0

如果您尝试打印'foregroud()。color()',您会得到什么? – alestanis

+0

总是我得到QColor(ARGB 1,0,0,0)... – mgh

+0

这代表白色。你应该在你设置*前景色的地方发布代码。 – alestanis

回答

3

,你必须使用QPalette类:

请帮我....

示例代码。 QPalette为GUI上的不同实体存储不同的颜色(文本颜色,背景等)。它从父窗口小部件继承,但可以针对每个窗口小部件进行更改。

QPlainTextEdit *pteEdit; // your text edit 
QPalette palette = pteEdit->palette(); 
QColor textColor = palette.color(QPalette::WindowText); 

阅读QPalette文档。它可能是一个不同的颜色角色,具体取决于窗口小部件类型和子类型。对于非活动文本,正常文本等。

+0

mmm! thanx为您的答案。但我测试它,并不工作...... :(你能帮我吗? – mgh