Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3#include <QApplication>
4#include <QTextEdit>
5
6QString tr(const char *text)
7{
8 return QApplication::translate(text, text);
9}
10
11int main_textblock_formats(int argc, char *argv[])
12{
13 QApplication app(argc, argv);
14
15//! [0]
16 QTextEdit *editor = new QTextEdit();
17 QTextCursor cursor(editor->textCursor());
18//! [0]
19 cursor.movePosition(QTextCursor::Start);
20
21 QTextBlockFormat blockFormat = cursor.blockFormat();
22 blockFormat.setTopMargin(4);
23 blockFormat.setLeftMargin(4);
24 blockFormat.setRightMargin(4);
25 blockFormat.setBottomMargin(4);
26
27 cursor.setBlockFormat(blockFormat);
28 cursor.insertText(tr("This contains plain text inside a "
29 "text block with margins to keep it separate "
30 "from other parts of the document."));
31
32 cursor.insertBlock();
33
34//! [1]
35 QTextBlockFormat backgroundFormat = blockFormat;
36 backgroundFormat.setBackground(QColor("lightGray"));
37
38 cursor.setBlockFormat(backgroundFormat);
39//! [1]
40 cursor.insertText(tr("The background color of a text block can be "
41 "changed to highlight text."));
42
43 cursor.insertBlock();
44
45 QTextBlockFormat rightAlignedFormat = blockFormat;
46 rightAlignedFormat.setAlignment(Qt::AlignRight);
47
48 cursor.setBlockFormat(rightAlignedFormat);
49 cursor.insertText(tr("The alignment of the text within a block is "
50 "controlled by the alignment properties of "
51 "the block itself. This text block is "
52 "right-aligned."));
53
54 cursor.insertBlock();
55
56 QTextBlockFormat paragraphFormat = blockFormat;
57 paragraphFormat.setAlignment(Qt::AlignJustify);
58 paragraphFormat.setTextIndent(32);
59
60 cursor.setBlockFormat(paragraphFormat);
61 cursor.insertText(tr("Text can be formatted so that the first "
62 "line in a paragraph has its own margin. "
63 "This makes the text more readable."));
64
65 cursor.insertBlock();
66
67 QTextBlockFormat reverseFormat = blockFormat;
68 reverseFormat.setAlignment(Qt::AlignJustify);
69 reverseFormat.setTextIndent(32);
70
71 cursor.setBlockFormat(reverseFormat);
72 cursor.insertText(tr("The direction of the text can be reversed. "
73 "This is useful for right-to-left "
74 "languages."));
75
76 editor->setWindowTitle(tr("Text Block Formats"));
77 editor->resize(480, 480);
78 editor->show();
79
80 return app.exec();
81}
int main_textblock_formats(int argc, char *argv[])
Definition main.cpp:11
QString tr(const char *)