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 QAction *quitAction = fileMenu->addAction(tr("E&xit"));
15 quitAction->setShortcut(tr("Ctrl+Q"));
16
17 QMenu *showMenu = new QMenu(tr("&Show"));
18
19 QAction *showTableAction = showMenu->addAction(tr("&Table"));
20
21 menuBar()->addMenu(fileMenu);
22 menuBar()->addMenu(showMenu);
23
24 editor = new QTextEdit();
25
26//! [0] //! [1]
27 QTextCursor cursor(editor->textCursor());
28//! [0]
29 cursor.movePosition(QTextCursor::Start);
30//! [1]
31
32 int rows = 11;
33 int columns = 4;
34
35//! [2]
36 QTextTableFormat tableFormat;
37 tableFormat.setBackground(QColor("#e0e0e0"));
38 QList<QTextLength> constraints;
39 constraints << QTextLength(QTextLength::PercentageLength, 16);
40 constraints << QTextLength(QTextLength::PercentageLength, 28);
41 constraints << QTextLength(QTextLength::PercentageLength, 28);
42 constraints << QTextLength(QTextLength::PercentageLength, 28);
43 tableFormat.setColumnWidthConstraints(constraints);
44//! [3]
45 QTextTable *table = cursor.insertTable(rows, columns, tableFormat);
46//! [2] //! [3]
47
48 int column;
49 int row;
50 QTextTableCell cell;
51 QTextCursor cellCursor;
52
53 QTextCharFormat charFormat;
54 charFormat.setForeground(Qt::black);
55
56//! [4]
57 cell = table->cellAt(0, 0);
58 cellCursor = cell.firstCursorPosition();
59 cellCursor.insertText(tr("Week"), charFormat);
60//! [4]
61
62//! [5]
63 for (column = 1; column < columns; ++column) {
64 cell = table->cellAt(0, column);
65 cellCursor = cell.firstCursorPosition();
66 cellCursor.insertText(tr("Team %1").arg(column), charFormat);
67 }
68
69 for (row = 1; row < rows; ++row) {
70 cell = table->cellAt(row, 0);
71 cellCursor = cell.firstCursorPosition();
72 cellCursor.insertText(tr("%1").arg(row), charFormat);
73
74 for (column = 1; column < columns; ++column) {
75 if ((row-1) % 3 == column-1) {
76//! [5] //! [6]
77 cell = table->cellAt(row, column);
78 QTextCursor cellCursor = cell.firstCursorPosition();
79 cellCursor.insertText(tr("On duty"), charFormat);
80 }
81//! [6] //! [7]
82 }
83//! [7] //! [8]
84 }
85//! [8]
86
87 connect(saveAction, &QAction::triggered, this, &MainWindow::saveFile);
88 connect(quitAction, &QAction::triggered, this, &MainWindow::close);
89 connect(showTableAction, &QAction::triggered, this, &MainWindow::showTable);
90
91 setCentralWidget(editor);
92 setWindowTitle(tr("Text Document Tables"));
93}
94
95void MainWindow::saveFile()
96{
97 QString fileName = QFileDialog::getSaveFileName(this,
98 tr("Save document as:"), "", tr("XML (*.xml)"));
99
100 if (!fileName.isEmpty()) {
101 if (writeXml(fileName))
102 setWindowTitle(fileName);
103 else
104 QMessageBox::warning(this, tr("Warning"),
105 tr("Failed to save the document."), QMessageBox::Cancel,
106 QMessageBox::NoButton);
107 }
108}
109
111{
112 QTextCursor cursor = editor->textCursor();
113 QTextTable *table = cursor.currentTable();
114
115 if (!table)
116 return;
117
118 QTableWidget *tableWidget = new QTableWidget(table->rows(), table->columns());
119
120//! [9]
121 for (int row = 0; row < table->rows(); ++row) {
122 for (int column = 0; column < table->columns(); ++column) {
123 QTextTableCell tableCell = table->cellAt(row, column);
124//! [9]
125 QTextFrame::iterator it;
126 QString text;
127 for (it = tableCell.begin(); !(it.atEnd()); ++it) {
128 QTextBlock childBlock = it.currentBlock();
129 if (childBlock.isValid())
130 text += childBlock.text();
131 }
132 QTableWidgetItem *newItem = new QTableWidgetItem(text);
133 tableWidget->setItem(row, column, newItem);
134 /*
135//! [10]
136 processTableCell(tableCell);
137//! [10]
138 */
139//! [11]
140 }
141//! [11] //! [12]
142 }
143//! [12]
144
145 tableWidget->setWindowTitle(tr("Table Contents"));
146 tableWidget->show();
147}
148
149void MainWindow::processFrame(QTextFrame *)
150{
151}
152
153void MainWindow::processBlock(QTextBlock)
154{
155}
156
157void MainWindow::processTable(QTextTable *table)
158{
159 QTextFrame *frame = qobject_cast<QTextFrame *>(table);
160//! [13]
161 QTextFrame::iterator it;
162 for (it = frame->begin(); !(it.atEnd()); ++it) {
163
164 QTextFrame *childFrame = it.currentFrame();
165 QTextBlock childBlock = it.currentBlock();
166
167 if (childFrame) {
168 QTextTable *childTable = qobject_cast<QTextTable*>(childFrame);
169
170 if (childTable)
171 processTable(childTable);
172 else
173 processFrame(childFrame);
174
175 } else if (childBlock.isValid()) {
176 processBlock(childBlock);
177 }
178 }
179//! [13]
180}
void saveFile()
void showTable()