void flush(void) {
   QTextBlockFormat bf = editor->textCursor().blockFormat();
   bf.setBottomMargin(0);
   editor->textCursor().setBlockFormat(bf);
   editor->append(buffer);
   buffer.clear();        
 }
示例#2
0
void FindDialog::find(bool backwards)
{
	QString text = m_find_string->text();
	if (text.isEmpty()) {
		return;
	}

	QTextDocument::FindFlags flags;
	if (!m_ignore_case->isChecked()) {
		flags |= QTextDocument::FindCaseSensitively;
	}
	if (m_whole_words->isChecked()) {
		flags |= QTextDocument::FindWholeWords;
	}
	if (backwards) {
		flags |= QTextDocument::FindBackward;
	}

	QTextEdit* document = m_documents->currentDocument()->text();
	QTextCursor cursor = document->document()->find(text, document->textCursor(), flags);
	if (cursor.isNull()) {
		cursor = document->textCursor();
		cursor.movePosition(!backwards ? QTextCursor::Start : QTextCursor::End);
		cursor = document->document()->find(text, cursor, flags);
	}

	if (!cursor.isNull()) {
		document->setTextCursor(cursor);
	} else {
		QMessageBox::information(this, tr("Sorry"), tr("Phrase not found."));
	}
}
示例#3
0
void MainWindow::updateTexts() {
    QObject *sender_obj = QObject::sender();
    QTextEdit *sender = dynamic_cast<QTextEdit*>(sender_obj);
    //QTextCursor cursor = sender->textCursor();
    int oldPos(sender->textCursor().selectionStart());


    ui->bit7TextEdit->blockSignals(true);
    ui->ASCIItextEdit->blockSignals(true);
    ui->UCStextEdit->blockSignals(true);
    ui->UTF8textEdit->blockSignals(true);
    ui->textEdit->blockSignals(true);

    ui->bit7TextEdit->setPlainText(  myString.toBit7() );
    //ui->ASCIItextEdit->setPlainText( myString.toASCII() );
    //ui->UCStextEdit->setPlainText(   myString.toUCS() );
    //hui->UTF8textEdit->setPlainText(  myString.toUTF8() );
    ui->textEdit->setPlainText(      myString.toQString() );

    ui->bit7TextEdit->blockSignals(false);
    ui->ASCIItextEdit->blockSignals(false);
    ui->UCStextEdit->blockSignals(false);
    ui->UTF8textEdit->blockSignals(false);
    ui->textEdit->blockSignals(false);
    //updateAllowed = true;

    //sender->setTextCursor(cursor);
    QTextCursor cursor(sender->textCursor());
    cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, oldPos);
    sender->setTextCursor(cursor);
}
示例#4
0
 /*!
    * \class SplitCellCommand
    * \author Anders Fernström
    * \date 2006-04-26
    *
    * \brief Split the cell
    */
  void SplitCellCommand::execute()
  {
    try
    {
      if( document()->getCursor()->currentCell() )
      {
        if( typeid( *document()->getCursor()->currentCell() ) == typeid( TextCell ) ||
          typeid( *document()->getCursor()->currentCell() ) == typeid( InputCell ) )
        {
          // extraxt text
          QTextEdit* editor = document()->getCursor()->currentCell()->textEdit();
          if( editor )
          {
            QTextCursor cursor = editor->textCursor();
            cursor.movePosition( QTextCursor::End, QTextCursor::KeepAnchor );
            QTextDocumentFragment text = cursor.selection();
            cursor.removeSelectedText();

            // add new cell
            if( typeid( *document()->getCursor()->currentCell() ) == typeid( TextCell ) )
            {
              AddCellCommand addcellCommand;
              addcellCommand.setApplication( application() );
              addcellCommand.setDocument( document() );
              addcellCommand.execute();
            }
            else
            {
              // inputcell
              CreateNewCellCommand newcellCommand( "Input" );
              newcellCommand.setApplication( application() );
              newcellCommand.setDocument( document() );
              newcellCommand.execute();
            }

            // add text to new cell
            QTextEdit* newEditor = document()->getCursor()->currentCell()->textEdit();
            QTextCursor newCursor = newEditor->textCursor();
            newCursor.insertFragment( text );
            newCursor.movePosition( QTextCursor::Start );
            newEditor->setTextCursor( newCursor );

            // update document
            document()->setChanged( true );
          }
        }
      }
    }
    catch( exception &e )
    {
      string str = string("SplitCellCommand(), Exception: ") + e.what();
      throw runtime_error( str.c_str() );
    }
  }
