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 : QMainWindow(parent)
10{
11 setWindowTitle("Dock Widgets");
12
13 setupDockWindow();
14 setupContents();
15 setupMenus();
16
17 textBrowser = new QTextBrowser(this);
18
20 this, &MainWindow::updateText);
21
22 updateText(headingList->item(0));
23 headingList->setCurrentRow(0);
24 setCentralWidget(textBrowser);
25}
26
27void 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();
36 if (parts.size() != 2)
37 break;
38
39 QString chapterTitle = parts[0];
40 QString fileName = parts[1];
41
42 QFile chapterFile(fileName);
43
44 chapterFile.open(QFile::ReadOnly);
45 QListWidgetItem *item = new QListWidgetItem(chapterTitle, headingList);
46 item->setData(Qt::DisplayRole, chapterTitle);
47 item->setData(Qt::UserRole, chapterFile.readAll());
49 chapterFile.close();
50
51 chapter++;
52 } while (titlesFile.isOpen());
53
54 titlesFile.close();
55}
56
57void MainWindow::setupDockWindow()
58{
60 contentsWindow = new QDockWidget(tr("Table of Contents"), this);
61 contentsWindow->setAllowedAreas(Qt::LeftDockWidgetArea
63 addDockWidget(Qt::LeftDockWidgetArea, contentsWindow);
64
65 headingList = new QListWidget(contentsWindow);
66 contentsWindow->setWidget(headingList);
68}
69
70void MainWindow::setupMenus()
71{
72 QAction *exitAct = new QAction(tr("E&xit"), this);
73 exitAct->setShortcut(tr("Ctrl+Q"));
74 exitAct->setStatusTip(tr("Exit the application"));
76
77 QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
78 fileMenu->addAction(exitAct);
79}
80
81void MainWindow::updateText(QListWidgetItem *item)
82{
84 textBrowser->setHtml(text);
85}
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...
static void quit()
\threadsafe
The QDockWidget class provides a widget that can be docked inside a QMainWindow or floated as a top-l...
Definition qdockwidget.h:20
\inmodule QtCore
Definition qfile.h:93
void setData(int key, const QVariant &value)
Sets this item's custom data for the key key to value.
void setFlags(GraphicsItemFlags flags)
Sets the item flags to flags.
QVariant data(int key) const
Returns this item's custom data for the key key as a QVariant.
The QListWidgetItem class provides an item for use with the QListWidget item view class.
Definition qlistwidget.h:23
The QListWidget class provides an item-based list widget.
void itemClicked(QListWidgetItem *item)
This signal is emitted with the specified item when a mouse button is clicked on an item in the widge...
The QMainWindow class provides a main application window.
Definition qmainwindow.h:25
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 QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QStringList split(const QString &sep, Qt::SplitBehavior behavior=Qt::KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Splits the string into substrings wherever sep occurs, and returns the list of those strings.
Definition qstring.cpp:8218
QString trimmed() const &
Definition qstring.h:447
The QTextBrowser class provides a rich text browser with hypertext navigation.
QString toString() const
Returns the variant as a QString if the variant has a userType() including, but not limited to:
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
QString text
@ RightDockWidgetArea
@ LeftDockWidgetArea
@ UserRole
@ DisplayRole
@ SkipEmptyParts
Definition qnamespace.h:128
@ ItemIsSelectable
@ ItemIsEnabled
#define qApp
#define tr(X)
connect(quitButton, &QPushButton::clicked, &app, &QCoreApplication::quit, Qt::QueuedConnection)
QGraphicsItem * item
QMenuBar * menuBar
[0]