Нужно сделать подсветку искомого слова., нашел пример Text Finder Example (http://doc.trolltech.com/4.7/uitools-textfinder.html) ...
Все банально просто есть lineEdit в которое вводиться искомое слово , и есть plainTextEdit в котором отображается сохраненный в файле текст....
Вот мой код :
void DemAstPro::on_pushButton_2_find_help_clicked()
{
QString searchString;
searchString = ui->lineEdit_4_find_help->text();
QTextDocument *document = ui->plainTextEdit_help->document();
bool found = false;
if (isFirstTime == false)
document->undo();
if (searchString.isEmpty())
{
QMessageBox::information(this, tr("Поле пошуку пусте"),
tr("Будь-ласка введіть слово для пошуку і натисніть Пошук"));
}
else
{
QTextCursor highlightCursor(document);
QTextCursor cursor(document);
cursor.beginEditBlock();
QTextCharFormat plainFormat(highlightCursor.charFormat());
QTextCharFormat colorFormat = plainFormat;
colorFormat.setBackground(Qt::yellow);
while (!highlightCursor.isNull() && !highlightCursor.atEnd())
{
highlightCursor = document->find(searchString, highlightCursor, QTextDocument::FindWholeWords);
if (!highlightCursor.isNull())
{
found = true;
highlightCursor.movePosition(QTextCursor::WordRight,
QTextCursor::KeepAnchor);
highlightCursor.mergeCharFormat(colorFormat);
}
}
cursor.endEditBlock();
isFirstTime = false;
if (found == false)
{
QMessageBox::information(this, tr("Слово не знайдене"), tr("Вибачте, неможливо знайти слово")); // вот сюда .....
}
}
}
Так нашел вроде че за трабл.... оно не ищет если слово встречается не более одного раза .....(Нашел методом народного тыка ) )//Нет что то не то ... ((
Вопрос .... подскажите пожалуйста .....
Что нужно сделать : при поиске слова оно его подсвечивает , но при поиске второго слова первое остается подсвеченным .... а должно очищаться....
KLIF, твой код не смотрел, но когда то решал такую же задачу. Самому, на самом деле, было лень возиться, выдрал решение из исходников Qt Assistant.
Вот кусок кода
void LogViewPlugin::findNext()
{
find(ui->leSearch->text(), true);
}
void LogViewPlugin::findPrevious()
{
find(ui->leSearch->text(), false);
}
void LogViewPlugin::findCurrentText(const QString &text)
{
find(text, true);
}
void LogViewPlugin::find(const QString &ttf, bool forward)
{
QPalette p = ui->leSearch->palette();
p.setColor(QPalette::Active, QPalette::Base, Qt::white);
bool found = false;
found = findInTextBrowser(ttf, forward);
if (!found && !ttf.isEmpty())
p.setColor(QPalette::Active, QPalette::Base, QColor(255, 102, 102));
ui->leSearch->setPalette(p);
}
bool LogViewPlugin::findInTextBrowser(const QString &ttf, bool forward)
{
QTextDocument *doc = ui->logView->document();
QTextCursor cursor = ui->logView->textCursor();
if (!doc || cursor.isNull())
return false;
QTextDocument::FindFlags options;
if (cursor.hasSelection()) {
cursor.setPosition(forward ? cursor.position() : cursor.anchor(),
QTextCursor::MoveAnchor);
}
if (!forward)
options |= QTextDocument::FindBackward;
if (ui->cbSens->isChecked())
options |= QTextDocument::FindCaseSensitively;
bool found = true;
QTextCursor newCursor = doc->find(ttf, cursor, options);
if (newCursor.isNull()) {
QTextCursor ac(doc);
ac.movePosition(options & QTextDocument::FindBackward
? QTextCursor::End : QTextCursor::Start);
newCursor = doc->find(ttf, ac, options);
if (newCursor.isNull()) {
found = false;
newCursor = cursor;
}
}
ui->logView->setTextCursor(newCursor);
return found;
}
void LogViewPlugin::findNext()
{
find(ui->leSearch->text(), true);
}
void LogViewPlugin::findPrevious()
{
find(ui->leSearch->text(), false);
}
void LogViewPlugin::findCurrentText(const QString &text)
{
find(text, true);
}
void LogViewPlugin::find(const QString &ttf, bool forward)
{
QPalette p = ui->leSearch->palette();
p.setColor(QPalette::Active, QPalette::Base, Qt::white);
bool found = false;
found = findInTextBrowser(ttf, forward);
if (!found && !ttf.isEmpty())
p.setColor(QPalette::Active, QPalette::Base, QColor(255, 102, 102));
ui->leSearch->setPalette(p);
}
bool LogViewPlugin::findInTextBrowser(const QString &ttf, bool forward)
{
QTextDocument *doc = ui->logView->document();
QTextCursor cursor = ui->logView->textCursor();
if (!doc || cursor.isNull())
return false;
QTextDocument::FindFlags options;
if (cursor.hasSelection()) {
cursor.setPosition(forward ? cursor.position() : cursor.anchor(),
QTextCursor::MoveAnchor);
}
if (!forward)
options |= QTextDocument::FindBackward;
if (ui->cbSens->isChecked())
options |= QTextDocument::FindCaseSensitively;
bool found = true;
QTextCursor newCursor = doc->find(ttf, cursor, options);
if (newCursor.isNull()) {
QTextCursor ac(doc);
ac.movePosition(options & QTextDocument::FindBackward
? QTextCursor::End : QTextCursor::Start);
newCursor = doc->find(ttf, ac, options);
if (newCursor.isNull()) {
found = false;
newCursor = cursor;
}
}
ui->logView->setTextCursor(newCursor);
return found;
}
void DemAstPro::on_pushButton_3_find_exm_clicked()
{
QString searchString = ui->lineEdit_5_find_exm->text();
/*QTextDocument */document = ui->plainTextEdit_2_exm->document();
bool found = false;
if (isFirstTime_1 == false)
{
document->undo();
}
if (searchString.isEmpty())
{
QMessageBox::information(this, tr("Поле пошуку пусте"),
tr("Будь-ласка введіть слово для пошуку і натисніть Пошук"));
return;
}
else
{
QTextCursor highlightCursor(document);
QTextCursor cursor(document);
cursor.beginEditBlock();
QTextCharFormat plainFormat(highlightCursor.charFormat());
QTextCharFormat colorFormat = plainFormat;
colorFormat.setBackground(Qt::yellow);
while (!highlightCursor.isNull() && !highlightCursor.atEnd())
{
highlightCursor = document->find(searchString, highlightCursor, QTextDocument::FindWholeWords);
if (!highlightCursor.isNull())
{
found = true;
highlightCursor.movePosition(QTextCursor::WordRight,
QTextCursor::KeepAnchor);
highlightCursor.mergeCharFormat(colorFormat);
}
}
cursor.endEditBlock();
isFirstTime_1 = false;
}
if (found == false)
{
QMessageBox::information(this, tr("Слово не знайдене"),
tr("Вибачте, неможливо знайти слово"));
isFirstTime_1 = true;
return;
}
}
Форум Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)