QT4.8-对QLineEdit执行高亮显示
QT4.8 - Implement highlight to QLineEdit
我正在寻找一种为QLineEdit小部件实现荧光笔的方法。
我正在使用QLineEdit在我的应用程序中存储一个路径变量,我想突出显示环境变量。
类似于以下内容:${MY_ENVVAR}/foo/bar/myfile
实际上,我想要一个类似于QHighligher课程的东西。
-
QSyntaxHighliger
子类 -
编写自己的
highlightBlock()
方法 -
在字符串中检测必须着色的特定文本(例如,可以通过正则表达式
QRegExp
),并使用setFormat()
方法用某种颜色绘制从x到x+n的字符串
有用链接:http://qt-project.org/doc/qt-4.8/qsyntaxhighlighter.html#highlightBlock
我以前从未在QLineEdit
中使用过highliter,所以这可能是不可能的。但我们可以简单地将highl附加到QTextEdit
上。所以我们应该从textEdit创建lineEdit,在web中有很多例子可以做到这一点。
例如(我使用hyde
给出的代码,并添加少量内容。)
文本编辑.h
#ifndef TEXTEDIT_H
#define TEXTEDIT_H
#include <QTextEdit>
#include <QCompleter>
#include <QTextEdit>
#include <QKeyEvent>
#include <QStyleOption>
#include <QApplication>
class TextEdit : public QTextEdit
{
Q_OBJECT
public:
explicit TextEdit(QWidget *parent = 0)
{
setTabChangesFocus(true);
setWordWrapMode(QTextOption::NoWrap);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
setFixedHeight(sizeHint().height());
}
void keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter)
event->ignore();
else
QTextEdit::keyPressEvent(event);
}
QSize sizeHint() const
{
QFontMetrics fm(font());
int h = qMax(fm.height(), 14) + 4;
int w = fm.width(QLatin1Char('x')) * 17 + 4;
QStyleOptionFrameV2 opt;
opt.initFrom(this);
return (style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(w, h).
expandedTo(QApplication::globalStrut()), this));
}
};
#endif // TEXTEDIT_H
用法(在main.cpp中)
#include "textedit.h"
//...
TextEdit *t = new TextEdit;
t->show();
new Highlighter(t->document());
Highlighter构造函数作为示例
Highlighter::Highlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
}
相关文章:
- Qt语法高亮显示单行注释的高亮显示规则示例
- 高亮显示图像
- 禁用鼠标悬停时ListView行的高亮显示
- 在Windows中,如何使用C++或C#在属于另一进程的窗口中获取高亮显示的文本
- Emacs大括号和大括号高亮显示
- 自定义文本框:高亮显示和选择
- 使用c#或cli/c++在visual studio中的winforms中为图形中的数据点着色或高亮显示
- Mac上的选定对象显示背景高亮显示,而不是边框高亮显示
- QT4.8-对QLineEdit执行高亮显示
- 高亮显示网格c++OpenGL的特定部分
- IDE条件块高亮显示
- Qt Creator c++11语法高亮显示通用项目
- Eclipse复制带有语法高亮显示的代码
- c++ Builder XE4 -自动补全和语法错误高亮显示
- 用于c++ 11的Vim语法高亮显示不会混淆其他高亮显示.例如,类/命名空间作用域
- Sublime不会高亮显示c++文件
- Visual c++ express 2010高亮显示
- Visual Studio 2013语法高亮显示停止工作
- 更改QWebView中的高亮显示颜色
- 如何在Vim中从语法高亮显示中排除c++原始字符串文字