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
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
22 QTextDocument *editorDocument = editor->document();
23 QTextFrame *root = editorDocument->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
37 QTextFrame *mainFrame = cursor.currentFrame();
38 cursor.insertText(...);
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
52 QTextFrameFormat frameFormat;
53 frameFormat.setMargin(32);
54 frameFormat.setPadding(8);
55 frameFormat.setBorder(4);
57 cursor.insertFrame(frameFormat);
58
59/* insert frame
61 cursor.insertFrame(frameFormat);
62 cursor.insertText(...);
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
73 cursor = mainFrame->lastCursorPosition();
74 cursor.insertText(...);
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
92 connect(quitAction, &QAction::triggered, this, &MainWindow::close);
93
94 setCentralWidget(editor);
95 setWindowTitle(tr("Text Document Frames"));
96}
97
99{
101 tr("Save document as:"), "", tr("XML (*.xml)"));
102
103 if (!fileName.isEmpty()) {
104 if (writeXml(fileName))
106 else
107 QMessageBox::warning(this, tr("Warning"),
108 tr("Failed to save the document."), QMessageBox::Cancel,
110 }
111}
112
113void MainWindow::processBlock(QTextBlock)
114{
115}
116
117void MainWindow::processFrame(QTextFrame *frame)
118{
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 }
132}
void saveFile()
The QAction class provides an abstraction for user commands that can be added to different user inter...
Definition qaction.h:30
void triggered(bool checked=false)
This signal is emitted when an action is activated by the user; for example, when the user clicks a m...
static QString getSaveFileName(QWidget *parent=nullptr, const QString &caption=QString(), const QString &dir=QString(), const QString &filter=QString(), QString *selectedFilter=nullptr, Options options=Options())
This is a convenience static function that returns a file name selected by the user.
@ Bold
Definition qfont.h:70
void setCentralWidget(QWidget *widget)
Sets the given widget to be the main window's central widget.
QAction * addMenu(QMenu *menu)
Appends menu to the menu bar.
Definition qmenubar.cpp:768
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus.
Definition qmenu.h:26
void addAction(QAction *action)
Appends the action action to this widget's list of actions.
Definition qwidget.cpp:3117
static StandardButton warning(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\reentrant
void setFontWeight(int weight)
Sets the text format's font weight to weight.
\reentrant \inmodule QtGui
Definition qtextcursor.h:30
\reentrant \inmodule QtGui
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.
QTextDocument * document
the underlying document of the text editor.
Definition qtextedit.h:51
void setPadding(qreal padding)
Sets the width of the frame's internal padding in pixels.
void setBorder(qreal border)
Sets the width (in pixels) of the frame's border.
void setMargin(qreal margin)
Sets the frame's margin in pixels.
\reentrant
Definition qtextobject.h:81
bool close()
Closes this widget.
Definition qwidget.cpp:8562
void setWindowTitle(const QString &)
Definition qwidget.cpp:6105
QCursor cursor
the cursor shape for this widget
Definition qwidget.h:135
QSet< QString >::iterator it
#define tr(X)
QFrame frame
[0]
QMenuBar * menuBar
[0]