Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
"../include/mainwindow.h"
7
8
MainWindow::MainWindow(QWidget *parent)
9
: QMainWindow(parent)
10
{
11
setWindowTitle(
"Dock Widgets"
);
12
13
setupDockWindow();
14
setupContents();
15
setupMenus();
16
17
textBrowser =
new
QTextBrowser(
this
);
18
19
connect(headingList, &QListWidget::itemClicked,
20
this
, &MainWindow::updateText);
21
22
updateText(headingList->item(0));
23
headingList->setCurrentRow(0);
24
setCentralWidget(textBrowser);
25
}
26
27
void
MainWindow
::
setupContents
()
28
{
29
QFile titlesFile(
":/Resources/titles.txt"
);
30
titlesFile.open(QFile::ReadOnly);
31
int
chapter = 0;
32
33
do
{
34
QString line = titlesFile.readLine().trimmed();
35
QStringList parts = line.split(u'\t', Qt::SkipEmptyParts);
36
if
(parts.size() != 2)
37
break
;
38
39
QString chapterTitle = parts[0];
40
QString fileName = parts[1];
41
42
QFile chapterFile(fileName);
43
headingList =
new
QListWidget();
44
45
chapterFile.open(QFile::ReadOnly);
46
QListWidgetItem *item =
new
QListWidgetItem(chapterTitle, headingList);
47
item->setData(Qt::DisplayRole, chapterTitle);
48
item->setData(Qt::UserRole, chapterFile.readAll());
49
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
50
chapterFile.close();
51
52
chapter++;
53
}
while
(titlesFile.isOpen());
54
55
titlesFile.close();
56
}
57
58
void
MainWindow
::
setupDockWindow
()
59
{
60
//! [0]
61
contentsWindow =
new
QDockWidget(tr(
"Table of Contents"
),
this
);
62
contentsWindow->setAllowedAreas(Qt::LeftDockWidgetArea
63
| Qt::RightDockWidgetArea);
64
addDockWidget(Qt::LeftDockWidgetArea, contentsWindow);
65
66
headingList =
new
QListWidget(contentsWindow);
67
contentsWindow->setWidget(headingList);
68
//! [0]
69
}
70
71
void
MainWindow
::
setupMenus
()
72
{
73
QAction *exitAct =
new
QAction(tr(
"E&xit"
),
this
);
74
exitAct->setShortcut(tr(
"Ctrl+Q"
));
75
exitAct->setStatusTip(tr(
"Exit the application"
));
76
connect(exitAct, &QAction::triggered, qApp, &QApplication::quit);
77
78
QMenu *fileMenu = menuBar()->addMenu(tr(
"&File"
));
79
fileMenu->addAction(exitAct);
80
}
81
82
void
MainWindow
::
updateText
(QListWidgetItem *item)
83
{
84
QString text = item->data(Qt::UserRole).toString();
85
textBrowser->setHtml(text);
86
}
MainWindow
[6]
Definition
src_corelib_kernel_qmetaobject.cpp:74
MainWindow::setupMenus
void setupMenus()
Definition
mainwindow.cpp:71
MainWindow::setupDockWindow
void setupDockWindow()
Definition
mainwindow.cpp:58
MainWindow::setupContents
void setupContents()
Definition
doc_src_qt4-mainwindow.cpp:15
MainWindow::updateText
void updateText(QListWidgetItem *item)
Definition
mainwindow.cpp:82
qtbase
src
widgets
doc
snippets
dockwidgets
mainwindow.cpp
Generated on
for Qt by
1.18.0