[SOLVED] QTextDocumentのテキスト検索
-
テキストの検索,ハイライト表示が欲しくてコード書いてみました。
こんなカンジでいいんでしょうか。
findtext.cpp ※※※追記:このコードよりtakumiasaki氏のコメントのコードを採用して下さい。※※※
@#include <QtGui>
const char *browseText = "<FONT color = blue>※女性からみた男のダメ姿ランキング トップ10<BR>"
"1位・飲食店などで、店員に偉そうにする(態度がでかい)<BR>"
"2位・割り勘で10円台まできっちり請求してくる<BR>"
"3位・蛾やゴキブリが出現したときに大騒ぎする<BR>"
"4位・食後に爪楊枝で思いっきり歯の掃除をする<BR>"
"5位・オフィスの机の上がフィギュアだらけ<BR>"
"6位・下着代わりりの柄モノTシャツが、ワイシャツの下から透けている<BR>"
"7位・車の駐車が下手<BR>"
"8位・職場ではスーツ姿が決まっているのに、私服がイマイチ<BR>"
"9位・電車の中で携帯ゲームに夢中になっている<BR>"
"10位・おしぼりで顔を拭く<BR></FONT>"
"<BR>"
"<B>170 :名無しさん@七周年 :2006/11/24(金) 02:04:41 ID:/5Mwkhru0</B><BR>"
"<FONT color = red>※ワイシャツの下から大騒ぎするランキング トップ10<BR>"
"1位・職場で、フィギュアに偉そうにする(携帯ゲームがでかい)<BR>"
"2位・下着代わりの10円、Tシャツがおしぼり<BR>"
"3位・電車の中で蛾やゴキブリが柄モノスーツ姿が決まっているのに<BR>"
"4位・歯で思いっきり車の掃除をするが下手<BR>"
"5位・オフィスが職場で机の上が台<BR>"
"6位・代わりの店員がきっちりTシャツ、態度が透けている<BR>"
"7位・車の駐車車の車の駐車が電車<BR>"
"8位・フィギュアの態度がイマイチ<BR>"
"9位・職場では下着に夢中になっている<BR>"
"10位・ゴキブリで顔を拭く</FONT>";typedef QMap<QTextCursor, QTextCharFormat> SelectedMap;
class MainWindow : public QDialog
{
Q_OBJECT
public:
MainWindow()
: browser(new QTextBrowser)
{
QLineEdit *line = new QLineEdit;
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(line);
layout->addWidget(browser);
setLayout(layout);
connect(line, SIGNAL(textChanged(QString)), SLOT(search(QString)));
resize(600,300);
// ハイライト表示の設定
highlightFormat.setForeground(QBrush(Qt::red));
highlightFormat.setBackground(QBrush(Qt::yellow));// テキトー browser->setHtml(browseText); QFont font = this->font(); font.setPointSize(14); browser->setFont(font); line->setPlaceholderText("検索する文字列を入力して下さい"); }
private slots:
void search(QString text)
{
// ハイライト表示を元に戻す
for (SelectedMap::iterator i = selected.begin()
; i != selected.end(); ++i)
{
QTextCursor(i.key()).setCharFormat(*i);
}
selected.clear();// 検索 QTextDocument *document = browser->document(); QTextCursor cursor(document); while (true) { cursor = document->find(text, cursor); if (cursor.position() == -1) return; // 元の表示形式を保存してハイライト表示 selected.insert(cursor, cursor.charFormat()); cursor.mergeCharFormat(highlightFormat); } }
private:
QTextBrowser *browser;
SelectedMap selected;
QTextCharFormat highlightFormat;
};int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());
MainWindow w;
w.show();
return a.exec();
}#include "findtext.moc"
@
-
[[Doc:QSyntaxHighlighter]] というそのために作られたクラスがありますので、それを使います。
参考: "Syntax Highlighting in QTextEdit":http://doc.qt.nokia.com/qq/qq21-syntaxhighlighter.html@
#include <QtGui>const char *browseText = "<FONT color = blue>※女性からみた男のダメ姿ランキング トップ10<BR>"
"1位・飲食店などで、店員に偉そうにする(態度がでかい)<BR>"
"2位・割り勘で10円台まできっちり請求してくる<BR>"
"3位・蛾やゴキブリが出現したときに大騒ぎする<BR>"
"4位・食後に爪楊枝で思いっきり歯の掃除をする<BR>"
"5位・オフィスの机の上がフィギュアだらけ<BR>"
"6位・下着代わりりの柄モノTシャツが、ワイシャツの下から透けている<BR>"
"7位・車の駐車が下手<BR>"
"8位・職場ではスーツ姿が決まっているのに、私服がイマイチ<BR>"
"9位・電車の中で携帯ゲームに夢中になっている<BR>"
"10位・おしぼりで顔を拭く<BR></FONT>"
"<BR>"
"<B>170 :名無しさん@七周年 :2006/11/24(金) 02:04:41 ID:/5Mwkhru0</B><BR>"
"<FONT color = red>※ワイシャツの下から大騒ぎするランキング トップ10<BR>"
"1位・職場で、フィギュアに偉そうにする(携帯ゲームがでかい)<BR>"
"2位・下着代わりの10円、Tシャツがおしぼり<BR>"
"3位・電車の中で蛾やゴキブリが柄モノスーツ姿が決まっているのに<BR>"
"4位・歯で思いっきり車の掃除をするが下手<BR>"
"5位・オフィスが職場で机の上が台<BR>"
"6位・代わりの店員がきっちりTシャツ、態度が透けている<BR>"
"7位・車の駐車車の車の駐車が電車<BR>"
"8位・フィギュアの態度がイマイチ<BR>"
"9位・職場では下着に夢中になっている<BR>"
"10位・ゴキブリで顔を拭く</FONT>";class SearchHighLighter : public QSyntaxHighlighter
{
Q_OBJECT
public:
explicit SearchHighLighter(QObject *parent = 0) :
QSyntaxHighlighter(parent)
{
init();
}explicit SearchHighLighter(QTextDocument *document) : QSyntaxHighlighter(document) { init(); } void init() { highlightFormat.setForeground(QBrush(Qt::red)); highlightFormat.setBackground(QBrush(Qt::yellow)); }
protected:
void highlightBlock(const QString &text)
{
if (mSearchText.isEmpty())
return;int index; int from = 0; while ((index = text.indexOf(mSearchText, from)) >= 0) { setFormat(index, mSearchText.length(), highlightFormat); from = index + mSearchText.length(); } }
public slots:
void setSearchText(const QString &text)
{
mSearchText = text;
}private:
QString mSearchText;
QTextCharFormat highlightFormat;
};class MainWindow : public QDialog
{
Q_OBJECT
public:
MainWindow()
: browser(new QTextBrowser), highlighter(new SearchHighLighter(this))
{
QLineEdit *line = new QLineEdit;
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(line);
layout->addWidget(browser);
setLayout(layout);
connect(line, SIGNAL(textChanged(QString)), SLOT(search(QString)));
resize(600,300);// テキトー browser->setHtml(browseText); // ハイライト表示の設定 highlighter->setDocument(browser->document()); QFont font = this->font(); font.setPointSize(14); browser->setFont(font); line->setPlaceholderText("検索する文字列を入力して下さい"); }
private slots:
void search(QString text)
{
highlighter->setSearchText(text);
highlighter->rehighlight();
}private:
QTextBrowser *browser;
SearchHighLighter *highlighter;
};int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());
MainWindow w;
w.show();
return a.exec();
}#include "findtext.moc"
@ -
exampleも有ったのね。。。
http://qt-project.org/doc/qt-4.8/richtext-syntaxhighlighter.html