示例#5
0
 virtual int overflow(int v = std::char_traits<char>::eof()) {
     if (v == '\n') {
         QTextBlockFormat bf = editor->textCursor().blockFormat();
         bf.setBottomMargin(0);
         editor->textCursor().setBlockFormat(bf);
         editor->append(buffer);
         buffer.clear();
     } else {
         buffer += (char)v;
     }
     return v;
 }
示例#6
0
void FindDialog::replaceAll()
{
	QString text = m_find_string->text();
	if (text.isEmpty()) {
		return;
	}

	QTextDocument::FindFlags flags;
	if (!m_ignore_case->isChecked()) {
		flags |= QTextDocument::FindCaseSensitively;
	}
	if (m_whole_words->isChecked()) {
		flags |= QTextDocument::FindWholeWords;
	}

	// Count instances
	int found = 0;
	QTextEdit* document = m_documents->currentDocument()->text();
	QTextCursor cursor = document->textCursor();
	cursor.movePosition(QTextCursor::Start);
	forever {
		cursor = document->document()->find(text, cursor, flags);
		if (!cursor.isNull()) {
			found++;
		} else {
			break;
		}
	}
	if (found) {
		if (QMessageBox::question(this, tr("Question"), tr("Replace %n instance(s)?", "", found), QMessageBox::Yes | QMessageBox::No) == QMessageBox::No) {
			return;
		}
	} else {
		QMessageBox::information(this, tr("Sorry"), tr("Phrase not found."));
		return;
	}

	// Replace instances
	QTextCursor start_cursor = document->textCursor();
	forever {
		cursor = document->document()->find(text, cursor, flags);
		if (!cursor.isNull()) {
			cursor.insertText(m_replace_string->text());
			document->setTextCursor(cursor);
		} else {
			break;
		}
	}
	document->setTextCursor(start_cursor);
}
示例#7
0
void FindDialog::replaceAll()
{
	QString text = m_find_string->text();
	if (text.isEmpty()) {
		return;
	}
	QRegExp regex(text, !m_ignore_case->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive, QRegExp::RegExp2);

	QTextDocument::FindFlags flags;
	if (!m_ignore_case->isChecked()) {
		flags |= QTextDocument::FindCaseSensitively;
	}
	if (m_whole_words->isChecked() && !m_regular_expressions->isChecked()) {
		flags |= QTextDocument::FindWholeWords;
	}

	// Count instances
	int found = 0;
	QTextEdit* document = m_documents->currentDocument()->text();
	QTextCursor cursor = document->textCursor();
	cursor.movePosition(QTextCursor::Start);
	if (!m_regular_expressions->isChecked()) {
		forever {
			cursor = document->document()->find(text, cursor, flags);
			if (!cursor.isNull()) {
				found++;
			} else {
				break;
			}
		}
	} else {
示例#8
0
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();
}
示例#9
0
QTextCursor QTextEditProto::textCursor() const
{
  QTextEdit *item = qscriptvalue_cast<QTextEdit*>(thisObject());
  if (item)
    return item->textCursor();
  return QTextCursor();
}
示例#10
0
int TextEdit::textCursor(lua_State * L) // const : QTextCursor
{
	QTextEdit* obj = ObjectHelper<QTextEdit>::check( L, 1);
	QTextCursor* res = ValueInstaller2<QTextCursor>::create( L );
	*res = 	obj->textCursor();
	return 1;
}
示例#11
0
void MainWindow::on_insertTitle_clicked()
{
    QTextEdit *pEdit = textEditor.editor;
    QTextCursor cursor = pEdit->textCursor();

    //set alignment center
    pEdit->setAlignment(Qt::AlignHCenter);
    groupCheck(0);

    //insert title
    cursor.insertText("“    ”学院 2011—2012学年第一学期\n");

    //insert paper info
    cursor.insertText("《           》期末考试试卷(  卷) 考核形式(  卷)\n");

    //set alignment left
    pEdit->setAlignment(Qt::AlignLeft);
    groupCheck(2);

    QFont font = pEdit->font();
    font.setBold(true);
    font.setPointSize(16);
    pEdit->setFont(font);
    pEdit->setTextCursor(cursor);
    pEdit->setFocus();
}
// Testing get/set functions
void tst_QTextObject::getSetCheck()
{
    QTextEdit edit;
    QTextFrame obj1(edit.document());
    // QTextFrameLayoutData * QTextFrame::layoutData()
    // void QTextFrame::setLayoutData(QTextFrameLayoutData *)
    QTextFrameLayoutData *var1 = new QTextFrameLayoutData;
    obj1.setLayoutData(var1);
    QCOMPARE(var1, obj1.layoutData());
    obj1.setLayoutData((QTextFrameLayoutData *)0);
    QCOMPARE((QTextFrameLayoutData *)0, obj1.layoutData());
    // delete var1; // No delete, since QTextFrame takes ownership

    QTextBlock obj2 = edit.textCursor().block();
    // QTextBlockUserData * QTextBlock::userData()
    // void QTextBlock::setUserData(QTextBlockUserData *)
    QTextBlockUserData *var2 = new QTextBlockUserData;
    obj2.setUserData(var2);
    QCOMPARE(var2, obj2.userData());
    obj2.setUserData((QTextBlockUserData *)0);
    QCOMPARE((QTextBlockUserData *)0, obj2.userData());

    // int QTextBlock::userState()
    // void QTextBlock::setUserState(int)
    obj2.setUserState(0);
    QCOMPARE(0, obj2.userState());
    obj2.setUserState(INT_MIN);
    QCOMPARE(INT_MIN, obj2.userState());
    obj2.setUserState(INT_MAX);
    QCOMPARE(INT_MAX, obj2.userState());
}
示例#13
0
    void updateBlockSelection()
    {
        QTextEdit *ed = qobject_cast<QTextEdit *>(m_widget);
        if (!ed)
            return;

        requestSetBlockSelection(ed->textCursor());
    }
示例#14
0
void MainWindow::mergeFormatOnWordOrSelection(const QTextCharFormat& format)
{
    QTextEdit *pEdit = textEditor.editor;
    QTextCursor cursor = pEdit->textCursor();
    if (!cursor.hasSelection())
        cursor.select(QTextCursor::WordUnderCursor);
    cursor.mergeCharFormat(format);
    pEdit->mergeCurrentCharFormat(format);
}
示例#15
0
SEXP
qt_qsetCursorPosition_QTextEdit(SEXP x, SEXP pos)
{
    QTextEdit *te = unwrapSmoke(x, QTextEdit);
    QTextCursor tc = te->textCursor();
    tc.setPosition(asInteger(pos));
    te->setTextCursor(tc);
    return R_NilValue;
}
示例#16
0
void VBoxLogSearchPanel::findCurrent (const QString &aSearchString)
{
    mButtonsNextPrev->setEnabled (0, aSearchString.length());
    mButtonsNextPrev->setEnabled (1, aSearchString.length());
    toggleWarning (!aSearchString.length());
    if (aSearchString.length())
        search (true, true);
    else
    {
        QTextEdit *browser = mViewer->currentLogPage();
        if (browser && browser->textCursor().hasSelection())
        {
            QTextCursor cursor = browser->textCursor();
            cursor.setPosition (cursor.anchor());
            browser->setTextCursor (cursor);
        }
    }
}
示例#17
0
void MainWindow::on_insertHtml_clicked()
{
    QTextEdit *pEdit = textEditor.editor;
    QTextCursor cursor = pEdit->textCursor();

    InsertHtmlDialog dlg(this);
    if(dlg.exec() != QDialog::Accepted)
        return;
    cursor.insertHtml(dlg.htmlText());
}
void QWordCompleter::replaceCurrentWord(QString text)
{
    QTextEdit* textEdit = qobject_cast<QTextEdit*>(widget());
    QTextCursor textCursor = textEdit->textCursor();
    textCursor.movePosition(QTextCursor::StartOfWord);
    textCursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
    if (text == "void") text += " " + name + "() {\n\n}";
    else if (text == "if") text += " ( ) {\n\n} else {\n\n}";
    textCursor.insertText(text);
    textEdit->setTextCursor(textCursor);
}
void FormulaEditorHighlighter::handleBrace(uint index)
{
    const Token& token = d->tokens.at(index);

    QTextEdit* textEdit = qobject_cast<QTextEdit*>(parent());
    Q_ASSERT(textEdit);
    int cursorPos = textEdit->textCursor().position();
    int distance = cursorPos - token.pos();
    int opType = token.asOperator();
    bool highlightBrace = false;

    //Check where the cursor is in relation to this left or right parenthesis token.
    //Only one pair of braces should be highlighted at a time, and if the cursor
    //is between two braces, the inner-most pair should be highlighted.

    if (opType == Token::LeftPar) {
        //If cursor is directly to the left of this left brace, highlight it
        if (distance == 1)
            highlightBrace = true;
        else
            //Cursor is directly to the right of this left brace, highlight it unless
            //there is another left brace to the right (in which case that should be highlighted instead as it
            //is the inner-most brace)
            if (distance == 2)
                if ((index == (uint)d->tokens.count() - 1) || (d->tokens.at(index + 1).asOperator() != Token::LeftPar))
                    highlightBrace = true;

    } else {
        //If cursor is directly to the right of this right brace, highlight it
        if (distance == 2)
            highlightBrace = true;
        else
            //Cursor is directly to the left of this right brace, so highlight it unless
            //there is another right brace to the left (in which case that should be highlighted instead as it
            //is the inner-most brace)
            if (distance == 1)
                if ((index == 0) || (d->tokens.at(index - 1).asOperator() != Token::RightPar))
                    highlightBrace = true;
    }

    if (highlightBrace) {
        QFont font = QFont(document()->defaultFont());
        font.setBold(true);
        setFormat(token.pos() + 1, token.text().length(), font);

        int matching = findMatchingBrace(index);

        if (matching != -1) {
            Token matchingBrace = d->tokens.at(matching);
            setFormat(matchingBrace.pos() + 1 , matchingBrace.text().length() , font);
        }
    }
}
void SourceViewer::doHighlightText(const QString &text, bool caseSensitive)
{
	qDebug() << "doHighlightText : text=" << text;

	workingHighlightText_ = true;
	do
	{
		QTextEdit *logTextEdit = getCurrentSourceEdit();
		if(logTextEdit == NULL)
			break;

		int orgValueVert = logTextEdit->verticalScrollBar()->value();
		int orgValueHor = logTextEdit->horizontalScrollBar()->value();
		QTextCursor orgCursor = logTextEdit->textCursor();
		QTextDocument::FindFlags flags = 0x0;
		if(caseSensitive)
			flags |=  QTextDocument::FindCaseSensitively;

		QList<QTextEdit::ExtraSelection> extraSelections;
		logTextEdit->moveCursor(QTextCursor::Start);

		QColor fgColor = QColor("white");
		QColor bgColor = QColor("#C32438");

		while(logTextEdit->find(text, flags))
		{
			QTextEdit::ExtraSelection extra;
			extra.format.setForeground(fgColor);
			extra.format.setBackground(bgColor);
			extra.cursor = logTextEdit->textCursor();
			extraSelections.append(extra);
		}

		logTextEdit->setExtraSelections(extraSelections);
		logTextEdit->setTextCursor(orgCursor);
		logTextEdit->verticalScrollBar()->setValue(orgValueVert);
		logTextEdit->horizontalScrollBar()->setValue(orgValueHor);
	} while(false);
	workingHighlightText_ = false;
}
示例#21
0
文件: qtextutil.cpp 项目: m8a/uim
int
QUimTextUtil::acquireSelectionTextInQTextEdit( enum UTextOrigin origin,
                                               int former_req_len,
                                               int latter_req_len,
                                               char **former, char **latter )
{
    QTextEdit *edit = static_cast<QTextEdit *>( mWidget );
    QTextCursor cursor = edit->textCursor();
    if ( ! cursor.hasSelection() )
        return -1;

    bool cursor_at_beginning = false;
    int current = cursor.position();
    int start = cursor.selectionStart();
    if ( current == start )
        cursor_at_beginning = true;

    QString text = cursor.selectedText();
    int len = text.length();
    int offset;
    if ( origin == UTextOrigin_Beginning ||
         ( origin == UTextOrigin_Cursor && cursor_at_beginning ) ) {
        *former = 0;
        offset = 0;
        if ( latter_req_len >= 0 ) {
            if ( len > latter_req_len )
                offset = len - latter_req_len;
        } else {
            if (! ( ~latter_req_len
                    & ( ~UTextExtent_Line | ~UTextExtent_Full ) ) )
                return -1;
        }
        *latter = strdup( text.left( len - offset ).toUtf8().data() );
    } else if ( origin == UTextOrigin_End ||
                ( origin == UTextOrigin_Cursor && !cursor_at_beginning ) ) {
        offset = 0;
        if ( former_req_len >= 0 ) {
            if ( len > former_req_len )
                offset = len - former_req_len;
        } else {
            if (! ( ~former_req_len
                    & ( ~UTextExtent_Line | ~UTextExtent_Full ) ) )
                return -1;
        }
        *former = strdup( text.mid( offset, len - offset ).toUtf8().data() );
        *latter = 0;
    } else {
        return -1;
    }

    return 0;
}
示例#22
0
bool FindDialog::find() {
    QTextEdit* textEdit = getCurrentTextEdit();
    if (textEdit == NULL) return false;
    int from = textEdit->textCursor().selectionEnd();
    QTextCursor cursor = textEdit->document()->find(ui->findString->text(), from);
    if (!cursor.isNull()) {
        textEdit->setTextCursor(cursor);
        return true;
    }
    else {
        return false;
    }
}
示例#23
0
void MainWindow::on_insertTable_clicked()
{
    InsertTableDialog dlg;
    if(dlg.exec() != QDialog::Accepted)
        return;

    QTextEdit *pEdit = textEditor.editor;
    QTextCursor cursor = pEdit->textCursor();
    QTextTableFormat format;
    format.setCellSpacing(0);
    format.setCellPadding(5);
    cursor.insertTable(dlg.row(), dlg.column(), format);
    pEdit->setFocus();
}
示例#24
0
    void editMenuActivated(QAction *action)
    {
        QWidget* w = qApp->focusWidget();
        QTextEdit* te = qobject_cast<QTextEdit*>(w);
        QLineEdit* le = qobject_cast<QLineEdit*>(w);

        if (action == selectAction) {
            highlighting = this;
            return;
        }
        highlighting = 0;

        if (action == copyAction) {
            if (te) {
                if (te->hasEditFocus()) {
                    te->copy();
                } else {
                    QTextCursor c = te->textCursor();
                    te->selectAll();
                    te->copy();
                    te->setTextCursor(c);   // reset selection
                }
            } else if (le) {
                if (le->hasEditFocus()) {
                    le->copy();
                } else {
                    qApp->clipboard()->setText(le->text());
                }
            }
        } else if (action == pasteAction) {
            // assumes clipboard is not empty if 'Paste' is able to be
            // activated, otherwise the line/textedit might be cleared
            // without pasting anything back into it
            if (te) {
                if (!te->hasEditFocus())
                    te->clear();
                te->paste();
                if (!te->hasEditFocus()) {
                    te->moveCursor(QTextCursor::Start);
                    te->ensureCursorVisible();
                }
            } else if (le) {
                if (!le->hasEditFocus())
                    le->clear();
                le->paste();
                if (!le->hasEditFocus())
                    le->home(false);
            }
        }
    }
QTextCursor	QsvTextOperationsWidget::getTextCursor()
{
	QTextCursor searchCursor;
	QTextEdit *t = qobject_cast<QTextEdit*>(parent());
	if (t) {
		searchCursor = t->textCursor();
	} else {
		QPlainTextEdit *pt = qobject_cast<QPlainTextEdit*>(parent());
		if (pt) {
			searchCursor = pt->textCursor();
		}
	}
	return searchCursor;
}
示例#26
0
文件: qtextutil.cpp 项目: m8a/uim
int
QUimTextUtil::deleteSelectionTextInQTextEdit( enum UTextOrigin origin,
                                              int former_req_len,
                                              int latter_req_len )
{
    QTextEdit *edit = static_cast<QTextEdit *>( mWidget );
    QTextCursor cursor = edit->textCursor();
    if ( ! cursor.hasSelection() )
        return -1;

    bool cursor_at_beginning = false;
    int current = cursor.position();
    int start = cursor.selectionStart();
    if ( current == start )
        cursor_at_beginning = true;

    QString text = cursor.selectedText();
    int len = text.length();
    int end = start + len;
    if ( origin == UTextOrigin_Beginning ||
         ( origin == UTextOrigin_Cursor && cursor_at_beginning ) ) {
        if ( latter_req_len >= 0 ) {
            if ( len > latter_req_len )
                end = start + latter_req_len;
        } else {
            if (! ( ~latter_req_len
                    & ( ~UTextExtent_Line | ~UTextExtent_Full ) ) )
                return -1;
        }
    } else if ( origin == UTextOrigin_End ||
                ( origin == UTextOrigin_Cursor && !cursor_at_beginning ) ) {
        if ( former_req_len >= 0 ) {
            if ( len > former_req_len )
                start = end - former_req_len;
        } else {
            if (! ( ~former_req_len
                    & ( ~UTextExtent_Line | ~UTextExtent_Full ) ) )
                return -1;
        }
    } else {
        return -1;
    }
    cursor.setPosition( start );
    cursor.setPosition( end, QTextCursor::KeepAnchor );
    edit->setTextCursor( cursor );
    cursor.deleteChar();

    return 0;
}
示例#27
0
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 (QString::compare(cursor.selectedText(), text, cs) == 0) {
		cursor.insertText(m_replace_string->text());
		document->setTextCursor(cursor);
	}
	find();
}
示例#28
0
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QTextEdit *editor = new QTextEdit();

    QTextCursor cursor(editor->textCursor());
    cursor.movePosition(QTextCursor::Start); 

    QTextCharFormat plainFormat(cursor.charFormat());

    QTextCharFormat headingFormat = plainFormat;
    headingFormat.setFontWeight(QFont::Bold);
    headingFormat.setFontPointSize(16);

    QTextCharFormat emphasisFormat = plainFormat;
    emphasisFormat.setFontItalic(true);

    QTextCharFormat qtFormat = plainFormat;
    qtFormat.setForeground(QColor("#990000"));

    QTextCharFormat underlineFormat = plainFormat;
    underlineFormat.setFontUnderline(true);

