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 QMenu *insertMenu = new QMenu(tr("&Insert"));
19
20 QAction *calendarAction = insertMenu->addAction(tr("&Calendar"));
21 calendarAction->setShortcut(tr("Ctrl+I"));
22
23 menuBar()->addMenu(fileMenu);
24 menuBar()->addMenu(insertMenu);
25
27 editor = new QTextEdit(this);
29
31 connect(quitAction, &QAction::triggered, this, &MainWindow::close);
33
34 setCentralWidget(editor);
35 setWindowTitle(tr("Text Document Writer"));
36}
37
39{
41 tr("Save document as:"), "", tr("XML (*.xml)"));
42
43 if (!fileName.isEmpty()) {
44 if (writeXml(fileName))
46 else
47 QMessageBox::warning(this, tr("Warning"),
48 tr("Failed to save the document."), QMessageBox::Cancel,
50 }
51}
52
54{
56 QTextCursor cursor(editor->textCursor());
57 cursor.movePosition(QTextCursor::Start);
58
59 QTextCharFormat format(cursor.charFormat());
60 format.setFontFamily("Courier");
61
62 QTextCharFormat boldFormat = format;
63 boldFormat.setFontWeight(QFont::Bold);
64
65 cursor.insertBlock();
66 cursor.insertText(" ", boldFormat);
67
69 int year = date.year(), month = date.month();
70
71 for (int weekDay = 1; weekDay <= 7; ++weekDay) {
72 cursor.insertText(QString("%1 ").arg(QLocale::system().dayName(weekDay), 3),
73 boldFormat);
74 }
75
76 cursor.insertBlock();
77 cursor.insertText(" ", format);
78
79 for (int column = 1; column < QDate(year, month, 1).dayOfWeek(); ++column) {
80 cursor.insertText(" ", format);
81 }
82
83 for (int day = 1; day <= date.daysInMonth(); ++day) {
85 int weekDay = QDate(year, month, day).dayOfWeek();
86
87 if (QDate(year, month, day) == date)
88 cursor.insertText(QString("%1 ").arg(day, 3), boldFormat);
89 else
90 cursor.insertText(QString("%1 ").arg(day, 3), format);
91
92 if (weekDay == 7) {
93 cursor.insertBlock();
94 cursor.insertText(" ", format);
95 }
97 }
99}
100
void saveFile()
void insertCalendar()
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...
\inmodule QtCore \reentrant
Definition qdatetime.h:29
int month() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
int year() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
static QDate currentDate()
Returns the system clock's current date.
int daysInMonth() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
int dayOfWeek() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
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
static QLocale system()
Returns a QLocale object initialized to the system locale.
Definition qlocale.cpp:2862
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
void setFontWeight(int weight)
Sets the text format's font weight to weight.
\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.
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
QDate date
[1]
GLint GLsizei GLsizei GLenum format
GLenum GLenum GLsizei void GLsizei void * column
SSL_CTX int void * arg
#define tr(X)
QMenuBar * menuBar
[0]