本文整理汇总了C++中QTextCursor::setPosition方法的典型用法代码示例。如果您正苦于以下问题:C++ QTextCursor::setPosition方法的具体用法?C++ QTextCursor::setPosition怎么用?C++ QTextCursor::setPosition使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QTextCursor
的用法示例。
在下文中一共展示了QTextCursor::setPosition方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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 );
}
}
示例2: findInBlock
static bool findInBlock(QTextDocument *doc, const QTextBlock &block, const QRegExp &expr, int offset,
QTextDocument::FindFlags options, QTextCursor &cursor)
{
QString text = block.text();
if(options & QTextDocument::FindBackward)
text.truncate(offset);
text.replace(QChar::Nbsp, QLatin1Char(' '));
int idx = -1;
while (offset >=0 && offset <= text.length()) {
idx = (options & QTextDocument::FindBackward) ?
expr.lastIndexIn(text, offset) : expr.indexIn(text, offset);
if (idx == -1)
return false;
if (options & QTextDocument::FindWholeWords) {
const int start = idx;
const int end = start + expr.matchedLength();
if ((start != 0 && text.at(start - 1).isLetterOrNumber())
|| (end != text.length() && text.at(end).isLetterOrNumber())) {
//if this is not a whole word, continue the search in the string
offset = (options & QTextDocument::FindBackward) ? idx-1 : end+1;
idx = -1;
continue;
}
}
//we have a hit, return the cursor for that.
break;
}
if (idx == -1)
return false;
cursor = QTextCursor(doc);
cursor.setPosition(block.position() + idx);
cursor.setPosition(cursor.position() + expr.matchedLength(), QTextCursor::KeepAnchor);
return true;
}
示例3: findText
void searchreplace::findText()
{
QString qs = window->te->toPlainText();
QString qtext = searchText->text();
if(qs.contains(searchText->text()))//Search to see if the text exists in the TextArea
{
replace->setEnabled(true);//Enable the replace button
replaceText->setText("");
int posStart, posEnd;
posStart = qs.indexOf(searchText->text(), 0, Qt::CaseSensitive);//Find the starting point of the text searched for
posEnd = posStart + qtext.length();//Find the end point of the text searched for
QTextCursor *cursor = &window->te->textCursor();//Point to the textcursor in the main Assignment2 window
cursor->setPosition(posStart, QTextCursor::MoveAnchor);//Move the cursor to the beginning point of text searched for and drop the anchor
cursor->setPosition(posEnd, QTextCursor::KeepAnchor);//Move the cursor to the end point of the text we searched for
window->activateWindow();//By activiating the Assignment2 window we can view the cursor
this->activateWindow();//We reactivate the search and replace Dialog since we are still using it
}
else
{
replace->setDisabled(true);
replaceText->setText("Search unsuccessful");
}
}
示例4: setBold
void NotepadWin::setBold()
{
QTextCharFormat fmt;
QTextCursor cursor = edit->textCursor();
if(cursor.hasSelection()) {
int selStart = cursor.selectionStart();
int selEnd = cursor.selectionEnd();
int pos = cursor.position();
bool isBold = false;
int i = 0;
for(i = selStart; i <= selEnd; i++) {
cursor.setPosition(i);
if(cursor.charFormat().fontWeight() == QFont::Bold) {
isBold = true;
break;
}
}
cursor.setPosition(pos);
fmt.setFontWeight(isBold ? QFont::Normal : QFont::Bold);
cursor.mergeCharFormat(fmt);
edit->mergeCurrentCharFormat(fmt);
}
}
示例5: setStrike
void NotepadWin::setStrike()
{
QTextCharFormat fmt;
QTextCursor cursor = edit->textCursor();
if(cursor.hasSelection()) {
int selStart = cursor.selectionStart();
int selEnd = cursor.selectionEnd();
int pos = cursor.position();
bool isStrike = false;
int i = 0;
for(i = selStart; i <= selEnd; i++) {
cursor.setPosition(i);
if(cursor.charFormat().fontStrikeOut()) {
isStrike = true;
break;
}
}
cursor.setPosition(pos);
fmt.setFontStrikeOut(isStrike ? false : true);
cursor.mergeCharFormat(fmt);
edit->mergeCurrentCharFormat(fmt);
}
}
示例6: selectControl
void MainWindow::selectControl(int index)
{
if (index < 0)
return;
if (m_codeCache.contains(m_currentControlName)) {
CodeCacheItem &cci = m_codeCache[m_currentControlName];
cci.cursorPosition = ui->plainTextEdit->textCursor().position();
cci.horizontalScrollPosition = ui->plainTextEdit->horizontalScrollBar()->value();
cci.verticalScrollPosition = ui->plainTextEdit->verticalScrollBar()->value();
if (ui->plainTextEdit->document()->isModified())
cci.code = ui->plainTextEdit->toPlainText();
}
const QString name = ui->controlComboBox->itemData(index).toString();
if (!m_codeCache.contains(name)) {
const Style &style = m_styles.at(ui->styleComboBox->currentIndex());
QScopedPointer<QFile> file(new QFile(style.controlFilePath(name)));
if (!file->open(QIODevice::ReadOnly)) {
/// TODO: MessageBox
qWarning("Cannot open file '%s': %s", qPrintable(file->fileName()),
qPrintable(file->errorString()));
return;
}
CodeCacheItem cci;
cci.index = index;
cci.name = name;
cci.filePath = file->fileName();
cci.code = file->readAll();
m_codeCache.insert(name, cci);
}
m_currentControlName = name;
const CodeCacheItem &cci = m_codeCache[name];
// Avoid textChanged() signal, when another file is loaded
ui->plainTextEdit->blockSignals(true);
ui->plainTextEdit->setPlainText(cci.code);
ui->plainTextEdit->blockSignals(false);
QTextCursor textCursor = ui->plainTextEdit->textCursor();
textCursor.setPosition(cci.cursorPosition);
ui->plainTextEdit->setTextCursor(textCursor);
ui->plainTextEdit->horizontalScrollBar()->setValue(cci.horizontalScrollPosition);
ui->plainTextEdit->verticalScrollBar()->setValue(cci.verticalScrollPosition);
m_qmlStyler->setCurrentControl(name);
}
示例7: setContent
void UiEditor::setContent(const QString &content, bool raiseWindow) {
//contents.replace("\t", " ");
quint16 cursorPos = ui->jsEditor->textCursor().position();
quint16 scrollPos = ui->jsEditor->verticalScrollBar()->sliderPosition();
ui->jsEditor->setPlainText(content);
if((!isVisible()) && (raiseWindow)) {
show();
Application::current->getMainWindow()->raise();
}
QTextCursor cursor = ui->jsEditor->textCursor();
cursor.setPosition(cursorPos);
ui->jsEditor->setTextCursor(cursor);
ui->jsEditor->verticalScrollBar()->setSliderPosition(scrollPos);
}
示例8: duplicate_line
/**
* Duplicates the current line and inserts the copy below the line
*/
void HaiQTextEdit::duplicate_line() {
QTextCursor cursor = textCursor();
int pos(cursor.position());
cursor.beginEditBlock();
cursor.clearSelection();
cursor.movePosition(QTextCursor::EndOfLine);
cursor.select(QTextCursor::LineUnderCursor);
QTextDocumentFragment text( cursor.selection() );
cursor.clearSelection();
cursor.insertText( QString(QChar::LineSeparator) );
cursor.insertFragment( text );
cursor.setPosition(pos);
cursor.endEditBlock();
}
示例9: setHtml
void ChatMessageArea::setHtml(const QString& html) {
// Create format with updated line height
QTextBlockFormat format;
format.setLineHeight(CHAT_MESSAGE_LINE_HEIGHT, QTextBlockFormat::ProportionalHeight);
// Possibly a bug in QT, the format won't take effect if `insertHtml` is used first. Inserting a space and deleting
// it after ensures the format is applied.
QTextCursor cursor = textCursor();
cursor.setBlockFormat(format);
cursor.insertText(" ");
cursor.insertHtml(html);
cursor.setPosition(0);
cursor.deleteChar();
}
示例10: delete_current_line
/**
* Deletes the current line
*/
void HaiQTextEdit::delete_current_line() {
QTextCursor cursor = textCursor();
cursor.beginEditBlock();
cursor.clearSelection();
cursor.movePosition(QTextCursor::StartOfLine);
int pos(cursor.position());
cursor.select(QTextCursor::LineUnderCursor);
// remove line (and line feed char)
cursor.removeSelectedText();
cursor.deleteChar();
// goto start of next line
cursor.setPosition(pos);
cursor.endEditBlock();
}
示例11: JBEAM_PasteNodeLine
/* Paste text at cursor position */
void JBEAM_TextBox::JBEAM_PasteNodeLine(QString nodeStr)
{
QString nodeline;
this->str_addIndent(&nodeline, 3);
nodeline += nodeStr + "\n";
QTextCursor textcursor = this->textCursor();
if(JBEAM_NodeCursor >= 0)
{
textcursor.setPosition(JBEAM_NodeCursor);
}
textcursor.insertText(nodeline);
this->setTextCursor(textcursor);
}
示例12: isUnit
bool RtfCssEditor::isUnit() {
QTextCursor tc = textCursor();
int currentPos = tc.position();
tc.select(QTextCursor::LineUnderCursor);
QString line = tc.selectedText();
tc.setPosition(currentPos);
QString value = CssParser::getValue(line);
if (value.isEmpty())
return false; //it end here
bool isNumeric;
int numericValue = value.toInt(&isNumeric,10);
if (isNumeric) {
tc.movePosition(QTextCursor::StartOfLine);
int startposition = tc.position();
tc.setPosition(currentPos);
if (startposition + line.indexOf(value) + value.count() <= currentPos) {
if ((line.indexOf(';') != -1) && (startposition + line.indexOf(';') < currentPos))
return false;
return true;
}
}
return false;
}
示例13: 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();
}
示例14: moveCloseToCursor
//-----------------------------------------------------------------------------
// Function: moveCloseToCursor()
//-----------------------------------------------------------------------------
void TextContentAssistWidget::moveCloseToCursor(int cursorPos)
{
// Save the old cursor.
QTextCursor oldCursor = target_->textCursor();
// Move the cursor to the given position.
QTextCursor cursor = oldCursor;
cursor.setPosition(cursorPos, QTextCursor::MoveAnchor);
target_->setTextCursor(cursor);
// Determine the correct upper-left corner position for the widget.
int parentWidth = parentWidget()->width();
int parentHeight = parentWidget()->height();
// By default, the desired position is below the cursor position.
QPoint pos = target_->mapTo(parentWidget(), target_->cursorRect().bottomLeft()) + QPoint(-10, 10);
// Restrict x coordinate by the screen width.
pos.setX(qMin(qMax(0, pos.x()), parentWidth - width()));
// Check if the tool tip hint is visible.
if (toolTipHintWidget_.isVisible())
{
// Then by default, place the widget above the cursor.
pos.setY(target_->mapTo(parentWidget(), target_->cursorRect().topLeft()).y() - height() - 10);
// Lift it up if the tool tip hint is also above the cursor.
if (pos.y() > toolTipHintWidget_.pos().y() - height())
{
pos.setY(toolTipHintWidget_.pos().y() - height());
}
// If the widget goes over the top edge of the screen, move it below the tool tip hint.
if (pos.y() < 0)
{
pos.setY(toolTipHintWidget_.pos().y() + toolTipHintWidget_.height());
}
}
// Otherwise lift the widget above the cursor only if necessary to keep it fully in view.
else if (pos.y() + height() > parentHeight)
{
pos.setY(target_->mapTo(parentWidget(), target_->cursorRect().topLeft()).y() - height() - 10);
}
// Move the widget to the final position.
move(pos);
// Restore the old cursor.
target_->setTextCursor(oldCursor);
}
示例15: setText
void ExternalEditor::setText(const QString &text)
{
Q_ASSERT(d->cellTool);
if (toPlainText() == text) {
return;
}
// This method is called from the embedded editor. Do not send signals back.
blockSignals(true);
KTextEdit::setPlainText(text);
QTextCursor textCursor = this->textCursor();
textCursor.setPosition(d->cellTool->editor()->cursorPosition());
setTextCursor(textCursor);
blockSignals(false);
}