C++ (Qt) int pointSize = ui->cbFontSize->currentText().toInt(); QFont font = ui->text->document()->defaultFont(); font.setPointSize(pointSize); ui->text->document()->setDefaultFont(font); ui->text->setFontPointSize(pointSize);
void QTextControlPrivate::setContent(Qt::TextFormat format, const QString &text, QTextDocument *document){ Q_Q(QTextControl); // for use when called from setPlainText. we may want to re-use the currently // set char format then. const QTextCharFormat charFormatForInsertion = cursor.charFormat(); bool clearDocument = true; if (!doc) { if (document) { doc = document; clearDocument = false; } else { palette = QApplication::palette("QTextControl");// QTextDocument *doc; - приватный член класса QTextControl///------------------------------------------------------------ doc = new QTextDocument(q);///------------------------------------------------------------ } _q_documentLayoutChanged(); cursor = QTextCursor(doc);// #### doc->documentLayout()->setPaintDevice(viewport); QObject::connect(doc, SIGNAL(contentsChanged()), q, SLOT(_q_updateCurrentCharFormatAndSelection())); QObject::connect(doc, SIGNAL(cursorPositionChanged(QTextCursor)), q, SLOT(_q_emitCursorPosChanged(QTextCursor))); QObject::connect(doc, SIGNAL(documentLayoutChanged()), q, SLOT(_q_documentLayoutChanged())); // convenience signal forwards QObject::connect(doc, SIGNAL(undoAvailable(bool)), q, SIGNAL(undoAvailable(bool))); QObject::connect(doc, SIGNAL(redoAvailable(bool)), q, SIGNAL(redoAvailable(bool))); QObject::connect(doc, SIGNAL(modificationChanged(bool)), q, SIGNAL(modificationChanged(bool))); QObject::connect(doc, SIGNAL(blockCountChanged(int)), q, SIGNAL(blockCountChanged(int))); } bool previousUndoRedoState = doc->isUndoRedoEnabled(); if (!document) doc->setUndoRedoEnabled(false); //Saving the index save some time. static int contentsChangedIndex = QTextDocument::staticMetaObject.indexOfSignal("contentsChanged()"); static int textChangedIndex = QTextControl::staticMetaObject.indexOfSignal("textChanged()"); // avoid multiple textChanged() signals being emitted QMetaObject::disconnect(doc, contentsChangedIndex, q, textChangedIndex); if (!text.isEmpty()) { // clear 'our' cursor for insertion to prevent // the emission of the cursorPositionChanged() signal. // instead we emit it only once at the end instead of // at the end of the document after loading and when // positioning the cursor again to the start of the // document. cursor = QTextCursor(); if (format == Qt::PlainText) { QTextCursor formatCursor(doc); // put the setPlainText and the setCharFormat into one edit block, // so that the syntax highlight triggers only /once/ for the entire // document, not twice. formatCursor.beginEditBlock(); doc->setPlainText(text); doc->setUndoRedoEnabled(false); formatCursor.select(QTextCursor::Document); formatCursor.setCharFormat(charFormatForInsertion); formatCursor.endEditBlock(); } else {#ifndef QT_NO_TEXTHTMLPARSER doc->setHtml(text);#else doc->setPlainText(text);#endif doc->setUndoRedoEnabled(false); } cursor = QTextCursor(doc); } else if (clearDocument) { doc->clear(); } cursor.setCharFormat(charFormatForInsertion); QMetaObject::connect(doc, contentsChangedIndex, q, textChangedIndex); emit q->textChanged(); if (!document) doc->setUndoRedoEnabled(previousUndoRedoState); _q_updateCurrentCharFormatAndSelection(); if (!document) doc->setModified(false); q->ensureCursorVisible(); emit q->cursorPositionChanged();}
void QTextControlPrivate::setContent(Qt::TextFormat format, const QString &text, QTextDocument *document){ Q_Q(QTextControl); // for use when called from setPlainText. we may want to re-use the currently // set char format then.
void QTextControl::clear(){ Q_D(QTextControl); // clears and sets empty content d->extraSelections.clear(); d->setContent();}