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 "../include/mainwindow.h"
7
8MainWindow::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
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
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
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
82void MainWindow::updateText(QListWidgetItem *item)
83{
84 QString text = item->data(Qt::UserRole).toString();
85 textBrowser->setHtml(text);
86}
void setupMenus()
void setupDockWindow()
void updateText(QListWidgetItem *item)