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 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
27 QTextCursor cursor(editor->textCursor());
29 cursor.movePosition(QTextCursor::Start);
31
32 int rows = 11;
33 int columns = 4;
34
36 QTextTableFormat tableFormat;
37 tableFormat.setBackground(QColor("#e0e0e0"));
38 QList<QTextLength> constraints;
43 tableFormat.setColumnWidthConstraints(constraints);
45 QTextTable *table = cursor.insertTable(rows, columns, tableFormat);
47
48 int column;
49 int row;
50 QTextTableCell cell;
51 QTextCursor cellCursor;
52
53 QTextCharFormat charFormat;
54 charFormat.setForeground(Qt::black);
55
57 cell = table->cellAt(0, 0);
58 cellCursor = cell.firstCursorPosition();
59 cellCursor.insertText(tr("Week"), charFormat);
61
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) {
77 cell = table->cellAt(row, column);
78 QTextCursor cellCursor = cell.firstCursorPosition();
79 cellCursor.insertText(tr("On duty"), charFormat);
80 }
82 }
84 }
86
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
96{
98 tr("Save document as:"), "", tr("XML (*.xml)"));
99
100 if (!fileName.isEmpty()) {
101 if (writeXml(fileName))
103 else
104 QMessageBox::warning(this, tr("Warning"),
105 tr("Failed to save the document."), QMessageBox::Cancel,
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
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);
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 /*
136 processTableCell(tableCell);
138 */
140 }
142 }
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);
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 }
180}
void saveFile()
void showTable()
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...
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
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.
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
iterator begin()
Definition qset.h:136
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
The QTableWidgetItem class provides an item for use with the QTableWidget class.
The QTableWidget class provides an item-based table view with a default model.
void setItem(int row, int column, QTableWidgetItem *item)
Sets the item for the given row and column to item.
\reentrant
\reentrant \inmodule QtGui
Definition qtextcursor.h:30
void insertText(const QString &text)
Inserts text at the current position, using the current character format.
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.
void setForeground(const QBrush &brush)
Sets the foreground brush to the specified brush.
void setBackground(const QBrush &brush)
Sets the brush use to paint the document's background to the brush specified.
\reentrant
Definition qtextobject.h:81
\reentrant
Definition qtextformat.h:45
\reentrant
Definition qtexttable.h:19
QTextCursor firstCursorPosition() const
Returns the first valid cursor position in this cell.
void setColumnWidthConstraints(const QList< QTextLength > &constraints)
Sets the column width constraints for the table.
\reentrant
Definition qtexttable.h:63
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
QString text
QSet< QString >::iterator it
@ black
Definition qnamespace.h:30
GLenum GLenum GLsizei void GLsizei void * column
GLenum GLenum GLsizei void * row
GLenum GLenum GLsizei void * table
SSL_CTX int void * arg
#define tr(X)
QFrame frame
[0]
QMenuBar * menuBar
[0]