/*! Append a new message to the current session and scroll to the end of the message protocol and returns true if the action was successful. The \a messageColor defines the color of the message box and should be provided as a full-color (no dimming required) color, as it is automatically adjusted for the border and background. */ bool QwcPrivateMessager::appendMessageToCurrentSession(QTextDocument *document, const QString message, const QColor messageColor) { if (!document) { return false; } QTextCursor cursor = document->rootFrame()->lastCursorPosition(); cursor.movePosition(QTextCursor::StartOfBlock); QTextFrameFormat frameFormat; frameFormat.setPadding(4); frameFormat.setBackground(messageColor.lighter(190)); frameFormat.setMargin(0); frameFormat.setBorder(2); frameFormat.setBorderBrush(messageColor.lighter(150)); frameFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Outset); // Title QTextCharFormat backupCharFormat = cursor.charFormat(); QTextCharFormat newCharFormat; newCharFormat.setFontPointSize(9); QTextBlockFormat headerFormat; headerFormat.setAlignment(Qt::AlignHCenter); cursor.insertBlock(headerFormat); cursor.setCharFormat(newCharFormat); cursor.insertText(QDateTime::currentDateTime().toString()); QTextFrame *frame = cursor.insertFrame(frameFormat); cursor.setCharFormat(backupCharFormat); frame->firstCursorPosition().insertText(message); return true; }
QTextFrameFormat DefaultFrameFormat() { QTextFrameFormat base; base.setBorderBrush ( Qt::blue ); base.setBorderStyle ( QTextFrameFormat::BorderStyle_Solid ); base.setBorder ( 0.6 ); return base; }
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); // 获取文档对象 QTextDocument *document = ui->textEdit->document(); // 获取根框架 QTextFrame *rootFrame = document->rootFrame(); // 创建框架格式 QTextFrameFormat format; // 边界颜色 format.setBorderBrush(Qt::red); // 边界宽度 format.setBorder(3); // 框架使用格式 rootFrame->setFrameFormat(format); QTextFrameFormat frameFormat; // 设置背景颜色 frameFormat.setBackground(Qt::lightGray); // 设置边距 frameFormat.setMargin(10); // 设置填衬 frameFormat.setPadding(5); frameFormat.setBorder(2); frameFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Dotted); //设置边框样式 // 获取光标 QTextCursor cursor = ui->textEdit->textCursor(); // 在光标处插入框架 cursor.insertFrame(frameFormat); //以下是5.2.2节内容 QAction *action_textFrame = new QAction(tr("框架"),this); connect(action_textFrame,SIGNAL(triggered()),this,SLOT(showTextFrame())); // 在工具栏添加动作 ui->mainToolBar->addAction(action_textFrame); QAction *action_textBlock = new QAction(tr("文本块"),this); connect(action_textBlock,SIGNAL(triggered()),this,SLOT(showTextBlock())); ui->mainToolBar->addAction(action_textBlock); QAction *action_font = new QAction(tr("字体"),this); // 设置动作可以被选中 action_font->setCheckable(true); connect(action_font,SIGNAL(toggled(bool)),this,SLOT(setTextFont(bool))); ui->mainToolBar->addAction(action_font); }
void KWQTableView::createPages(QPrinter *printer, QTextDocument *textDoc, bool sendToPrinter) { printer->setFullPage(true); int myDpi = printer->logicalDpiY(); if (Prefs::printStyle() == Prefs::EnumPrintStyle::Flashcard) { printer->setOrientation(QPrinter::Landscape); int cardWidth = qRound(5 * qreal(myDpi)); int cardHeight = qRound(3 * qreal(myDpi)); QTextTable *table = textDoc->rootFrame()->lastCursorPosition().insertTable(model()->rowCount(), 2); QTextTableFormat tableFormat = table->format(); tableFormat.setHeaderRowCount(0); tableFormat.setBorderStyle(QTextFrameFormat::BorderStyle_None); tableFormat.setCellSpacing(0); tableFormat.setCellPadding(0); QVector<QTextLength> constraints; constraints.append(QTextLength(QTextLength::FixedLength, cardWidth)); constraints.append(QTextLength(QTextLength::FixedLength, cardWidth)); tableFormat.setColumnWidthConstraints(constraints); table->setFormat(tableFormat); QTextBlockFormat headerFormat; headerFormat.setAlignment(Qt::AlignLeft); QTextCharFormat headerCharFormat; headerCharFormat.setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont)); QTextBlockFormat cellFormat; cellFormat.setAlignment(Qt::AlignCenter); QTextCharFormat cellCharFormat; cellCharFormat.setFont(Prefs::editorFont()); QTextFrameFormat cardFormat; cardFormat.setBorder(1); cardFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Solid); cardFormat.setBorderBrush(QBrush(Qt::black)); cardFormat.setWidth(QTextLength(QTextLength::FixedLength, cardWidth)); cardFormat.setHeight(QTextLength(QTextLength::FixedLength, cardHeight)); cardFormat.setPadding(qRound(0.25 * myDpi)); QTextFrameFormat lineFormat; lineFormat.setBorder(1); lineFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Solid); lineFormat.setBorderBrush(QBrush(Qt::black)); lineFormat.setWidth(QTextLength(QTextLength::FixedLength, qRound(4.5 * myDpi))); lineFormat.setHeight(1.1); //1 is drawn as a box whereas this is drawn as a line. Strange... lineFormat.setPadding(0); QTextFrame *card; for (int i = 0; i < model()->rowCount(); i++) { for (int j = 0; j < model()->columnCount(); j++) { cardFormat.setPosition(QTextFrameFormat::FloatLeft); card = table->cellAt(i, j).firstCursorPosition().insertFrame(cardFormat); card->lastCursorPosition().insertText(model()->headerData(j, Qt::Horizontal, Qt::DisplayRole).toString(), headerCharFormat); card->lastCursorPosition().insertFrame(lineFormat); card->lastCursorPosition().insertBlock(); card->lastCursorPosition().insertBlock(); card->lastCursorPosition().insertBlock(cellFormat, cellCharFormat); card->lastCursorPosition().insertText(model()->data(model()->index(i, j)).toString(), cellCharFormat); } } } else { textDoc->rootFrame()->lastCursorPosition().insertText(QStringLiteral("kwordquiz %1").arg(KWQ_VERSION)); if (Prefs::printStyle() == Prefs::EnumPrintStyle::Exam) textDoc->rootFrame()->lastCursorPosition().insertText(' ' + i18n("Name:_____________________________ Date:__________")); QTextTable* table; if (Prefs::printStyle() == Prefs::EnumPrintStyle::Exam) table = textDoc->rootFrame()->lastCursorPosition().insertTable(model()->rowCount() + 1, model()->columnCount() + 2); else table = textDoc->rootFrame()->lastCursorPosition().insertTable(model()->rowCount() + 1, model()->columnCount() + 1); QTextTableFormat tableFormat = table->format(); tableFormat.setHeaderRowCount(1); tableFormat.setBorder(1); tableFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Solid); tableFormat.setCellSpacing(0); tableFormat.setBorderBrush(QBrush(Qt::black)); tableFormat.setCellPadding(2); QVector<QTextLength> constraints; constraints.append(QTextLength(QTextLength::FixedLength, verticalHeader()->width())); constraints.append(QTextLength(QTextLength::FixedLength, columnWidth(0))); constraints.append(QTextLength(QTextLength::FixedLength, columnWidth(1))); if (Prefs::printStyle() == Prefs::EnumPrintStyle::Exam) constraints.append(QTextLength(QTextLength::FixedLength, 50)); tableFormat.setColumnWidthConstraints(constraints); table->setFormat(tableFormat); QTextBlockFormat headerFormat; headerFormat.setAlignment(Qt::AlignHCenter); QTextCharFormat headerCharFormat; headerCharFormat.setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont)); QTextCursor cellCursor; cellCursor = table->cellAt(0, 1).firstCursorPosition(); cellCursor.mergeBlockFormat(headerFormat); cellCursor.mergeCharFormat(headerCharFormat); cellCursor.insertText(model()->headerData(0, Qt::Horizontal, Qt::DisplayRole).toString()); cellCursor = table->cellAt(0, 2).firstCursorPosition(); cellCursor.mergeBlockFormat(headerFormat); cellCursor.mergeCharFormat(headerCharFormat); cellCursor.insertText(model()->headerData(1, Qt::Horizontal, Qt::DisplayRole).toString()); if (Prefs::printStyle() == Prefs::EnumPrintStyle::Exam) { cellCursor = table->cellAt(0, 3).firstCursorPosition(); cellCursor.mergeBlockFormat(headerFormat); cellCursor.mergeCharFormat(headerCharFormat); cellCursor.insertText(i18n("Score")); } headerCharFormat = cellCursor.charFormat(); QTextCharFormat cellCharFormat = cellCursor.charFormat(); cellCharFormat.setFont(Prefs::editorFont()); for (int i = 0; i < model()->rowCount(); i++) { table->cellAt(i + 1, 0).firstCursorPosition().insertText(model()->headerData(i, Qt::Vertical, Qt::DisplayRole).toString(), headerCharFormat); table->cellAt(i + 1, 1).firstCursorPosition().insertText(model()->data(model()->index(i, 0)).toString(), cellCharFormat); if (Prefs::printStyle() == Prefs::EnumPrintStyle::List) table->cellAt(i + 1, 2).firstCursorPosition().insertText(model()->data(model()->index(i, 1)).toString(), cellCharFormat); } } if (sendToPrinter) textDoc->print(printer); }