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#include "mainwindow.h"
4
5#include <QtWidgets>
6
8{
9 QMenu *fileMenu = new QMenu(tr("&File"));
10
11 fileMenu->addAction(tr("&Open..."), QKeySequence(tr("Ctrl+O", "File|Open")),
13
14 QAction *quitAction = fileMenu->addAction(tr("E&xit"), this, &MainWindow::close);
15 quitAction->setShortcut(tr("Ctrl+Q"));
16
17 QMenu *editMenu = new QMenu(tr("&Edit"));
18
19 cutAction = editMenu->addAction(tr("Cu&t"), this, &MainWindow::cutSelection);
20 cutAction->setShortcut(tr("Ctrl+X"));
21 cutAction->setEnabled(false);
22
23 copyAction = editMenu->addAction(tr("&Copy"), this, &MainWindow::copySelection);
24 copyAction->setShortcut(tr("Ctrl+C"));
25 copyAction->setEnabled(false);
26
27 pasteAction = editMenu->addAction(tr("&Paste"), this, &MainWindow::pasteSelection);
28 pasteAction->setShortcut(tr("Ctrl+V"));
29 pasteAction->setEnabled(false);
30
31 QMenu *selectMenu = new QMenu(tr("&Select"));
32 selectMenu->addAction(tr("&Word"), this, &MainWindow::selectWord);
33 selectMenu->addAction(tr("&Line"), this, &MainWindow::selectLine);
34 selectMenu->addAction(tr("&Block"), this, &MainWindow::selectBlock);
35 selectMenu->addAction(tr("&Frame"), this, &MainWindow::selectFrame);
36
37 menuBar()->addMenu(fileMenu);
38 menuBar()->addMenu(editMenu);
39 menuBar()->addMenu(selectMenu);
40
41 editor = new QTextEdit(this);
42 document = new QTextDocument(this);
43 editor->setDocument(document);
44
47
48 setCentralWidget(editor);
49 setWindowTitle(tr("Text Document Writer"));
50}
51
53{
55 tr("Open file"), currentFile, "HTML files (*.html);;Text files (*.txt)");
56
57 if (!fileName.isEmpty()) {
59 if (info.completeSuffix() == "html") {
61
63 editor->setHtml(QString(file.readAll()));
64 file.close();
65 currentFile = fileName;
66 }
67 } else if (info.completeSuffix() == "txt") {
69
71 editor->setPlainText(file.readAll());
72 file.close();
73 currentFile = fileName;
74 }
75 }
76 }
77}
78
80{
81 QTextCursor cursor = editor->textCursor();
82 if (cursor.hasSelection()) {
83 selection = cursor.selection();
84 cursor.removeSelectedText();
85 }
86}
87
89{
90 QTextCursor cursor = editor->textCursor();
91 if (cursor.hasSelection()) {
92 selection = cursor.selection();
93 cursor.clearSelection();
94 }
95}
96
98{
99 QTextCursor cursor = editor->textCursor();
100 cursor.insertFragment(selection);
101}
102
104{
105 QTextCursor cursor = editor->textCursor();
106
108 cursor.beginEditBlock();
110 cursor.movePosition(QTextCursor::StartOfWord);
113 cursor.endEditBlock();
115
116 editor->setTextCursor(cursor);
117}
118
120{
121 QTextCursor cursor = editor->textCursor();
122
123 cursor.beginEditBlock();
124 cursor.movePosition(QTextCursor::StartOfLine);
126 cursor.endEditBlock();
127
128 editor->setTextCursor(cursor);
129}
130
132{
133 QTextCursor cursor = editor->textCursor();
134
135 cursor.beginEditBlock();
136 cursor.movePosition(QTextCursor::StartOfBlock);
138 cursor.endEditBlock();
139
140 editor->setTextCursor(cursor);
141}
142
144{
145 QTextCursor cursor = editor->textCursor();
146 QTextFrame *frame = cursor.currentFrame();
147
148 cursor.beginEditBlock();
149 cursor.setPosition(frame->firstPosition());
150 cursor.setPosition(frame->lastPosition(), QTextCursor::KeepAnchor);
151 cursor.endEditBlock();
152
153 editor->setTextCursor(cursor);
154}
155
157{
158 QTextCursor cursor = editor->textCursor();
159 cutAction->setEnabled(cursor.hasSelection());
160 copyAction->setEnabled(cursor.hasSelection());
161
162 pasteAction->setEnabled(!selection.isEmpty());
163}
void selectBlock()
void copySelection()
void openFile()
void cutSelection()
void selectLine()
void selectFrame()
void pasteSelection()
void selectWord()
void updateMenus()
The QAction class provides an abstraction for user commands that can be added to different user inter...
Definition qaction.h:30
void setEnabled(bool)
Definition qaction.cpp:927
void close() override
Calls QFileDevice::flush() and closes the file.
static QString getOpenFileName(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 an existing file selected by the user.
\inmodule QtCore
Definition qfile.h:93
QFILE_MAYBE_NODISCARD bool open(OpenMode flags) override
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition qfile.cpp:904
QByteArray readAll()
Reads all remaining data from the device, and returns it as a byte array.
The QKeySequence class encapsulates a key sequence as used by shortcuts.
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 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 \inmodule QtGui
Definition qtextcursor.h:30
bool isEmpty() const
Returns true if the fragment is empty; otherwise returns false.
\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
void setTextCursor(const QTextCursor &cursor)
Sets the visible cursor.
void setPlainText(const QString &text)
Changes the text of the text edit to the string text.
QTextCursor textCursor() const
Returns a copy of the QTextCursor that represents the currently visible cursor.
void setHtml(const QString &text)
void setDocument(QTextDocument *document)
void selectionChanged()
This signal is emitted whenever the selection changes.
\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
#define tr(X)
QFile file
[0]
QFrame frame
[0]
QMenuBar * menuBar
[0]
QHostInfo info
[0]