Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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{
9}
10
11int main_textblock_formats(int argc, char *argv[])
12{
13 QApplication app(argc, argv);
14
16 QTextEdit *editor = new QTextEdit();
17 QTextCursor cursor(editor->textCursor());
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
35 QTextBlockFormat backgroundFormat = blockFormat;
36 backgroundFormat.setBackground(QColor("lightGray"));
37
38 cursor.setBlockFormat(backgroundFormat);
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}
The QApplication class manages the GUI application's control flow and main settings.
static int exec()
Enters the main event loop and waits until exit() is called, then returns the value that was set to e...
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
static QString translate(const char *context, const char *key, const char *disambiguation=nullptr, int n=-1)
\threadsafe
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
void setLeftMargin(qreal margin)
Sets the paragraph's left margin.
void setAlignment(Qt::Alignment alignment)
Sets the paragraph's alignment.
void setBottomMargin(qreal margin)
Sets the paragraph's bottom margin.
void setRightMargin(qreal margin)
Sets the paragraph's right margin.
void setTopMargin(qreal margin)
Sets the paragraph's top margin.
\reentrant \inmodule QtGui
Definition qtextcursor.h:30
The QTextEdit class provides a widget that is used to edit and display both plain and rich text.
Definition qtextedit.h:27
QTextCursor textCursor() const
Returns a copy of the QTextCursor that represents the currently visible cursor.
void setBackground(const QBrush &brush)
Sets the brush use to paint the document's background to the brush specified.
QString text
QCursor cursor
@ AlignRight
Definition qnamespace.h:146
@ AlignJustify
Definition qnamespace.h:149
int main_textblock_formats(int argc, char *argv[])
Definition main.cpp:11
#define tr(X)
QApplication app(argc, argv)
[0]