如何从qplaintextedit获取文本颜色?

时间:2012-10-21 08:36:17

标签: c++ qt colors qplaintextedit

我希望从纯文本中获取文本颜色。我可以使用charFormat()获取fontWeight和其他格式但是当我调试前景色时,它被设置为无颜色!!?

请帮助我......

示例代码:

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

1 个答案:

答案 0 :(得分:3)

如果你正在使用Qt4,你必须使用QPalette类。 QPalette为GUI上的不同实体存储不同的颜色(文本颜色,背景等)。它继承自父窗口小部件,但可以为您拥有的每个窗口小部件进行更改。

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

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

相关问题