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
mainwindow.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
4#include <QtWidgets>
5
6#include "mainwindow.h"
7
9{
10 QMenu *fileMenu = new QMenu(tr("&File"));
11
12 QAction *saveAction = fileMenu->addAction(tr("&Save..."));
13 saveAction->setShortcut(tr("Ctrl+S"));
14
15 QAction *quitAction = fileMenu->addAction(tr("E&xit"));
16 quitAction->setShortcut(tr("Ctrl+Q"));
17
18 menuBar()->addMenu(fileMenu);
19 editor = new QTextEdit;
20
21//! [rootframe]
22 QTextDocument *editorDocument = editor->document();
23 QTextFrame *root = editorDocument->rootFrame();
24//! [rootframe]
25 processFrame(root);
26
27 QTextCursor cursor(editor->textCursor());
28 cursor.movePosition(QTextCursor::Start);
29
30 QTextFrame *mainFrame = cursor.currentFrame();
31
32 QTextCharFormat plainCharFormat;
33 QTextCharFormat boldCharFormat;
34 boldCharFormat.setFontWeight(QFont::Bold);
35/* main frame
36//! [0]
37 QTextFrame *mainFrame = cursor.currentFrame();
38 cursor.insertText(...);
39//! [0]
40*/
41 cursor.insertText("Text documents are represented by the "
42 "QTextDocument class, rather than by QString objects. "
43 "Each QTextDocument object contains information about "
44 "the document's internal representation, its structure, "
45 "and keeps track of modifications to provide undo/redo "
46 "facilities. This approach allows features such as the "
47 "layout management to be delegated to specialized "
48 "classes, but also provides a focus for the framework.",
49 plainCharFormat);
50
51//! [1]
52 QTextFrameFormat frameFormat;
53 frameFormat.setMargin(32);
54 frameFormat.setPadding(8);
55 frameFormat.setBorder(4);
56//! [1]
57 cursor.insertFrame(frameFormat);
58
59/* insert frame
60//! [2]
61 cursor.insertFrame(frameFormat);
62 cursor.insertText(...);
63//! [2]
64*/
65 cursor.insertText("Documents are either converted from external sources "
66 "or created from scratch using Qt. The creation process "
67 "can done by an editor widget, such as QTextEdit, or by "
68 "explicit calls to the Scribe API.", boldCharFormat);
69
70 cursor = mainFrame->lastCursorPosition();
71/* last cursor
72//! [3]
73 cursor = mainFrame->lastCursorPosition();
74 cursor.insertText(...);
75//! [3]
76*/
77 cursor.insertText("There are two complementary ways to visualize the "
78 "contents of a document: as a linear buffer that is "
79 "used by editors to modify the contents, and as an "
80 "object hierarchy containing structural information "
81 "that is useful to layout engines. In the hierarchical "
82 "model, the objects generally correspond to visual "
83 "elements such as frames, tables, and lists. At a lower "
84 "level, these elements describe properties such as the "
85 "style of text used and its alignment. The linear "
86 "representation of the document is used for editing and "
87 "manipulation of the document's contents.",
88 plainCharFormat);
89
90
91 connect(saveAction, &QAction::triggered, this, &MainWindow::saveFile);
92 connect(quitAction, &QAction::triggered, this, &MainWindow::close);
93
94 setCentralWidget(editor);
95 setWindowTitle(tr("Text Document Frames"));
96}
97
98void MainWindow::saveFile()
99{
100 QString fileName = QFileDialog::getSaveFileName(this,
101 tr("Save document as:"), "", tr("XML (*.xml)"));
102
103 if (!fileName.isEmpty()) {
104 if (writeXml(fileName))
105 setWindowTitle(fileName);
106 else
107 QMessageBox::warning(this, tr("Warning"),
108 tr("Failed to save the document."), QMessageBox::Cancel,
109 QMessageBox::NoButton);
110 }
111}
112
113void MainWindow::processBlock(QTextBlock)
114{
115}
116
117void MainWindow::processFrame(QTextFrame *frame)
118{
119//! [4]
120 QTextFrame::iterator it;
121 for (it = frame->begin(); !(it.atEnd()); ++it) {
122
123 QTextFrame *childFrame = it.currentFrame();
124 QTextBlock childBlock = it.currentBlock();
125
126 if (childFrame)
127 processFrame(childFrame);
128 else if (childBlock.isValid())
129 processBlock(childBlock);
130 }
131//! [4]
132}
void saveFile()