Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何从qplaintextedit获取文本颜色?_C++_Qt_Colors_Qplaintextedit - Fatal编程技术网

C++ 如何从qplaintextedit获取文本颜色?

C++ 如何从qplaintextedit获取文本颜色?,c++,qt,colors,qplaintextedit,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.f

我想从纯文本中获取文本颜色。我可以用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

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

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

阅读QPalette文档。它可能是一个不同的颜色角色,这取决于小部件的类型和子类型。对于非活动文本、普通文本等。

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

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

阅读QPalette文档。它可能是一个不同的颜色角色,这取决于小部件的类型和子类型。对于非活动文本、普通文本等。

如果您尝试打印foreground.color,您会得到什么?我总是得到QColorARGB 1、0、0…表示白色。你应该在设置前景色的地方张贴代码。我的文字有颜色!!!我创建了一个编辑器,并为注释、关键字、函数等设置了颜色。我希望在编辑器中选择时获得文本的颜色…如果您尝试打印foreground.color您得到了什么?我总是得到QColorARGB 1,0,0…代表白色。你应该在设置前景色的地方张贴代码。我的文字有颜色!!!我创建了一个编辑器,并为注释、关键字、函数等设置了颜色。我想在编辑器中选择时获得文本的颜色…嗯!谢谢你的回答。但我测试了它,但不起作用…:你能帮我吗?嗯!谢谢你的回答。但我测试了它,但不起作用…:你能帮助我吗?