//! [0]
    cursor.insertText(tr("Character formats"),
                      headingFormat);

    cursor.insertBlock();

    cursor.insertText(tr("Text can be displayed in a variety of "
                                  "different character formats. "), plainFormat);
    cursor.insertText(tr("We can emphasize text by "));
    cursor.insertText(tr("making it italic"), emphasisFormat);
//! [0]
    cursor.insertText(tr(", give it a "), plainFormat);
    cursor.insertText(tr("different color "), qtFormat);
    cursor.insertText(tr("to the default text color, "), plainFormat);
    cursor.insertText(tr("underline it"), underlineFormat);
    cursor.insertText(tr(", and use many other effects."), plainFormat);

    editor->setWindowTitle(tr("Text Document Character Formats"));
    editor->resize(320, 480);
    editor->show();
    return app.exec();
}
示例#29
0
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QTextEdit *editor = new QTextEdit();

    QTextCursor cursor(editor->textCursor());
    cursor.movePosition(QTextCursor::Start);

    QTextCharFormat plainFormat(cursor.charFormat());
    QTextCharFormat colorFormat = plainFormat;
    colorFormat.setForeground(Qt::red);

    cursor.insertText(tr("Text can be displayed in a variety of "
                                  "different character "
                                  "formats. "), plainFormat);
    cursor.insertText(tr("We can emphasize text by making it "));
    cursor.insertText(tr("italic, give it a different color "));
    cursor.insertText(tr("to the default text color, underline it, "));
    cursor.insertText(tr("and use many other effects."));

    QString searchString = tr("text");

    QTextDocument *document = editor->document();
