本文整理汇总了C++中QTextCursor类的典型用法代码示例。如果您正苦于以下问题:C++ QTextCursor类的具体用法?C++ QTextCursor怎么用?C++ QTextCursor使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了QTextCursor类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: date
//! [5]
void MainWindow::insertCalendar()
{
editor->clear();
QTextCursor cursor = editor->textCursor();
cursor.beginEditBlock();
QDate date(selectedDate.year(), selectedDate.month(), 1);
//! [5]
//! [6]
QTextTableFormat tableFormat;
tableFormat.setAlignment(Qt::AlignHCenter);
tableFormat.setBackground(QColor("#e0e0e0"));
tableFormat.setCellPadding(2);
tableFormat.setCellSpacing(4);
//! [6] //! [7]
QVector<QTextLength> constraints;
constraints << QTextLength(QTextLength::PercentageLength, 14)
<< QTextLength(QTextLength::PercentageLength, 14)
<< QTextLength(QTextLength::PercentageLength, 14)
<< QTextLength(QTextLength::PercentageLength, 14)
<< QTextLength(QTextLength::PercentageLength, 14)
<< QTextLength(QTextLength::PercentageLength, 14)
<< QTextLength(QTextLength::PercentageLength, 14);
tableFormat.setColumnWidthConstraints(constraints);
//! [7]
//! [8]
QTextTable *table = cursor.insertTable(1, 7, tableFormat);
//! [8]
//! [9]
QTextFrame *frame = cursor.currentFrame();
QTextFrameFormat frameFormat = frame->frameFormat();
frameFormat.setBorder(1);
frame->setFrameFormat(frameFormat);
//! [9]
//! [10]
QTextCharFormat format = cursor.charFormat();
format.setFontPointSize(fontSize);
QTextCharFormat boldFormat = format;
boldFormat.setFontWeight(QFont::Bold);
QTextCharFormat highlightedFormat = boldFormat;
highlightedFormat.setBackground(Qt::yellow);
//! [10]
//! [11]
for (int weekDay = 1; weekDay <= 7; ++weekDay) {
QTextTableCell cell = table->cellAt(0, weekDay-1);
//! [11] //! [12]
QTextCursor cellCursor = cell.firstCursorPosition();
cellCursor.insertText(QString("%1").arg(QDate::longDayName(weekDay)),
boldFormat);
}
//! [12]
//! [13]
table->insertRows(table->rows(), 1);
//! [13]
while (date.month() == selectedDate.month()) {
int weekDay = date.dayOfWeek();
QTextTableCell cell = table->cellAt(table->rows()-1, weekDay-1);
QTextCursor cellCursor = cell.firstCursorPosition();
if (date == QDate::currentDate())
cellCursor.insertText(QString("%1").arg(date.day()), highlightedFormat);
else
cellCursor.insertText(QString("%1").arg(date.day()), format);
date = date.addDays(1);
if (weekDay == 7 && date.month() == selectedDate.month())
table->insertRows(table->rows(), 1);
}
cursor.endEditBlock();
//! [14]
setWindowTitle(tr("Calendar for %1 %2"
).arg(QDate::longMonthName(selectedDate.month())
).arg(selectedDate.year()));
}
示例2: textCursor
QString LiteEditorWidget::wordUnderCursor() const
{
QTextCursor tc = textCursor();
tc.select(QTextCursor::WordUnderCursor);
return tc.selectedText();
}
示例3: getSelFormat
//Returning format of selected text
QTextCharFormat HtmlNote::getSelFormat() const
{
QTextCursor cursor = text_edit->textCursor();
return cursor.charFormat();
}
示例4: onSelectionChanged
void NotepadWin::onSelectionChanged()
{
QTextCharFormat fmt;
QTextCursor cursor = edit->textCursor();
bool isBold = false;
bool isItalic = false;
bool isUnderline = false;
bool isStrike = false;
bool aLeft = false;
bool aCenter = false;
bool aRight = false;
bool aJustify = false;
if(cursor.hasSelection()) {
int selStart = cursor.selectionStart();
int selEnd = cursor.selectionEnd();
int pos = cursor.position();
int i = 0;
for(i = selStart; i <= selEnd; i++) {
cursor.setPosition(i);
if(cursor.charFormat().fontWeight() == QFont::Bold)
isBold = true;
if(cursor.charFormat().fontItalic())
isItalic = true;
if(cursor.charFormat().fontUnderline())
isUnderline = true;
if(cursor.charFormat().fontStrikeOut())
isStrike = true;
if(edit->alignment() == Qt::AlignLeft)
aLeft = true;
else if(edit->alignment() == Qt::AlignCenter)
aCenter = true;
else if(edit->alignment() == Qt::AlignRight)
aRight = true;
else if(edit->alignment() == Qt::AlignJustify)
aJustify = true;
}
cursor.setPosition(pos);
}
else {
if(cursor.charFormat().fontWeight() == QFont::Bold)
isBold = true;
if(cursor.charFormat().fontItalic())
isItalic = true;
if(cursor.charFormat().fontUnderline())
isUnderline = true;
if(cursor.charFormat().fontStrikeOut())
isStrike = true;
if(edit->alignment() == Qt::AlignLeft)
aLeft = true;
else if(edit->alignment() == Qt::AlignCenter)
aCenter = true;
else if(edit->alignment() == Qt::AlignRight)
aRight = true;
else if(edit->alignment() == Qt::AlignJustify)
aJustify = true;
}
bold->setChecked(isBold ? true : false);
italic->setChecked(isItalic ? true : false);
underline->setChecked(isUnderline ? true : false);
strikethrough->setChecked(isStrike ? true : false);
leftSided->setChecked(aLeft ? true : false);
centered->setChecked(aCenter ? true : false);
rightSided->setChecked(aRight ? true : false);
justified->setChecked(aJustify ? true : false);
}
示例5: switch
void LiteEditorWidget::keyPressEvent(QKeyEvent *e)
{
if (!m_completer) {
LiteEditorWidgetBase::keyPressEvent(e);
return;
}
if (m_inputCursorOffset > 0) {
m_completer->hidePopup();
LiteEditorWidgetBase::keyPressEvent(e);
return;
}
if (m_completer->popup()->isVisible()) {
// The following keys are forwarded by the completer to the widget
switch (e->key()) {
case Qt::Key_Enter:
case Qt::Key_Return:
case Qt::Key_Escape:
case Qt::Key_Tab:
case Qt::Key_Backtab:
case Qt::Key_Shift:
e->ignore();
return; // let the completer do default behavior
case Qt::Key_N:
case Qt::Key_P:
if (e->modifiers() == Qt::ControlModifier) {
e->ignore();
return;
}
default:
break;
}
}
bool isInImport = false;
if (m_textLexer->isInStringOrComment(this->textCursor())) {
isInImport = m_textLexer->isInImport(this->textCursor());
if (!isInImport) {
LiteEditorWidgetBase::keyPressEvent(e);
m_completer->hidePopup();
return;
}
}
LiteEditorWidgetBase::keyPressEvent(e);
const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
//always break if ctrl is pressed and there's a key
// if (((e->modifiers() & Qt::ControlModifier) && !e->text().isEmpty())) {
// return;
// }
if (e->modifiers() & Qt::ControlModifier) {
if (!e->text().isEmpty()) {
m_completer->hidePopup();
}
return;
}
if (e->key() == Qt::Key_Tab || e->key() == Qt::Key_Backtab) {
return;
}
if (e->text().isEmpty()) {
if (e->key() != Qt::Key_Backspace) {
m_completer->hidePopup();
return;
}
}
//import line
if (isInImport) {
QString completionPrefix = importUnderCursor(textCursor());
if (completionPrefix.isEmpty()) {
return;
}
m_completer->setCompletionContext(LiteApi::CompleterImportContext);
m_completer->setCompletionPrefix("");
m_completer->startCompleter(completionPrefix);
return;
}
//static QString eow("[email protected]#$%^&*()+{}|:\"<>?,/;'[]\\-="); // end of word
static QString eow("[email protected]#$%^&*()+{}|\"<>?,/;'[]\\-="); // end of word
bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift;
QString completionPrefix = textUnderCursor(textCursor());
if (completionPrefix.startsWith("...")) {
completionPrefix = completionPrefix.mid(3);
} else if (completionPrefix.startsWith(".")) {
completionPrefix.insert(0,'@');
}
if (hasModifier || e->text().isEmpty()||
( completionPrefix.length() < m_completionPrefixMin && completionPrefix.right(1) != ".")
|| eow.contains(e->text().right(1))) {
if (m_completer->popup()->isVisible()) {
m_completer->popup()->hide();
//fmt.Print( -> Print
if (e->text() == "(") {
QTextCursor cur = textCursor();
cur.movePosition(QTextCursor::Left);
//.........这里部分代码省略.........
示例6: slotDisplayErr
/*!
* \brief Insert process stderr output in the Error Message output window.
*
* Called when the process sends an output to stderr.
*/
void SimMessage::slotDisplayErr()
{
QTextCursor cursor = ErrText->textCursor();
cursor.movePosition(QTextCursor::End);
ErrText->insertPlainText(QString(SimProcess.readAllStandardError()));
}
示例7: if
void QTerminal::keyPressEvent(QKeyEvent * event) {
int key = event->key();
this->setTextCursor(curCursorLoc);
// Keep QTextEdit in sync with shell as much as possible...
if (key != Qt::Key_Backspace) {
if (key == Qt::Key_Return || key == Qt::Key_Enter) {
inputCharCount = 0;
} else if (key == Qt::Key_Up) {
if (cmdHistory.size()) {
if (histLocation == -1) {
histLocation = cmdHistory.size() - 1;
tempCmd = cmdStr;
} else if (histLocation == 0) {
QApplication::beep();
event->ignore();
return;
} else {
--histLocation;
}
for (int i = 0; i < inputCharCount; ++i) {
QTextEdit::keyPressEvent(new QKeyEvent(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier));
}
inputCharCount = cmdHistory.at(histLocation).length();
this->insertPlainText(cmdHistory.at(histLocation));
cmdStr = cmdHistory.at(histLocation);
}
event->ignore();
return;
} else if (key == Qt::Key_Down) {
QString str = "";
if (histLocation == -1) {
QApplication::beep();
event->ignore();
return;
} else if (histLocation == cmdHistory.size() - 1) {
histLocation = -1;
str = tempCmd;
} else {
++histLocation;
str = cmdHistory.at(histLocation);
}
for (int i = 0; i < inputCharCount; ++i) {
QTextEdit::keyPressEvent(new QKeyEvent(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier));
}
inputCharCount = str.length();
this->insertPlainText(str);
cmdStr = str;
} else if (key == Qt::Key_Left) {
if (inputCharCount) {
--inputCharCount;
QTextEdit::keyPressEvent(event);
} else {
QApplication::beep();
}
} else if (key == Qt::Key_Right) {
QTextCursor cursor = this->textCursor();
if (cursor.movePosition(QTextCursor::Right)) {
++inputCharCount;
this->setTextCursor(cursor);
} else {
QApplication::beep();
}
} else if (key == Qt::Key_Tab) {
// TODO: see if we can hack in tab completion. Currently we don't do anything.
} else {
QString text = event->text();
for (int i = 0; i < text.length(); ++i) {
if (text.at(i).isPrint()) {
//only increase input counter for printable characters
++inputCharCount;
}
}
QTextEdit::keyPressEvent(event);
}
} else {
if (inputCharCount) {
--inputCharCount;
QTextEdit::keyPressEvent(event);
cmdStr.remove(inputCharCount, 1);
} else {
QApplication::beep();
}
}
// now pass a char* copy of the input to the shell process
if (key == Qt::Key_Return || key == Qt::Key_Enter) {
this->moveCursor(QTextCursor::End);
shell->write(cmdStr.toAscii().data(), cmdStr.length());
shell->write("\r\n", 2);
QTextEdit::keyPressEvent(event);
cmdHistory.push_back(cmdStr);
histLocation = -1;
//.........这里部分代码省略.........
示例8: textCursor
void Editor::doBackspace()
{
QTextCursor cursor = textCursor();
cursor.deletePreviousChar();
setTextCursor(cursor);
}
示例9: cellAt
/*!
\fn QTextTableCell QTextTable::cellAt(const QTextCursor &cursor) const
\overload
Returns the table cell containing the given \a cursor.
*/
QTextTableCell QTextTable::cellAt(const QTextCursor &c) const
{
return cellAt(c.position());
}
示例10: LOG
void MythRemoteLineEdit::updateCycle(QString current_choice, QString set)
{
int index;
QString aString, bString;
// Show the characters in the current set being cycled
// through, with the current choice in a different color. If the current
// character is uppercase X (interpreted as destructive
// backspace) or an underscore (interpreted as a space)
// then show these special cases in yet another color.
if (shift)
{
set = set.toUpper();
current_choice = current_choice.toUpper();
}
bString = "<B>";
if (current_choice == "_" || current_choice == "X")
{
bString += "<FONT COLOR=\"#";
bString += hex_special;
bString += "\">";
bString += current_choice;
bString += "</FONT>";
}
else
{
bString += "<FONT COLOR=\"#";
bString += hex_selected;
bString += "\">";
bString += current_choice;
bString += "</FONT>";
}
bString += "</B>";
index = set.indexOf(current_choice);
int length = set.length();
if (index < 0 || index > length)
{
LOG(VB_GENERAL, LOG_ALERT,
QString("MythRemoteLineEdit passed a choice of \"%1"
"\" which is not in set \"%2\"")
.arg(current_choice).arg(set));
setText("????");
return;
}
else
{
set.replace(index, current_choice.length(), bString);
}
QString esc_upto = pre_cycle_text_before_cursor;
QString esc_from = pre_cycle_text_after_cursor;
esc_upto.replace("<", "<").replace(">", ">").replace("\n", "<br>");
esc_from.replace("<", "<").replace(">", ">").replace("\n", "<br>");
aString = esc_upto;
aString += "<FONT COLOR=\"#";
aString += hex_unselected;
aString += "\">[";
aString += set;
aString += "]</FONT>";
aString += esc_from;
setHtml(aString);
QTextCursor tmp = textCursor();
tmp.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor);
tmp.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor,
pre_cycle_pos + set.length());
setTextCursor(tmp);
update();
if (current_choice == "X" && !pre_cycle_text_before_cursor.isEmpty())
{
// If current selection is delete, select the character to be deleted
QTextCursor tmp = textCursor();
tmp.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor);
tmp.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor,
pre_cycle_pos - 1);
tmp.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor);
setTextCursor(tmp);
}
else
{
// Restore original cursor location
QTextCursor tmp = textCursor();
tmp.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor);
tmp.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor,
pre_cycle_pos);
setTextCursor(tmp);
}
}
示例11: highlightParentheses
void Utilities::highlightParentheses(QPlainTextEdit *pPlainTextEdit, QTextCharFormat parenthesesMatchFormat,
QTextCharFormat parenthesesMisMatchFormat)
{
if (pPlainTextEdit->isReadOnly()) {
return;
}
QTextCursor backwardMatch = pPlainTextEdit->textCursor();
QTextCursor forwardMatch = pPlainTextEdit->textCursor();
if (pPlainTextEdit->overwriteMode()) {
backwardMatch.movePosition(QTextCursor::Right);
}
const TextBlockUserData::MatchType backwardMatchType = TextBlockUserData::matchCursorBackward(&backwardMatch);
const TextBlockUserData::MatchType forwardMatchType = TextBlockUserData::matchCursorForward(&forwardMatch);
QList<QTextEdit::ExtraSelection> selections = pPlainTextEdit->extraSelections();
if (backwardMatchType == TextBlockUserData::NoMatch && forwardMatchType == TextBlockUserData::NoMatch) {
pPlainTextEdit->setExtraSelections(selections);
return;
}
if (backwardMatch.hasSelection()) {
QTextEdit::ExtraSelection selection;
if (backwardMatchType == TextBlockUserData::Mismatch) {
selection.cursor = backwardMatch;
selection.format = parenthesesMisMatchFormat;
selections.append(selection);
} else {
selection.cursor = backwardMatch;
selection.format = parenthesesMatchFormat;
selection.cursor.setPosition(backwardMatch.selectionStart());
selection.cursor.setPosition(selection.cursor.position() + 1, QTextCursor::KeepAnchor);
selections.append(selection);
selection.cursor.setPosition(backwardMatch.selectionEnd());
selection.cursor.setPosition(selection.cursor.position() - 1, QTextCursor::KeepAnchor);
selections.append(selection);
}
}
if (forwardMatch.hasSelection()) {
QTextEdit::ExtraSelection selection;
if (forwardMatchType == TextBlockUserData::Mismatch) {
selection.cursor = forwardMatch;
selection.format = parenthesesMisMatchFormat;
selections.append(selection);
} else {
selection.cursor = forwardMatch;
selection.format = parenthesesMatchFormat;
selection.cursor.setPosition(forwardMatch.selectionStart());
selection.cursor.setPosition(selection.cursor.position() + 1, QTextCursor::KeepAnchor);
selections.append(selection);
selection.cursor.setPosition(forwardMatch.selectionEnd());
selection.cursor.setPosition(selection.cursor.position() - 1, QTextCursor::KeepAnchor);
selections.append(selection);
}
}
pPlainTextEdit->setExtraSelections(selections);
}
示例12: textShape
void SimpleParagraphWidget::fillListButtons()
{
KoZoomHandler zoomHandler;
zoomHandler.setZoom(1.2);
zoomHandler.setDpi(72, 72);
KoInlineTextObjectManager itom;
KoTextRangeManager tlm;
TextShape textShape(&itom, &tlm);
textShape.setSize(QSizeF(300, 100));
QTextCursor cursor (textShape.textShapeData()->document());
foreach(const Lists::ListStyleItem &item, Lists::genericListStyleItems()) {
QPixmap pm(48,48);
pm.fill(Qt::transparent);
QPainter p(&pm);
p.translate(0, -1.5);
p.setRenderHint(QPainter::Antialiasing);
if(item.style != KoListStyle::None) {
KoListStyle listStyle;
KoListLevelProperties llp = listStyle.levelProperties(1);
llp.setStyle(item.style);
if (KoListStyle::isNumberingStyle(item.style)) {
llp.setStartValue(1);
llp.setListItemSuffix(".");
}
listStyle.setLevelProperties(llp);
cursor.select(QTextCursor::Document);
QTextCharFormat textCharFormat=cursor.blockCharFormat();
textCharFormat.setFontPointSize(11);
textCharFormat.setFontWeight(QFont::Normal);
cursor.setCharFormat(textCharFormat);
QTextBlock cursorBlock = cursor.block();
KoTextBlockData data(cursorBlock);
cursor.insertText("----");
listStyle.applyStyle(cursor.block(),1);
cursorBlock = cursor.block();
KoTextBlockData data1(cursorBlock);
cursor.insertText("\n----");
cursorBlock = cursor.block();
KoTextBlockData data2(cursorBlock);
cursor.insertText("\n----");
cursorBlock = cursor.block();
KoTextBlockData data3(cursorBlock);
KoTextDocumentLayout *lay = dynamic_cast<KoTextDocumentLayout*>(textShape.textShapeData()->document()->documentLayout());
if(lay)
lay->layout();
KoShapePaintingContext paintContext; //FIXME
textShape.paintComponent(p, zoomHandler, paintContext);
widget.bulletListButton->addItem(pm, static_cast<int> (item.style));
}
}
示例13: textCursor
bool SourceViewerWidget::findText(const QString &text, WebWidget::FindFlags flags)
{
const bool isTheSame = (text == m_findText);
m_findText = text;
m_findFlags = flags;
if (!text.isEmpty())
{
QTextDocument::FindFlags nativeFlags;
if (flags.testFlag(WebWidget::BackwardFind))
{
nativeFlags |= QTextDocument::FindBackward;
}
if (flags.testFlag(WebWidget::CaseSensitiveFind))
{
nativeFlags |= QTextDocument::FindCaseSensitively;
}
QTextCursor findTextCursor = m_findTextAnchor;
if (!isTheSame)
{
findTextCursor = textCursor();
}
else if (!flags.testFlag(WebWidget::BackwardFind))
{
findTextCursor.setPosition(findTextCursor.selectionEnd(), QTextCursor::MoveAnchor);
}
m_findTextAnchor = document()->find(text, findTextCursor, nativeFlags);
if (m_findTextAnchor.isNull())
{
m_findTextAnchor = textCursor();
m_findTextAnchor.setPosition((flags.testFlag(WebWidget::BackwardFind) ? (document()->characterCount() - 1) : 0), QTextCursor::MoveAnchor);
m_findTextAnchor = document()->find(text, m_findTextAnchor, nativeFlags);
}
if (!m_findTextAnchor.isNull())
{
const QTextCursor currentTextCursor = textCursor();
disconnect(this, SIGNAL(cursorPositionChanged()), this, SLOT(updateTextCursor()));
setTextCursor(m_findTextAnchor);
ensureCursorVisible();
const QPoint position(horizontalScrollBar()->value(), verticalScrollBar()->value());
setTextCursor(currentTextCursor);
horizontalScrollBar()->setValue(position.x());
verticalScrollBar()->setValue(position.y());
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(updateTextCursor()));
}
}
m_findTextSelection = m_findTextAnchor;
updateSelection();
return !m_findTextAnchor.isNull();
}
示例14: textCursor
void AMGraphicsTextItem::selectAllText()
{
QTextCursor newCursor = textCursor();
newCursor.select(QTextCursor::Document);
setTextCursor(newCursor);
}
示例15: moveCursorToEnd
static void moveCursorToEnd(Editor* editor)
{
QTextCursor cursor = editor->textCursor();
cursor.movePosition(QTextCursor::EndOfBlock);
editor->setTextCursor(cursor);
}