本文整理汇总了C++中QTextCursor::selectionStart方法的典型用法代码示例。如果您正苦于以下问题:C++ QTextCursor::selectionStart方法的具体用法?C++ QTextCursor::selectionStart怎么用?C++ QTextCursor::selectionStart使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QTextCursor
的用法示例。
在下文中一共展示了QTextCursor::selectionStart方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
bool QsvTextOperationsWidget::issue_search( const QString &text, QTextCursor newCursor, QFlags<QTextDocument::FindFlag> findOptions, QLineEdit *l, bool moveCursor )
{
QTextCursor c = m_document->find( text, newCursor, findOptions );
bool found = ! c.isNull();
//lets try again, from the start
if (!found) {
c.movePosition(findOptions.testFlag(QTextDocument::FindBackward)? QTextCursor::End : QTextCursor::Start);
c = m_document->find(text, c, findOptions);
found = ! c.isNull();
}
QPalette p = l->palette();
if (found) {
p.setColor(QPalette::Base, searchFoundColor);
} else {
if (!text.isEmpty())
p.setColor(QPalette::Base, searchNotFoundColor);
else
p.setColor(QPalette::Base,
l->style()->standardPalette().base().color()
);
c = m_searchCursor;
}
l->setPalette(p);
if (moveCursor){
int start = c.selectionStart();
int end = c.selectionEnd();
c.setPosition(end ,QTextCursor::MoveAnchor);
c.setPosition(start,QTextCursor::KeepAnchor);
setTextCursor(c);
}
return found;
}
示例2: displayTooltip
void MainWindow::displayTooltip(QMouseEvent *e)
{
static bool isFirstTime = true;
static int selectionStart;
static QString selectedText;
static bool isExisted = false; //мы выделяли существующее в словаре?
bool isExist = false;
int i;
QPoint p = e->pos();
QTextCursor tc = ui->textEdit->cursorForPosition(p);
tc.select(QTextCursor::WordUnderCursor);
if (selectionStart != tc.selectionStart()) {
//проверяем, есть ли слово в словаре
for(i = 0; i < bptr->getListWords().size(); i++)
{
if(!bptr->getListWords().at(i).word.compare(tc.selectedText(), Qt::CaseInsensitive))
{
isExist = true;
break;
}
if(!bptr->getListWords().at(i).word.compare(selectedText, Qt::CaseInsensitive))
{
isExisted = true;
}
}
if (!isFirstTime)
{
QToolTip::hideText(); //скрываем предыдущий тултип
//Надо удалить выделение предыдущего слова
QTextCursor tc(ui->textEdit->document());
tc.setPosition(selectionStart);
tc.select(QTextCursor::WordUnderCursor);
if(isExisted)
tc.insertHtml(QString("<u>" + selectedText + "</b>"));
else
{
tc.insertHtml(QString(selectedText));
}
isExisted = false;
}
selectionStart = tc.selectionStart();
selectedText = tc.selectedText();
if (tc.selectedText().length() > 0 && isExist)
{
// QToolTip::showText(e->globalPos(), bptr->getListWords().at(i).toString(),
// ui->textEdit, ui->textEdit->cursorRect());
QToolTip::showText(e->globalPos(), bptr->getListWords().at(i).toString(),
ui->textEdit, ui->textEdit->cursorRect(tc));
isFirstTime = false;
tc.insertHtml(QString("<b>" + tc.selectedText() + "</b>"));
}
}
}
示例3: extendSelectionForEnvVar
inline void extendSelectionForEnvVar(QPlainTextEdit * textEdit, QTextCursor selection)
{
if (selection.hasSelection()) {
if (selection.selectedText() == QStringLiteral("~")) {
QTextCursor wordAfter(selection);
wordAfter.movePosition(QTextCursor::NextCharacter);
wordAfter.select(QTextCursor::WordUnderCursor);
if ( wordAfter.hasSelection() && (selection.block() == wordAfter.block()) ) {
selection.setPosition(selection.selectionStart());
selection.setPosition(wordAfter.selectionEnd(), QTextCursor::KeepAnchor);
textEdit->setTextCursor(selection);
}
} else {
int positionBeforeSelection = selection.selectionStart() - 1;
if (positionBeforeSelection >= 0) {
QChar charBeforeSelection = textEdit->document()->characterAt(positionBeforeSelection);
if (charBeforeSelection == QChar('~')) {
QTextCursor extendedSelection = textEdit->textCursor();
extendedSelection.setPosition(positionBeforeSelection);
extendedSelection.setPosition(selection.selectionEnd(), QTextCursor::KeepAnchor);
textEdit->setTextCursor(extendedSelection);
}
}
}
}
}
示例4: deleteTab
void CodeEditor::deleteTab()
{
QString deletion = " ";
QTextCursor cursor = textCursor();
if (cursor.selectionEnd() - cursor.selectionStart() <= 0) {
//delete 4 spaces (tab)
cursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor, deletion.length());
QString selected = cursor.selectedText();
if (selected.startsWith(deletion))
cursor.deletePreviousChar();
} else {
QTextBlock firstBlock = document()->findBlock(cursor.selectionStart());
QTextBlock lastBlock = document()->findBlock(cursor.selectionEnd() - 1);
cursor.setPosition(firstBlock.position());
cursor.beginEditBlock();
do {
if (cursor.block().text().startsWith(deletion)) {
cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, deletion.length());
cursor.removeSelectedText();
}
} while (cursor.movePosition(QTextCursor::NextBlock) && cursor.position() <= lastBlock.position());
cursor.endEditBlock();
}
}
示例5: updateLink
void GolangEdit::updateLink(const QTextCursor &_cursor)
{
QTextCursor cursor = _cursor;
LiteApi::selectWordUnderCursor(cursor);
if (cursor.selectionStart() == cursor.selectionEnd()) {
m_editor->clearLink();
return;
}
if (m_linkCursor.selectionStart() == cursor.selectionStart() &&
m_linkCursor.selectionEnd() == cursor.selectionEnd()) {
if (m_lastLink.hasValidTarget()) {
m_editor->showLink(m_lastLink);
}
return;
}
m_linkCursor = cursor;
m_lastLink = LiteApi::Link();
if (m_findLinkProcess->isRunning()) {
m_findLinkProcess->kill();
m_findLinkProcess->waitForFinished(100);
//return;
}
QString cmd = LiteApi::liteide_stub_cmd(m_liteApp);
QString src = cursor.document()->toPlainText();
m_srcData = src.toUtf8();
int offset = src.left(cursor.selectionStart()).length();
QFileInfo info(m_editor->filePath());
m_findLinkProcess->setEnvironment(LiteApi::getGoEnvironment(m_liteApp).toStringList());
m_findLinkProcess->setWorkingDirectory(info.path());
m_findLinkProcess->startEx(cmd,QString("type -cursor %1:%2 -cursor_stdin -def -info .").
arg(info.fileName()).
arg(offset));
}
示例6: mouseMoveLineNumEvent
void CCodeEdit::mouseMoveLineNumEvent(QMouseEvent* event)
{
QPoint pt(0, event->y());
if( m_lineIconSize.width() < event->x() &&
0 != (event->buttons()& Qt::LeftButton) )
{
QTextCursor cursor = textCursor();
if( cursor.position() == cursor.selectionStart() )
{
// 選択開始位置より前を選択
cursor.setPosition(cursor.selectionEnd());
cursor.movePosition(QTextCursor::EndOfLine);
cursor.setPosition(cursorForPosition(pt).position(), QTextCursor::KeepAnchor);
cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor);
}
else
{
// 選択開始位置より後ろを選択
cursor.setPosition(cursor.selectionStart());
cursor.movePosition(QTextCursor::StartOfLine);
cursor.setPosition(cursorForPosition(pt).position(), QTextCursor::KeepAnchor);
cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor);
}
setTextCursor(cursor);
}
}
示例7: mousePressEvent
void mousePressEvent(QMouseEvent * event)
{
if( event->buttons() != Qt::LeftButton )
return;
if( event->modifiers() == Qt::ShiftModifier )
{
QTextCursor cur = d_codeEditor->textCursor();
const int selStart = d_codeEditor->document()->findBlock( cur.selectionStart() ).blockNumber();
const int selEnd = d_codeEditor->document()->findBlock( cur.selectionEnd() ).blockNumber();
Q_ASSERT( selStart <= selEnd ); // bei position und anchor nicht erfllt
const int clicked = d_codeEditor->lineAt( event->pos() );
if( clicked <= selEnd )
{
if( cur.selectionStart() == cur.position() )
d_codeEditor->selectLines( selEnd, clicked );
else
d_codeEditor->selectLines( selStart, clicked );
}else
d_codeEditor->selectLines( selStart, clicked );
}else
{
// Beginne Drag
d_start = d_codeEditor->lineAt( event->pos() );
d_codeEditor->selectLines( d_start, d_start );
}
}
示例8: cursorPositionChanged
void TextEdit::cursorPositionChanged()
{
//alignmentChanged(textEdit->alignment());
QColor col = Qt::red;
QTextCharFormat fmt;
fmt.setForeground(col);
QTextCursor cursor = textEdit->textCursor();
cout<<"#TextEdit::cursorPositionChanged:";
cout<<"cursor.selectionStart:"<<cursor.selectionStart()<<","<<cursor.selectionEnd();
int selectionStart=cursor.selectionStart(),
selectionEnd=cursor.selectionEnd();
cursor.mergeCharFormat(fmt);
colorChanged(col);
if(!cursor.hasSelection()) return;
QTextBlock block=cursor.block();
int blockStart=block.position();
QTextLayout* layout=cursor.block().layout();
QTextLine layoutLine=layout->lineForTextPosition(selectionStart-blockStart);
cout<<"layout line:";
int lineNumber= layoutLine.lineNumber();
QPoint blockOffset=layout->position().toPoint();
QPoint lineOffset=layoutLine.position().toPoint();
printPoint(blockOffset);
printPoint(lineOffset);
QPoint linePoint(blockOffset.x()+lineOffset.x(),blockOffset.y()+lineOffset.y());
QPoint lineEndPoint=QPoint(linePoint.x()+PAGEWIDTH,linePoint.y());
// cout<<"block:"<<rect.left()<<","<<rect.right()<<":"<<rect.top()<<","<<rect.bottom();
// cout<<"blockstart:"<<blockStart;
// int x=blockPoint.x()+(float)((selectionStart-blockStart)%LINELENGTH)/LINELENGTH*PAGEWIDTH;
// int y=blockPoint.y()+((selectionStart-blockStart)/LINELENGTH+1)*LINEHEIGHT;
// int x1=blockPoint.x()+(float)((selectionEnd-blockStart)%LINELENGTH)/LINELENGTH*PAGEWIDTH;
// cout<<"block position:"<<blockPoint.x()<<","<<blockPoint.y();
// cout<<"selection position:"<<x<<","<<y<<":"<<x1<<","<<y;
//// int y1=blockPoint.y()+((cursor.selectionEnd()-blockStart)/LINELENGTH+1)*15;
// QPoint selectionPoint(x,y);
// QPoint selectionEndPoint(x1,y);
image->paintLine(linePoint,lineEndPoint);
// int lineStart=blockStart,lineEnd;
// for(int i=0;i<block.lineCount();i++){
// QTextLine line = layout->lineAt(i);
// qreal lineWidth=line.width(),lineHeight=line.height();
// lineEnd=lineStart+line.textLength();
// int a=line.textStart(),b=line.textLength()+a;
// cout<<"line.cursorToX:"<<line.cursorToX(selectionStart)<<","<<line.cursorToX(selectionEnd);
// cout<<"line.textstart:"<<a<<","<<b;
// cout<<"line.width:"<<lineWidth<<" height:"<<lineHeight;
// rect=line.naturalTextRect();
// cout<<"line.naturaltextrect:"<<rect.left()<<":"<<rect.top()<<":"<<rect.right()<<":"<<rect.bottom();
// lineStart=lineEnd;
// }
}
示例9: copyUpDown
void GenericCodeEditor::copyUpDown(bool up)
{
// directly taken from qtcreator
// Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
// GNU Lesser General Public License
QTextCursor cursor = textCursor();
QTextCursor move = cursor;
move.beginEditBlock();
bool hasSelection = cursor.hasSelection();
if (hasSelection) {
move.setPosition(cursor.selectionStart());
move.movePosition(QTextCursor::StartOfBlock);
move.setPosition(cursor.selectionEnd(), QTextCursor::KeepAnchor);
move.movePosition(move.atBlockStart() ? QTextCursor::Left: QTextCursor::EndOfBlock,
QTextCursor::KeepAnchor);
} else {
move.movePosition(QTextCursor::StartOfBlock);
move.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
}
QString text = move.selectedText();
if (up) {
move.setPosition(cursor.selectionStart());
move.movePosition(QTextCursor::StartOfBlock);
move.insertBlock();
move.movePosition(QTextCursor::Left);
} else {
move.movePosition(QTextCursor::EndOfBlock);
if (move.atBlockStart()) {
move.movePosition(QTextCursor::NextBlock);
move.insertBlock();
move.movePosition(QTextCursor::Left);
} else {
move.insertBlock();
}
}
int start = move.position();
move.clearSelection();
move.insertText(text);
int end = move.position();
move.setPosition(start);
move.setPosition(end, QTextCursor::KeepAnchor);
move.endEditBlock();
setTextCursor(move);
}
示例10: indent
void CodeEditer::indent(QTextDocument *doc, const QTextCursor &cursor, QChar typedChar)
{
if (cursor.hasSelection()) {
QTextBlock block = doc->findBlock(qMin(cursor.selectionStart(), cursor.selectionEnd()));
const QTextBlock end = doc->findBlock(qMax(cursor.selectionStart(), cursor.selectionEnd())).next();
do {
indentBlock(doc, block, typedChar);
block = block.next();
} while (block.isValid() && block != end);
} else {
indentBlock(doc, cursor.block(), typedChar);
}
}
示例11: getTextDocument
void QsvTextOperationsWidget::on_replaceOldText_returnPressed()
{
if (QApplication::keyboardModifiers().testFlag(Qt::ControlModifier) ||
QApplication::keyboardModifiers().testFlag(Qt::AltModifier) ||
QApplication::keyboardModifiers().testFlag(Qt::ShiftModifier) ) {
on_replaceAll_clicked();
showReplace();
return;
}
QTextCursor c = m_searchCursor;
QTextDocument *doc = getTextDocument();
if (!doc){
qDebug("%s:%d - no document found, using a wrong class? wrong parent?", __FILE__,__LINE__);
return;
}
c = doc->find( replaceFormUi->findText->text(), c, getReplaceFlags() );
if (c.isNull())
return;
int start = c.selectionStart();
int end = c.selectionEnd();
c.beginEditBlock();
c.deleteChar();
c.insertText( replaceFormUi->replaceText->text() );
c.setPosition(start,QTextCursor::KeepAnchor);
c.setPosition(end ,QTextCursor::MoveAnchor);
c.endEditBlock();
setTextCursor( c );
// is there any other apperance of this text?
m_searchCursor = c;
updateReplaceInput();
}
示例12: editUncomment
void TikzEditorView::editUncomment()
{
bool go = true;
QTextCursor textCursor = m_tikzEditor->textCursor();
if (textCursor.hasSelection())
{
textCursor.beginEditBlock();
const int start = textCursor.selectionStart();
int end = textCursor.selectionEnd() - 2;
textCursor.setPosition(start, QTextCursor::MoveAnchor);
textCursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor);
while (textCursor.position() < end && go)
{
textCursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 2);
if (textCursor.selectedText() == QLatin1String("% "))
{
textCursor.removeSelectedText();
--end;
}
go = textCursor.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor);
}
textCursor.endEditBlock();
}
else
{
textCursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor);
textCursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 2);
if (textCursor.selectedText() == QLatin1String("% "))
textCursor.removeSelectedText();
}
}
示例13: disableFormattingSelectedText
void ClangFormat::disableFormattingSelectedText()
{
TextEditor::TextEditorWidget *widget = TextEditor::TextEditorWidget::currentTextEditorWidget();
if (!widget)
return;
const QTextCursor tc = widget->textCursor();
if (!tc.hasSelection())
return;
// Insert start marker
const QTextBlock selectionStartBlock = tc.document()->findBlock(tc.selectionStart());
QTextCursor insertCursor(tc.document());
insertCursor.beginEditBlock();
insertCursor.setPosition(selectionStartBlock.position());
insertCursor.insertText("// clang-format off\n");
const int positionToRestore = tc.position();
// Insert end marker
QTextBlock selectionEndBlock = tc.document()->findBlock(tc.selectionEnd());
insertCursor.setPosition(selectionEndBlock.position() + selectionEndBlock.length() - 1);
insertCursor.insertText("\n// clang-format on");
insertCursor.endEditBlock();
// Reset the cursor position in order to clear the selection.
QTextCursor restoreCursor(tc.document());
restoreCursor.setPosition(positionToRestore);
widget->setTextCursor(restoreCursor);
// The indentation of these markers might be undesired, so reformat.
// This is not optimal because two undo steps will be needed to remove the markers.
const int reformatTextLength = insertCursor.position() - selectionStartBlock.position();
BeautifierPlugin::formatCurrentFile(command(selectionStartBlock.position(),
reformatTextLength));
}
示例14: insert_head
void MarkdownEdit::insert_head(const QString &tag, bool blockStart)
{
QTextCursor cur = m_ed->textCursor();
cur.beginEditBlock();
if (cur.hasSelection()) {
QTextBlock begin = m_ed->document()->findBlock(cur.selectionStart());
QTextBlock end = m_ed->document()->findBlock(cur.selectionEnd());
if (end.position() == cur.selectionEnd()) {
end = end.previous();
}
QTextBlock block = begin;
do {
if (block.text().length() > 0) {
if (blockStart) {
cur.setPosition(block.position());
} else {
QString text = block.text();
foreach(QChar c, text) {
if (!c.isSpace()) {
cur.setPosition(block.position()+text.indexOf(c));
break;
}
}
}
cur.insertText(tag);
}
block = block.next();
} while(block.isValid() && block.position() <= end.position());
} else {
示例15: textUnderCursor
QString CWidgetReturnEmitTextEdit::textUnderCursor() const
{
QTextCursor tc = textCursor();
tc.select(QTextCursor::WordUnderCursor);
// Save selected positions of current word.
int selectionStart = tc.selectionStart();
int selectionEnd = tc.selectionEnd();
// If selection is at beginning of text edit then there can't be a slash to check for
if (selectionStart == 0)
return tc.selectedText();
// Modify selection to include previous character
tc.setPosition(selectionStart - 1, QTextCursor::MoveAnchor);
tc.setPosition(selectionEnd, QTextCursor::KeepAnchor);
// If previous character was / return current selection for command completion
if(tc.selectedText().startsWith('/'))
return tc.selectedText();
else {
// Else restore original selection and return for nick completion
tc.setPosition(selectionStart, QTextCursor::MoveAnchor);
tc.setPosition(selectionEnd, QTextCursor::KeepAnchor);
return tc.selectedText();
}
}