//! [0]
    QTextCursor newCursor(document);

    while (!newCursor.isNull() && !newCursor.atEnd()) {
        newCursor = document->find(searchString, newCursor);

        if (!newCursor.isNull()) {
            newCursor.movePosition(QTextCursor::WordRight,
                                   QTextCursor::KeepAnchor);

            newCursor.mergeCharFormat(colorFormat);
        }
//! [0] //! [1]
    }
//! [1]

    editor->setWindowTitle(tr("Text Document Find"));
    editor->resize(320, 480);
    editor->show();
    return app.exec();
}
示例#30
0
void MainWindow::on_insetList_clicked()
{
    QInputDialog dlg;
    QStringList items;
    items << tr("○..., ○..., ○...")
          << tr("●..., ●..., ●...")
          << tr("■..., ■..., ■...")
          << tr("1..., 2..., 3...")
          << tr("a..., b..., c...")
          << tr("A..., B..., C...")
          << tr("i..., ii..., iii...")
          << tr("I..., II..., III...");

    bool ok;
    QString item = QInputDialog::getItem(this, tr("请选择列表样式"),
                                         tr("列表前缀:"), items, 0, false, &ok);
    if (!ok || item.isEmpty())
        return;

    QTextEdit *pEdit = textEditor.editor;
    QTextCursor cursor = pEdit->textCursor();

    QTextListFormat listFormat;
    if(item == tr("○..., ○..., ○..."))
        listFormat.setStyle(QTextListFormat::ListCircle);
    else if(item == tr("●..., ●..., ●..."))
        listFormat.setStyle(QTextListFormat::ListDisc);
    else if(item == tr("■..., ■..., ■..."))
        listFormat.setStyle(QTextListFormat::ListSquare);
    else if(item == tr("1..., 2..., 3..."))
        listFormat.setStyle(QTextListFormat::ListDecimal);
    else if(item == tr("a..., b..., c..."))
        listFormat.setStyle(QTextListFormat::ListLowerAlpha);
    else if(item == tr("A..., B..., C..."))
        listFormat.setStyle(QTextListFormat::ListUpperAlpha);
    else if(item == tr("i..., ii..., iii..."))
        listFormat.setStyle(QTextListFormat::ListLowerRoman);
    else
        listFormat.setStyle(QTextListFormat::ListUpperRoman);

    cursor.insertList(listFormat);
    pEdit->setFocus();
}