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 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
26//! [0]
27 editor = new QTextEdit(this);
28//! [0]
29
30 connect(saveAction, &QAction::triggered, this, &MainWindow::saveFile);
31 connect(quitAction, &QAction::triggered, this, &MainWindow::close);
32 connect(calendarAction, &QAction::triggered, this, &MainWindow::insertCalendar);
33
34 setCentralWidget(editor);
35 setWindowTitle(tr("Text Document Writer"));
36}
37
39{
40 QString fileName = QFileDialog::getSaveFileName(this,
41 tr("Save document as:"), "", tr("XML (*.xml)"));
42
43 if (!fileName.isEmpty()) {
44 if (writeXml(fileName))
45 setWindowTitle(fileName);
46 else
47 QMessageBox::warning(this, tr("Warning"),
48 tr("Failed to save the document."), QMessageBox::Cancel,
49 QMessageBox::NoButton);
50 }
51}
52
53void MainWindow::insertCalendar()
54{
55//! [1]
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
68 QDate date = QDate::currentDate();
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) {
84//! [1] //! [2]
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 }
96//! [2] //! [3]
97 }
98//! [3]
99}
void saveFile()