本文整理汇总了C++中QTextCursor::selectedText方法的典型用法代码示例。如果您正苦于以下问题:C++ QTextCursor::selectedText方法的具体用法?C++ QTextCursor::selectedText怎么用?C++ QTextCursor::selectedText使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QTextCursor
的用法示例。
在下文中一共展示了QTextCursor::selectedText方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: textUnderCursor
QString ScriptEditorWidget::textUnderCursor() const
{
QString szWord;
QTextCursor tc = textCursor();
if(tc.atBlockStart())
return QString();
tc.clearSelection();
tc.movePosition(QTextCursor::StartOfWord,QTextCursor::KeepAnchor);
if(tc.atBlockStart())
{
szWord.append(tc.selectedText());
tc.movePosition(QTextCursor::EndOfWord,QTextCursor::KeepAnchor);
szWord.append(tc.selectedText());
if(tc.atBlockEnd()){
return szWord;
}
tc.movePosition(QTextCursor::NextCharacter,QTextCursor::KeepAnchor);
szWord.append(tc.selectedText());
if(szWord.right(1)!=".")
szWord.chop(1);
return szWord;
}
tc.movePosition(QTextCursor::PreviousCharacter,QTextCursor::KeepAnchor);
szWord=tc.selectedText();
if(szWord.left(1)==".")
{
tc.movePosition(QTextCursor::StartOfWord);
tc.movePosition(QTextCursor::PreviousCharacter);
tc.movePosition(QTextCursor::PreviousWord);
tc.movePosition(QTextCursor::EndOfWord,QTextCursor::KeepAnchor,1);
szWord.prepend(tc.selectedText());
} else szWord.remove(0,1);
return szWord;
}
示例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: increaseSelectedTextIndention
/**
* @brief Increases (or decreases) the indention of the selected text (if there is a text selected) in the noteTextEdit
* @return
*/
bool QMarkdownTextEdit::increaseSelectedTextIndention( bool reverse )
{
QTextCursor c = this->textCursor();
QString selectedText = c.selectedText();
if ( selectedText != "" )
{
// we need this strange newline character we are getting in the selected text for newlines
QString newLine = QString::fromUtf8( QByteArray::fromHex( "e280a9" ) );
QString newText;
if ( reverse )
{
// unindent text
newText = selectedText.replace( newLine + "\t", "\n" );
// remove leading \t
newText.replace( QRegularExpression( "^\\t" ), "" );
}
else
{
// indent text
newText = selectedText.replace( newLine, "\n\t" ).prepend( "\t" );
// remove trailing \t
newText.replace( QRegularExpression( "\\t$" ), "" );
}
// insert the new text
c.insertText( newText );
// update the selection to the new text
c.setPosition( c.position() - newText.size(), QTextCursor::KeepAnchor );
this->setTextCursor( c );
return true;
}
// if nothing was selected but we want to reverse the indention check if there is a \t in front or after the cursor and remove it if so
else if ( reverse )
{
int pos = c.position();
// check for \t in front of cursor
c.setPosition( pos - 1, QTextCursor::KeepAnchor );
if ( c.selectedText() != "\t" )
{
// (select to) check for \t after the cursor
c.setPosition( pos );
c.setPosition( pos + 1, QTextCursor::KeepAnchor );
}
if ( c.selectedText() == "\t" )
{
c.removeSelectedText();
}
return true;
}
return false;
}
示例4: preventDoubleSpace
void TextZone::preventDoubleSpace()
{
QTextCursor *tCursor = new QTextCursor(document());
int cursorPos = this->textCursor().position();
QString prevChar;
QString nextChar;
if(this->textCursor().atBlockStart() == false){
do {tCursor->setPosition(cursorPos);
tCursor->movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor,1);
prevChar = tCursor->selectedText();
if(prevChar == " "){
tCursor->removeSelectedText();
cursorPos -= 1;
}
}
while(prevChar == " ");
}
if(this->textCursor().atBlockEnd() == false){
do {tCursor->setPosition(cursorPos);
tCursor->movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor,1);
nextChar = tCursor->selectedText();
if(nextChar == " "){
tCursor->removeSelectedText();
}
}
while(nextChar == " ");
}
this->textCursor().insertText(" ");
}
示例5: replace
void FindDialog::replace()
{
QString text = m_find_string->text();
if (text.isEmpty()) {
return;
}
QTextEdit* document = m_documents->currentDocument()->text();
QTextCursor cursor = document->textCursor();
Qt::CaseSensitivity cs = m_ignore_case->isChecked() ? Qt::CaseInsensitive : Qt::CaseSensitive;
if (!m_regular_expressions->isChecked()) {
if (QString::compare(cursor.selectedText(), text, cs) == 0) {
cursor.insertText(m_replace_string->text());
document->setTextCursor(cursor);
}
} else {
QRegExp regex(text, cs, QRegExp::RegExp2);
QString match = cursor.selectedText();
if (regex.exactMatch(match)) {
match.replace(regex, m_replace_string->text());
cursor.insertText(match);
document->setTextCursor(cursor);
}
}
find();
}
示例6: 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();
}
}
示例7: 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();
}
}
示例8: mousePressEvent
void TCommandLine::mousePressEvent(QMouseEvent* event)
{
if (event->button() == Qt::RightButton) {
QTextCursor c = cursorForPosition(event->pos());
c.select(QTextCursor::WordUnderCursor);
if (!Hunspell_spell(mpHunspell, c.selectedText().toLatin1().data())) {
char** sl;
mHunspellSuggestionNumber = Hunspell_suggest(mpHunspell, &sl, c.selectedText().toLatin1().data());
auto popup = new QMenu(this);
for (int i = 0; i < mHunspellSuggestionNumber; i++) {
QAction* pA;
pA = popup->addAction(sl[i]);
connect(pA, &QAction::triggered, this, &TCommandLine::slot_popupMenu);
}
mpHunspellSuggestionList = sl;
mPopupPosition = event->pos();
popup->popup(event->globalPos());
}
event->accept();
return;
}
QPlainTextEdit::mousePressEvent(event);
}
示例9: showNotepadContextMenu
void Notepad::showNotepadContextMenu(const QPoint &pt)
{
// Set Notepad popup menu
QMenu *menu = ui->notepadTextEdit->createStandardContextMenu();
QTextCursor cur = ui->notepadTextEdit->textCursor();
QAction* first = menu->actions().at(0);
if (cur.hasSelection()) {
// Get selected text
//this->main->add_debug_output("Selected text: " + cur.selectedText());
this->addr = cur.selectedText();
} else {
// Get word under the cursor
cur.select( QTextCursor::WordUnderCursor);
//this->main->add_debug_output("Word: " + cur.selectedText());
this->addr = cur.selectedText();
}
ui->actionDisassmble_bytes->setText( "Disassemble bytes at: " + this->addr);
ui->actionDisassmble_function->setText( "Disassemble function at: " + this->addr);
ui->actionHexdump_bytes->setText( "Hexdump bytes at: " + this->addr);
ui->actionCompact_Hexdump->setText( "Compact Hexdump at: " + this->addr);
ui->actionHexdump_function->setText( "Hexdump function at: " + this->addr);
menu->insertAction(first, ui->actionDisassmble_bytes);
menu->insertAction(first, ui->actionDisassmble_function);
menu->insertAction(first, ui->actionHexdump_bytes);
menu->insertAction(first, ui->actionCompact_Hexdump);
menu->insertAction(first, ui->actionHexdump_function);
menu->insertSeparator(first);
ui->notepadTextEdit->setContextMenuPolicy(Qt::DefaultContextMenu);
menu->exec(ui->notepadTextEdit->mapToGlobal(pt));
delete menu;
ui->notepadTextEdit->setContextMenuPolicy(Qt::CustomContextMenu);
}
示例10: replaceSelection
QString AutoCompleter::replaceSelection(QTextCursor &cursor, const QString &textToInsert) const
{
if (!cursor.hasSelection())
return QString();
if (isQuote(textToInsert) && m_surroundWithQuotes)
return cursor.selectedText() + textToInsert;
if (m_surroundWithBrackets)
return surroundSelectionWithBrackets(textToInsert, cursor.selectedText());
return QString();
}
示例11: reg
QTextDocument *Exporter::prepareTextDoc(QTextDocument *textDoc)
{
QTextDocument *textDocument = textDoc->clone(this);
// textDocument->setDefaultStyleSheet("p, li { white-space: pre-wrap; } p{line-height: 2em; font-family:'Liberation Serif'; font-size:19pt;margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:72px;}");
//cut blank spaces at the begining and end :
QTextCursor *tCursor = new QTextCursor(textDocument);
tCursor->movePosition(QTextCursor::Start, QTextCursor::MoveAnchor,1);
tCursor->movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor,1);
while(tCursor->selectedText() == " "){
tCursor->deleteChar();
tCursor->movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor,1);
}
tCursor->movePosition(QTextCursor::End, QTextCursor::MoveAnchor,1);
tCursor->movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor,1);
while(tCursor->selectedText() == " "){
tCursor->deleteChar();
tCursor->movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor,1);
}
//set text config :
QTextBlockFormat blockFormat;
// blockFormat.setBottomMargin(0);
// blockFormat.setTopMargin(0);
// blockFormat.setTextIndent(72);
blockFormat.setLineHeight(200, QTextBlockFormat::ProportionalHeight);
blockFormat.setAlignment(Qt::AlignJustify);
// QTextCharFormat charFormat;
// charFormat.setFontPointSize(12);
// charFormat.setFontFamily("Courrier");
tCursor->select(QTextCursor::Document);
tCursor->mergeBlockFormat(blockFormat);
// tCursor->mergeBlockCharFormat(charFormat);
QRegExp reg("-qt-paragraph-type:.*;|margin-top:.*;|margin-bottom:.*;|margin-left:.*;|margin-right:.*;|-qt-block-indent:.*;|text-indent:.*;|font-family:.*;|font-size:.*;");
reg.setMinimal(true);
textDocument->setHtml(textDocument->toHtml().remove(reg));
return textDocument;
}
示例12: autoIndent
int Editor::autoIndent()
{
QTextCursor cur = this->textCursor();
if(cur.selectedText().length() > 0) {
return 0;
}
int col = cur.columnNumber();
cur.movePosition(QTextCursor::StartOfLine,QTextCursor::MoveAnchor);
cur.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor,col);
QString text = cur.selectedText();
cur.clearSelection();
int stop = -1;
int indent = -1;
int slcm = text.indexOf("'");
if(slcm > -1) {
stop = slcm;
}
cur.beginEditBlock();
cur.insertBlock();
for(int n = 0; n <= stop || isspace(text[n].toLatin1()); n++) {
if(n == slcm) {
if(text.indexOf("''") > -1)
cur.insertText("''");
else
cur.insertText("'");
}
else {
cur.insertText(" ");
}
}
if(indent > 0) {
for(int n = 0; n < indent; n++) {
cur.insertText(" ");
}
}
this->setTextCursor(cur);
cur.endEditBlock();
return 1;
}
示例13: findDialog
void Editor::findDialog()
{
FindDialog dialog(this);
// Get current selection and set default value.
QTextCursor cursor = currentTextEdit()->textCursor();
QString selection = cursor.selectedText();
if( selection != "" ) {
dialog.setSearchText(selection);
}
int result = dialog.exec();
if( result == QDialog::Accepted ) {
lastSearch = dialog.searchText();
lastSearchOptions = 0;
if( dialog.caseSensitive() ) lastSearchOptions |= QTextDocument::FindCaseSensitively;
if( dialog.wholeWord() ) lastSearchOptions |= QTextDocument::FindWholeWords;
if( dialog.direction() == FindDialog::Direction_Backward ) lastSearchOptions |= QTextDocument::FindBackward;
if( !find(lastSearch, lastSearchOptions) ) {
QMessageBox::information(this, tr("Find"), tr("String '%0' not found").arg(lastSearch));
}
}
}
示例14: handleKeyBackspace
void GenericCodeEditor::handleKeyBackspace(QKeyEvent * event, QTextCursor & textCursor, bool & updateCursor)
{
if (event->modifiers() & Qt::META) {
textCursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::KeepAnchor);
textCursor.removeSelectedText();
} else {
if ( !overwriteMode()
|| (textCursor.positionInBlock() == 0)
|| textCursor.hasSelection() ) {
QPlainTextEdit::keyPressEvent(event);
} else {
// in overwrite mode, backspace should insert a space
textCursor.beginEditBlock();
textCursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor);
QString selectedText = textCursor.selectedText();
if (selectedText == QStringLiteral(" ") ||
selectedText == QStringLiteral("\t") ) {
textCursor.clearSelection();
} else {
textCursor.insertText(QString(QChar(' ')));
textCursor.movePosition(QTextCursor::PreviousCharacter);
}
textCursor.endEditBlock();
}
updateCursor = true;
}
}
示例15: mouseReleaseEvent
void DrugInputEdit::mouseReleaseEvent(QMouseEvent *event) {
Q_UNUSED(event);
QTextCursor tc = textCursor();
tc.select(QTextCursor::LineUnderCursor);
QString name = tc.selectedText();
emit drugNameChanged(name);
}