void Journal::on_calendarWidget_activated(const QDate &date) { ui->textEdit->setText(notes); QString searchString; searchString=date.toString(); QTextDocument *document = ui->textEdit->document(); bool found=false; QTextCursor highlightCursor(document); QTextCursor cursor(document); cursor.beginEditBlock(); QTextCharFormat plainFormat(highlightCursor.charFormat()); QTextCharFormat colorFormat = plainFormat; colorFormat.setForeground(Qt::red); 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(); }
/*! * Clear highlight of the found text. */ void TextEdit::clearFind() { QTextDocument *document = this->document(); QTextCursor highlightCursor(document); QTextCharFormat plainFormat(highlightCursor.charFormat()); plainFormat.setBackground(Qt::white); plainFormat.setForeground(Qt::black); highlightCursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor); highlightCursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor); highlightCursor.setCharFormat(plainFormat); }
void CodeEdit::searchText(const QString &text, bool caseSenstive) { m_searchText = text; //ui->results->setVisible(!m_searchText.isEmpty()); // TODO is this correct? document->undo(); m_startPositions.clear(); if (m_searchText.isEmpty()) { updateResultLabel(-1); return; } QTextCursor highlightCursor(document); QTextCursor cursor(document); cursor.beginEditBlock(); QTextCharFormat plainFormat(highlightCursor.charFormat()); QTextCharFormat colorFormat = plainFormat; colorFormat.setForeground(Qt::red); while (!highlightCursor.isNull() && !highlightCursor.atEnd()) { highlightCursor = document->find(m_searchText, highlightCursor, caseSenstive ? QTextDocument::FindCaseSensitively : QTextDocument::FindFlags()); if (!highlightCursor.isNull()) { highlightCursor.mergeCharFormat(colorFormat); m_startPositions.append(highlightCursor.position() - m_searchText.length()); } } cursor.endEditBlock(); if (!m_startPositions.isEmpty()) { markSearchResult(0); } else { updateResultLabel(-1); } //ui->results->setVisible(!m_startPositions.isEmpty()); }
//! [6] //! [7] void TextFinder::on_findButton_clicked() { QString searchString = ui_lineEdit->text(); QTextDocument *document = ui_textEdit->document(); bool found = false; if (isFirstTime == false) document->undo(); if (searchString.isEmpty()) { QMessageBox::information(this, tr("Empty Search Field"), "The search field is empty. Please enter a word and click Find."); } else { QTextCursor highlightCursor(document); QTextCursor cursor(document); cursor.beginEditBlock(); //! [6] QTextCharFormat plainFormat(highlightCursor.charFormat()); QTextCharFormat colorFormat = plainFormat; colorFormat.setForeground(Qt::red); 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); } } //! [8] cursor.endEditBlock(); //! [7] //! [9] isFirstTime = false; if (found == false) { QMessageBox::information(this, tr("Word Not Found"), "Sorry, the word cannot be found."); } } }
int main(int argc, char *argv[]) { QApplication app(argc, argv); QTextEdit *editor = new QTextEdit(); QTextCursor cursor(editor->textCursor()); cursor.movePosition(QTextCursor::Start); QTextCharFormat plainFormat(cursor.charFormat()); QTextCharFormat headingFormat = plainFormat; headingFormat.setFontWeight(QFont::Bold); headingFormat.setFontPointSize(16); QTextCharFormat emphasisFormat = plainFormat; emphasisFormat.setFontItalic(true); QTextCharFormat qtFormat = plainFormat; qtFormat.setForeground(QColor("#990000")); QTextCharFormat underlineFormat = plainFormat; underlineFormat.setFontUnderline(true); //! [0] cursor.insertText(tr("Character formats"), headingFormat); cursor.insertBlock(); cursor.insertText(tr("Text can be displayed in a variety of " "different character formats. "), plainFormat); cursor.insertText(tr("We can emphasize text by ")); cursor.insertText(tr("making it italic"), emphasisFormat); //! [0] cursor.insertText(tr(", give it a "), plainFormat); cursor.insertText(tr("different color "), qtFormat); cursor.insertText(tr("to the default text color, "), plainFormat); cursor.insertText(tr("underline it"), underlineFormat); cursor.insertText(tr(", and use many other effects."), plainFormat); editor->setWindowTitle(tr("Text Document Character Formats")); editor->resize(320, 480); editor->show(); return app.exec(); }
int main(int argc, char *argv[]) { QApplication app(argc, argv); QTextEdit *editor = new QTextEdit(); QTextCursor cursor(editor->textCursor()); cursor.movePosition(QTextCursor::Start); QTextCharFormat plainFormat(cursor.charFormat()); QTextCharFormat colorFormat = plainFormat; colorFormat.setForeground(Qt::red); cursor.insertText(tr("Text can be displayed in a variety of " "different character " "formats. "), plainFormat); cursor.insertText(tr("We can emphasize text by making it ")); cursor.insertText(tr("italic, give it a different color ")); cursor.insertText(tr("to the default text color, underline it, ")); cursor.insertText(tr("and use many other effects.")); QString searchString = tr("text"); QTextDocument *document = editor->document(); //! [0] QTextCursor newCursor(document); while (!newCursor.isNull() && !newCursor.atEnd()) { newCursor = document->find(searchString, newCursor); if (!newCursor.isNull()) { newCursor.movePosition(QTextCursor::WordRight, QTextCursor::KeepAnchor); newCursor.mergeCharFormat(colorFormat); } //! [0] //! [1] } //! [1] editor->setWindowTitle(tr("Text Document Find")); editor->resize(320, 480); editor->show(); return app.exec(); }
void TextEdit::findButton(const QRegExp searchString) { QTextDocument *document = textEdit->document(); if (isFirstTime == false) document->undo(); highLightRows.clear(); curHighlightRow = -1; if (searchString.isEmpty()) { return; } else { QTextCursor highlightCursor(document); QTextCursor cursor(document); cursor.beginEditBlock(); QTextCharFormat plainFormat(highlightCursor.charFormat()); QTextCharFormat colorFormat = plainFormat; colorFormat.setBackground(Qt::yellow); //qDebug() << searchString.pattern().size(); while (!highlightCursor.isNull() && !highlightCursor.atEnd()) { //highlightCursor = document->find(searchString, highlightCursor, QTextDocument::FindWholeWords); highlightCursor = document->find(searchString, highlightCursor); if (!highlightCursor.isNull()) { //highlightCursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); //highlightCursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, searchString.pattern().size()); //highlightCursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor); highlightCursor.movePosition(QTextCursor::NoMove, QTextCursor::KeepAnchor); highlightCursor.mergeCharFormat(colorFormat); if(!highLightRows.contains(highlightCursor.blockNumber())) highLightRows.append(highlightCursor.blockNumber()); } } cursor.endEditBlock(); isFirstTime = false; if(!highLightRows.isEmpty()) curHighlightRow = highLightRows[0]; select_cur_line(); } }
//search function void DocumentWidget::s_search() { QString searchString=m_searchLine->text(); QTextDocument *document=m_txt->document(); bool found = false; if (isFirstTime == false) document->undo(); if (searchString.isEmpty()) { QMessageBox::information(this, tr("Empty Search Field"), "Please enter a word."); isFirstTime=true; } else { QTextCursor highlightCursor(document); QTextCursor cursor(document); cursor.beginEditBlock(); QTextCharFormat plainFormat(highlightCursor.charFormat()); QTextCharFormat colorFormat = plainFormat; colorFormat.setForeground(Qt::red); 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("Word Not Found"), "Sorry, the word cannot be found."); isFirstTime = true; } } m_searchLine->clear(); }
void Notepad::on_searchEdit_returnPressed() { QString searchString = ui->searchEdit->text(); QTextDocument *document = ui->notepadTextEdit->document(); bool found = false; if (isFirstTime == false) document->undo(); if (!searchString.isEmpty()) { QTextCursor highlightCursor(document); QTextCursor cursor(document); cursor.beginEditBlock(); QTextCharFormat plainFormat(highlightCursor.charFormat()); QTextCharFormat colorFormat = plainFormat; colorFormat.setForeground(Qt::red); 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("Word Not Found"), "Sorry, the word cannot be found."); } */ } }
bool BaseEditor::findAllOccurrance(const QString &text, QTextDocument::FindFlags qff, bool isRE) { QTextDocument *doc = document(); findTextSelection.clear(); bool finded=false; if(text.isEmpty()) { prevFindCursor = QTextCursor(); return finded; } else { QTextEdit::ExtraSelection es; QTextCursor highlightCursor(doc); QTextCharFormat plainFormat(highlightCursor.charFormat()); QTextCharFormat colorFormat = plainFormat; colorFormat.setBackground(Qt::yellow); es.format = colorFormat; QRegExp re(text); while(!highlightCursor.isNull() && !highlightCursor.atEnd()) { highlightCursor = isRE ? doc->find(re, highlightCursor, qff) : doc->find(text, highlightCursor, qff); if(!highlightCursor.isNull()) { finded = true; es.cursor = highlightCursor; findTextSelection.append(es); } } if(!finded) { prevFindCursor = highlightCursor; } return finded; } }
void MainWindow::on_pb_search_clicked() { QTextDocument *document = textBrowser->document(); QString filter = m_search->text(); if (!filter.isEmpty()) { QTextCursor highlightCursor(document); QTextCursor cursor(document); //***************开始*************** cursor.beginEditBlock(); QTextCharFormat plainFormat(highlightCursor.charFormat()); QTextCharFormat colorFormat = plainFormat; colorFormat.setBackground(m_Pallette->search_bg_color); colorFormat.setForeground(m_Pallette->error_fg_color); QFlag flag = 0; if(cb_search_case->isChecked()){ flag = flag | QTextDocument::FindCaseSensitively; } if(cb_wholeWord->isChecked()){ flag = flag | QTextDocument::FindWholeWords; } while (!highlightCursor.isNull() && !highlightCursor.atEnd()) { highlightCursor = document->find(filter, highlightCursor, flag); if (!highlightCursor.isNull()) { highlightCursor.mergeCharFormat(colorFormat); } } cursor.endEditBlock(); //***************结束*************** } }
void Notepad::on_searchEdit_textChanged(const QString &arg1) { QString searchString = ui->searchEdit->text(); QTextDocument *document = ui->notepadTextEdit->document(); bool found = false; if (isFirstTime == false) document->undo(); if (!searchString.isEmpty()) { QTextCursor highlightCursor(document); QTextCursor cursor(document); cursor.beginEditBlock(); QTextCharFormat plainFormat(highlightCursor.charFormat()); QTextCharFormat colorFormat = plainFormat; colorFormat.setForeground(Qt::red); while (!highlightCursor.isNull() && !highlightCursor.atEnd()) { highlightCursor = document->find(searchString, highlightCursor); if (!highlightCursor.isNull()) { found = true; //highlightCursor.movePosition(QTextCursor::WordRight, // QTextCursor::KeepAnchor); highlightCursor.mergeCharFormat(colorFormat); } } cursor.endEditBlock(); isFirstTime = false; } }