Qt 6.x
The Qt SDK
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
listing.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/*
5rsslisting.cpp
6
7Provides a widget for displaying news items from RDF news sources.
8RDF is an XML-based format for storing items of information (see
9http://www.w3.org/RDF/ for details).
10
11The widget itself provides a simple user interface for specifying
12the URL of a news source, and controlling the downloading of news.
13
14The widget downloads and parses the XML asynchronously, feeding the
15data to an XML reader in pieces. This allows the user to interrupt
16its operation, and also allows very large data sources to be read.
17*/
18
19
20#include <QtCore>
21#include <QtGui>
22#include <QtNetwork>
23#include <QtXml>
24
25#include "rsslisting.h"
26
27
28/*
29 Constructs an RSSListing widget with a simple user interface, and sets
30 up the XML reader to use a custom handler class.
31
32 The user interface consists of a line edit, two push buttons, and a
33 list view widget. The line edit is used for entering the URLs of news
34 sources; the push buttons start and abort the process of reading the
35 news.
36*/
37
38RSSListing::RSSListing(QWidget *parent)
40{
41 lineEdit = new QLineEdit(this);
42
43 fetchButton = new QPushButton(tr("Fetch"), this);
44 abortButton = new QPushButton(tr("Abort"), this);
45 abortButton->setEnabled(false);
46
47 treeWidget = new QTreeWidget(this);
48 QStringList headerLabels;
49 headerLabels << tr("Title") << tr("Link");
50 treeWidget->setHeaderLabels(headerLabels);
51
52 handler = 0;
53
54 connect(&http, SIGNAL(readyRead(QHttpResponseHeader)),
55 this, SLOT(readData(QHttpResponseHeader)));
56
57 connect(&http, SIGNAL(requestFinished(int,bool)),
58 this, SLOT(finished(int,bool)));
59
60 connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(fetch()));
61 connect(fetchButton, SIGNAL(clicked()), this, SLOT(fetch()));
62 connect(abortButton, SIGNAL(clicked()), &http, SLOT(abort()));
63
64 QVBoxLayout *layout = new QVBoxLayout(this);
65
66 QHBoxLayout *hboxLayout = new QHBoxLayout;
67
68 hboxLayout->addWidget(lineEdit);
69 hboxLayout->addWidget(fetchButton);
70 hboxLayout->addWidget(abortButton);
71
72 layout->addLayout(hboxLayout);
74
75 setWindowTitle(tr("RSS listing example"));
76}
77
78/*
79 Starts fetching data from a news source specified in the line
80 edit widget.
81
82 The line edit is made read only to prevent the user from modifying its
83 contents during the fetch; this is only for cosmetic purposes.
84 The fetch button is disabled, and the abort button is enabled to allow
85 the user to interrupt processing. The list view is cleared, and we
86 define the last list view item to be 0, meaning that there are no
87 existing items in the list.
88
89 We reset the flag used to determine whether parsing should begin again
90 or continue. A new handler is created, if required, and made available
91 to the reader.
92
93 The HTTP handler is supplied with the raw contents of the line edit and
94 a fetch is initiated. We keep the ID value returned by the HTTP handler
95 for future reference.
96*/
97
98void RSSListing::fetch()
99{
100 lineEdit->setReadOnly(true);
101 fetchButton->setEnabled(false);
102 abortButton->setEnabled(true);
103 treeWidget->clear();
104
105 lastItemCreated = 0;
106
107 newInformation = true;
108
109 if (handler != 0)
110 delete handler;
111 handler = new Handler;
112
113 xmlReader.setContentHandler(handler);
114 xmlReader.setErrorHandler(handler);
115
116 connect(handler, SIGNAL(newItem(QString&,QString&)),
117 this, SLOT(addItem(QString&,QString&)));
118
119 QUrl url(lineEdit->text());
120
121 http.setHost(url.host());
122 connectionId = http.get(url.path());
123}
124
125/*
126 Reads data received from the RDF source.
127
128 We read all the available data, and pass it to the XML
129 input source. The first time we receive new information,
130 the reader is set up for a new incremental parse;
131 we continue parsing using a different function on
132 subsequent calls involving the same data source.
133
134 If parsing fails for any reason, we abort the fetch.
135*/
136
137void RSSListing::readData(const QHttpResponseHeader &resp)
138{
139 bool ok;
140
141 if (resp.statusCode() != 200)
142 http.abort();
143 else {
144 xmlInput.setData(http.readAll());
145
146 if (newInformation) {
147 ok = xmlReader.parse(&xmlInput, true);
148 newInformation = false;
149 }
150 else
151 ok = xmlReader.parseContinue();
152
153 if (!ok)
154 http.abort();
155 }
156}
157
158/*
159 Finishes processing an HTTP request.
160
161 The default behavior is to keep the text edit read only.
162
163 If an error has occurred, the user interface is made available
164 to the user for further input, allowing a new fetch to be
165 started.
166
167 If the HTTP get request has finished, we perform a final
168 parsing operation on the data returned to ensure that it was
169 well-formed. Whether this is successful or not, we make the
170 user interface available to the user for further input.
171*/
172
173void RSSListing::finished(int id, bool error)
174{
175 if (error) {
176 qWarning("Received error during HTTP fetch.");
177 lineEdit->setReadOnly(false);
178 abortButton->setEnabled(false);
179 fetchButton->setEnabled(true);
180 }
181 else if (id == connectionId) {
182
183 bool ok = xmlReader.parseContinue();
184 if (!ok)
185 qWarning("Parse error at the end of input.");
186
187 lineEdit->setReadOnly(false);
188 abortButton->setEnabled(false);
189 fetchButton->setEnabled(true);
190 }
191}
192
193/*
194 Adds an item to the list view as it is reported by the handler.
195
196 We keep a record of the last item created to ensure that the
197 items are created in sequence.
198*/
199
200void RSSListing::addItem(QString &title, QString &link)
201{
203
204 item = new QTreeWidgetItem(treeWidget, lastItemCreated);
205 item->setText(0, title);
206 item->setText(1, link);
207
208 lastItemCreated = item;
209}
210
void addWidget(QWidget *, int stretch=0, Qt::Alignment alignment=Qt::Alignment())
Adds widget to the end of this box layout, with a stretch factor of stretch and alignment alignment.
void addLayout(QLayout *layout, int stretch=0)
Adds layout to the end of the box, with serial stretch factor stretch.
The QHBoxLayout class lines up widgets horizontally.
Definition qboxlayout.h:78
The QLineEdit widget is a one-line text editor.
Definition qlineedit.h:28
void setReadOnly(bool)
QString text
the line edit's text.
Definition qlineedit.h:32
The QPushButton widget provides a command button.
Definition qpushbutton.h:20
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
The QTreeWidgetItem class provides an item for use with the QTreeWidget convenience class.
Definition qtreewidget.h:23
The QTreeWidget class provides a tree view that uses a predefined tree model.
void clear()
Clears the tree widget by removing all of its items and selections.
void setHeaderLabels(const QStringList &labels)
Adds a column in the header for each item in the labels list, and sets the label for each column.
\inmodule QtCore
Definition qurl.h:94
QString host(ComponentFormattingOptions=FullyDecoded) const
Returns the host of the URL if it is defined; otherwise an empty string is returned.
Definition qurl.cpp:2337
QString path(ComponentFormattingOptions options=FullyDecoded) const
Returns the path of the URL.
Definition qurl.cpp:2465
The QVBoxLayout class lines up widgets vertically.
Definition qboxlayout.h:91
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
void setEnabled(bool)
Definition qwidget.cpp:3365
DBusConnection const char DBusError * error
#define qWarning
Definition qlogging.h:162
#define SLOT(a)
Definition qobjectdefs.h:51
#define SIGNAL(a)
Definition qobjectdefs.h:52
#define tr(X)
QUrl url("example.com")
[constructor-url-reference]
connect(quitButton, &QPushButton::clicked, &app, &QCoreApplication::quit, Qt::QueuedConnection)
QVBoxLayout * layout
QLineEdit * lineEdit
QString title
[35]
QByteArray readData()
QPushButton * abortButton
scene addItem(form)
QGraphicsItem * item
QTreeWidget * treeWidget
[0]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent