本文整理汇总了C++中QTextCursor::insertText方法的典型用法代码示例。如果您正苦于以下问题:C++ QTextCursor::insertText方法的具体用法?C++ QTextCursor::insertText怎么用?C++ QTextCursor::insertText使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QTextCursor
的用法示例。
在下文中一共展示了QTextCursor::insertText方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: addTag
void LrcEditor::addTag(const QString &attr, const QString &value)
{
// ignore empty value
if (value.isEmpty())
return ;
QRegExp exp("^\\[" + attr + ":.*\\][\\s]{0,}$");
QString newValue = "[" + attr + ":" + value + "]";
bool found = false;
for (auto it = document()->begin(); it != document()->end(); it = it.next())
{
if (exp.indexIn(it.text()) >= 0)
{
found = true;
qDebug() << "found tag at line: " << it.blockNumber() << qPrintable(it.text());
if (it.text().trimmed() != newValue)
{
QTextCursor cursor(it);
cursor.beginEditBlock();
cursor.select(QTextCursor::LineUnderCursor);
cursor.removeSelectedText();
cursor.insertText(newValue);
cursor.endEditBlock();
}
// we assume there's only one
break;
}
}
if (!found)
{
QTextCursor cursor = textCursor();
cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor);
cursor.insertText(newValue + m_charNewLine);
}
}
示例2: replace
FindResult replace(QTextCursor &cursor, QTextDocument const &document,
ReplaceQuery const &query)
{
cursor.setPosition(cursor.selectionStart(), QTextCursor::MoveAnchor);
FindResult result = find(cursor, document, true, query);
if((result == FindResult::Found) && cursor.hasSelection())
{
cursor.beginEditBlock();
int start = cursor.selectionStart();
int length = query.replaceValue.length();
cursor.insertText(query.replaceValue);
cursor.setPosition(start, QTextCursor::MoveAnchor);
cursor.movePosition(QTextCursor::NextCharacter,
QTextCursor::KeepAnchor, length);
cursor.endEditBlock();
}
return result;
}
示例3: JBEAM_AddBeam
/* Add beam, in JBEAM edit widget at text cursor position */
void JBEAM_TextBox::JBEAM_AddBeam()
{
QString beamline;
this->str_addIndent(&beamline, 3);
beamline+= "[\"" + CurrentNodeBeam->TempBeam.Node1Name + '"';
beamline+= ", ";
beamline+= '"' + CurrentNodeBeam->TempBeam.Node2Name + '"';
beamline+=("],\n");
QTextCursor textcursor = this->textCursor();
if(JBEAM_BeamCursor >= 0)
{
textcursor.setPosition(JBEAM_BeamCursor);
}
for(int i=0; i<CurrentNodeBeam->TempBeam.comments.size();i++)
{
JBEAM_AddComment(JBEAM_BeamCursor, CurrentNodeBeam->TempBeam.comments.ReadComment(i));
}
textcursor.insertText(beamline);
this->setTextCursor(textcursor);
}
示例4: keyPressEvent
void CodeEditor::keyPressEvent(QKeyEvent *e)
{
if ((codeCompleter) && codeCompleter->popup()->isVisible() && (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Backtab || e->key() == Qt::Key_Escape|| e->key() == Qt::Key_Return || e->key() == Qt::Key_Tab))
{
e->ignore();
return;
}
else if (e->key() == Qt::Key_Tab)
{
QTextCursor cur = textCursor();
cur.beginEditBlock();
cur.insertText("\t");
cur.endEditBlock();
setTextCursor(cur);
e->ignore();
return;
}
else if (e->key() == Qt::Key_D && (e->modifiers() & Qt::ControlModifier)){
deleteLine();
} else if (e->key() == Qt::Key_Up && (e->modifiers() & Qt::AltModifier)){
copyLineUp();
}else if (e->key() == Qt::Key_Up && (e->modifiers() & Qt::ControlModifier)) {
moveLineUp();
} else if (e->key() == Qt::Key_Down && (e->modifiers() & Qt::AltModifier)){
copyLineDown();
}else if (e->key() == Qt::Key_Down && (e->modifiers() & Qt::ControlModifier)){
moveLineDown();
}else if (e->key() == Qt::Key_Space &&(e->modifiers() & Qt::ControlModifier)){
popupSuggestions();
}else if (e->key() == Qt::Key_3 && (e->modifiers() & Qt::ControlModifier)){
toggleComments();
}else if(e->key() == Qt::Key_Period){
popupSuggestions();
QTextEdit::keyPressEvent(e);
}else
QTextEdit::keyPressEvent(e);
}
示例5: setFolded
void DocBlock::setFolded(bool fold)
{
if (fold == folded) return; //! do nothing
folded = fold;
if (fold)
{
backup = myTextItem->document()->clone();
if (docType == Image)
{
QTextCursor cursor = QTextCursor(myTextItem->document());
cursor.document()->setPlainText(" ");
cursor.insertImage(QImage(":/image.png"));
cursor.insertText(QFileInfo(path).fileName());
}
else
{
QString cue = myTextItem->toPlainText();
int index = cue.indexOf("\n");
cue.truncate(qMin(8, index));
cue.append(" ...");
myTextItem->setPlainText(cue);
myTextItem->setTextInteractionFlags(Qt::TextEditable | Qt::TextSelectableByKeyboard);
}
}
else
{
Q_ASSERT(backup != 0);
myTextItem->setDocument(backup);
if (!docType == Text)
myTextItem->setTextInteractionFlags(Qt::NoTextInteraction);
backup = 0;
}
updateBlock();
}
示例6: keyPressEvent
void CodeEdit::keyPressEvent(QKeyEvent *event)
{
QPlainTextEdit::keyPressEvent(event);
if((event->key() == Qt::Key_Return) || (event->key() == Qt::Key_Enter)) {
if(blockCount() == 1)
return;
QString indent;
const QString prevLine = textCursor().block().previous().text();
for(int i = 0; i < prevLine.size(); ++i) {
if(!prevLine[i].isSpace())
break;
indent += prevLine[i];
}
QTextCursor cursor = textCursor();
cursor.insertText(indent);
setTextCursor(cursor);
}
}
示例7: exportRasterImage
void Matrix::exportRasterImage(const QString& fileName, int quality, int dpi)
{
if (!dpi)
dpi = logicalDpiX();
QImage image = d_matrix_model->renderImage();
int dpm = (int)ceil(100.0/2.54*dpi);
image.setDotsPerMeterX(dpm);
image.setDotsPerMeterY(dpm);
if (fileName.endsWith(".odf")){
QTextDocument *document = new QTextDocument();
QTextCursor cursor = QTextCursor(document);
cursor.movePosition(QTextCursor::End);
cursor.insertText(objectName());
cursor.insertBlock();
cursor.insertImage(image);
QTextDocumentWriter writer(fileName);
writer.write(document);
} else
image.save(fileName, 0, quality);
}
示例8: indentOrUnindent
void PlainTextEditor::indentOrUnindent( bool doIndent )
{
QTextCursor cursor = textCursor();
cursor.beginEditBlock();
// Indent or unindent the selected lines
int pos = cursor.position();
int anchor = cursor.anchor();
int start = qMin( anchor, pos );
int end = qMax( anchor, pos );
QTextDocument* doc = document();
QTextBlock startBlock = doc->findBlock(start);
QTextBlock endBlock = doc->findBlock( end -1 ).next();
for ( QTextBlock block = startBlock; block != endBlock; block = block.next() ) {
QString text = block.text();
if ( doIndent ) {
const int indentPosition = firstNonSpace( text );
cursor.setPosition( block.position() +indentPosition );
cursor.insertText( QString( IndentSize, QLatin1Char( ' ' ) ) );
} else {
const int indentPosition = firstNonSpace( text );
const int targetColumn = indentedColumn( columnAt( text, indentPosition ), false );
cursor.setPosition( block.position() +indentPosition );
cursor.setPosition( block.position() +targetColumn, QTextCursor::KeepAnchor );
cursor.removeSelectedText();
}
}
// Reselect the selected lines
cursor.setPosition( startBlock.position() );
cursor.setPosition( endBlock.previous().position(), QTextCursor::KeepAnchor );
cursor.movePosition( QTextCursor::EndOfBlock, QTextCursor::KeepAnchor );
cursor.endEditBlock();
setTextCursor( cursor );
}
示例9: insertCompletion
void SqlTextEdit::insertCompletion(const QString& completion)
{
if (m_Completer->widget() != this)
return;
QTextCursor tc = textCursor();
int extra = completion.length() - m_Completer->completionPrefix().length();
tc.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor);
// slight workaround for a field completion without any completionPrefix
// eg. "tablename.;" if you would select a field completion and hit enter
// without this workaround the text would be inserted after the ';'
// because endofword moves to the end of the line
// or we have no competionprefix at all (CTRL+SPACE) without text
// under cursor
if(tc.selectedText() == "." || m_Completer->completionPrefix().isEmpty())
tc.movePosition(QTextCursor::Right);
else
tc.movePosition(QTextCursor::EndOfWord);
tc.insertText(completion.right(extra));
setTextCursor(tc);
}
示例10: Replace
//
// Replace text if doreplace is true, otherwise only do "find next"
// Returns true if the find next failed (end of document)
//
bool MibEditor::Replace(bool doreplace)
{
QTextCursor tc;
ff = 0;
find_string = replace_uid.comboFind->currentText();
if (!find_strings.contains(find_string))
find_strings.append(find_string);
replace_string = replace_uid.comboReplace->currentText();
if (!replace_strings.contains(replace_string))
replace_strings.append(replace_string);
if (replace_uid.checkWords->isChecked())
ff |= QTextDocument::FindWholeWords;
if (replace_uid.checkCase->isChecked())
ff |= QTextDocument::FindCaseSensitively;
if (replace_uid.checkBackward->isChecked())
ff |= QTextDocument::FindBackward;
tc = s->MainUI()->MIBFile->textCursor();
if(doreplace && !tc.isNull() && tc.hasSelection() &&
(tc.selectedText().compare(find_string,
(replace_uid.checkCase->isChecked()?
Qt::CaseSensitive:Qt::CaseInsensitive))==0))
tc.insertText(replace_uid.comboReplace->currentText());
tc = s->MainUI()->MIBFile->document()->find(find_string,
s->MainUI()->MIBFile->textCursor(),
ff);
if (!tc.isNull())
{
s->MainUI()->MIBFile->setTextCursor(tc);
tc.select(QTextCursor::WordUnderCursor);
}
return (tc.isNull()?true:false);
}
示例11: addOutputText
void ScriptingWidget::addOutputText(const QString& strMessage, bool error, bool processEvents)
{
if (strMessage.isEmpty() == false)
{
QTextCharFormat newTextFormat = currentCharFormat();
if (error)
{
newTextFormat.setFont(mErrorFont);
newTextFormat.setForeground(QBrush(mErrorColor));
}
else
{
newTextFormat.setFont(mOutputFont);
newTextFormat.setForeground(QBrush(mOutputColor));
}
QTextCursor cursor = textCursor();
cursor.clearSelection();
if (mExecutingCommand)
{
cursor.movePosition(QTextCursor::End);
}
else
{
int pos = std::max(mCommandStartPos - mPrompt.size(), 0);
cursor.setPosition(pos);
mCommandStartPos += strMessage.size();
}
cursor.insertText(strMessage, newTextFormat);
if (mExecutingCommand)
{
setTextCursor(cursor);
}
if (processEvents)
{
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
}
}
}
示例12: replaceWordsInCurrentEditor
void SpellCheckerCore::replaceWordsInCurrentEditor( const WordList& wordsToReplace, const QString& replacementWord )
{
if( wordsToReplace.count() == 0 ) {
Q_ASSERT( wordsToReplace.count() != 0 );
return;
}
if( d->currentEditor == nullptr ) {
Q_ASSERT( d->currentEditor != nullptr );
return;
}
TextEditor::TextEditorWidget* editorWidget = qobject_cast<TextEditor::TextEditorWidget*>( d->currentEditor->widget() );
if( editorWidget == nullptr ) {
Q_ASSERT( editorWidget != nullptr );
return;
}
QTextCursor cursor = editorWidget->textCursor();
/* Iterate the words and replace all one by one */
for( const Word& wordToReplace: wordsToReplace ) {
editorWidget->gotoLine( int32_t( wordToReplace.lineNumber ), int32_t( wordToReplace.columnNumber ) - 1 );
int wordStartPos = editorWidget->textCursor().position();
editorWidget->gotoLine( int32_t( wordToReplace.lineNumber ), int32_t( wordToReplace.columnNumber ) + wordToReplace.length - 1 );
int wordEndPos = editorWidget->textCursor().position();
cursor.beginEditBlock();
cursor.setPosition( wordStartPos );
cursor.setPosition( wordEndPos, QTextCursor::KeepAnchor );
cursor.removeSelectedText();
cursor.insertText( replacementWord );
cursor.endEditBlock();
}
/* If more than one suggestion was replaced, show a notification */
if( wordsToReplace.count() > 1 ) {
Utils::FadingIndicator::showText( editorWidget,
tr( "%1 occurrences replaced." ).arg( wordsToReplace.count() ),
Utils::FadingIndicator::SmallText );
}
}
示例13: keyPressEvent
void SourceEdit::keyPressEvent(QKeyEvent * event)
{
QTextEdit::keyPressEvent(event);
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter)
{
QTextCursor cursor = this->textCursor();
QTextBlock prevBlock = cursor.block().previous();
if (prevBlock.isValid()) {
QString text = prevBlock.text();
QString indentStr("");
for (int n = 0; text[n].isSpace(); n++)
indentStr += text[n];
// increase indent
text = text.trimmed();
QRegExp rx("^(if|else|while|for).*");
if (text.endsWith('{') || rx.exactMatch(text))
indentStr += '\t';
cursor.insertText(indentStr);
}
}
else if (event->key() == Qt::Key_BraceRight)
{
// decrease indent
QTextCursor cursor = this->textCursor();
cursor.movePosition(QTextCursor::Left);
if (cursor.block().text().endsWith("\t}"))
cursor.deletePreviousChar();
}
else if (event->key() == Qt::Key_BraceLeft)
{
// decrease indent
QTextCursor cursor = this->textCursor();
cursor.movePosition(QTextCursor::Left);
if (cursor.block().text().endsWith("\t{"))
cursor.deletePreviousChar();
}
}
示例14: doPrintStdout
void Terminal::doPrintStdout(const QString &message, QColor color)
{
logMessage("Terminal::doPrintStdout()");
if (!message.trimmed().isEmpty())
{
// format
QTextCharFormat format;
format.setForeground(color);
// cursor
QTextCursor cursor = txtOutput->textCursor();
cursor.movePosition(QTextCursor::End);
cursor.beginEditBlock();
cursor.insertText(message, format);
cursor.endEditBlock();
// output
txtOutput->setTextCursor(cursor);
txtOutput->ensureCursorVisible();
QApplication::processEvents();
}
}
示例15: unindent
void CodeEditor::unindent()
{
QTextCursor cur = textCursor();
const int selStart = cur.selectionStart();
const int selEnd = cur.selectionEnd();
Q_ASSERT( selStart <= selEnd );
QTextBlock b = document()->findBlock( selStart );
cur.beginEditBlock();
do
{
const int indent = _indents(b);
if( indent > 0 )
{
cur.setPosition( b.position() );
cur.setPosition( _indentToPos( b, indent ), QTextCursor::KeepAnchor );
cur.insertText( QString( indent - 1, QChar('\t') ) );
}
b = b.next();
}while( b.isValid() && b.position() < selEnd );
cur.endEditBlock();
}