当前位置: 首页>>代码示例>>C++>>正文


C++ QTextCursor::selectedText方法代码示例

本文整理汇总了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;
}
开发者ID:philouvb,项目名称:KVIrc,代码行数:35,代码来源:ScriptEditorImplementation.cpp

示例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>"));
        }
    }
}
开发者ID:maharajd,项目名称:BookReader,代码行数:60,代码来源:mainwindow.cpp

示例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;
}
开发者ID:brillywu,项目名称:qmarkdowntextedit,代码行数:64,代码来源:qmarkdowntextedit.cpp

示例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(" ");
}
开发者ID:jwvdveen,项目名称:plume-creator-legacy,代码行数:34,代码来源:textzone.cpp

示例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();
}
开发者ID:barak,项目名称:focuswriter,代码行数:27,代码来源:find_dialog.cpp

示例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();
	}
}
开发者ID:jfmcarreira,项目名称:ktikz-old,代码行数:31,代码来源:tikzeditorview.cpp

示例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();
	}
}
开发者ID:quazaa-development-team,项目名称:quazaa,代码行数:27,代码来源:widgetreturnemittextedit.cpp

示例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);
}
开发者ID:Mudlet,项目名称:Mudlet,代码行数:25,代码来源:TCommandLine.cpp

示例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);
}
开发者ID:probonopd,项目名称:iaito,代码行数:33,代码来源:notepad.cpp

示例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();
}
开发者ID:karakin,项目名称:qt-creator,代码行数:10,代码来源:autocompleter.cpp

示例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;
}
开发者ID:jacquetc,项目名称:plume-creator-legacy,代码行数:55,代码来源:exporter.cpp

示例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;
}
开发者ID:FeodorFitsner,项目名称:PropellerIDE,代码行数:49,代码来源:editor.cpp

示例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));
        }
    }
}
开发者ID:dgu123,项目名称:qshaderedit,代码行数:26,代码来源:editor.cpp

示例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;
    }
}
开发者ID:ARTisERR0R,项目名称:supercollider,代码行数:27,代码来源:editor.cpp

示例15: mouseReleaseEvent

void DrugInputEdit::mouseReleaseEvent(QMouseEvent *event) {
    Q_UNUSED(event);
    QTextCursor tc = textCursor();
    tc.select(QTextCursor::LineUnderCursor);
    QString name = tc.selectedText();
    emit drugNameChanged(name);
}
开发者ID:DesiOtaku,项目名称:QDrugList,代码行数:7,代码来源:druginputedit.cpp


注:本文中的QTextCursor::selectedText方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。