Results 1 to 3 of 3

Thread: RSS Reader

  1. #1
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default RSS Reader

    My second little Qt application, a simple RSS reader. Error handling is minimal but it works nicely so long as the RSS feed is valid. Main code below and package attached.
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. QToolBar * tb = addToolBar(tr("Open"));
    5.  
    6. /* Open rss feed combo. */
    7. combo = new QComboBox;
    8. ListView * lv = new ListView;
    9. combo->setView(lv);
    10. combo->setEditable(true);
    11. combo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    12. connect(combo, SIGNAL(activated(int)), this, SLOT(openRssFeed()));
    13. tb->addWidget(combo);
    14.  
    15. /* Open RSS Feed button. */
    16. QAction * act = new QAction(tr("Open RSS Feed"), this);
    17. act->setIcon(style()->standardIcon(QStyle::SP_BrowserReload));
    18. connect(act, SIGNAL(triggered()), this, SLOT(openRssFeed()));
    19. tb->addAction(act);
    20.  
    21. /* List-view to display list of stories. */
    22. tv = new QListView;
    23. connect(tv, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(listViewDoubleClicked(QModelIndex)));
    24. model = new QStandardItemModel(0, 1, this);
    25. tv->setModel(model);
    26.  
    27. /* Progress bar to signal progress of RSS feed download and web page load. */
    28. progress = new QProgressBar;
    29. statusBar()->addPermanentWidget(progress);
    30.  
    31. /* Web-view to display articles. */
    32. wv = new QWebView;
    33. wv->load(QUrl("about:blank"));
    34. connect(wv, SIGNAL(loadProgress(int)), progress, SLOT(setValue(int)));
    35.  
    36. QSplitter * splitter = new QSplitter;
    37. splitter->addWidget(tv);
    38. splitter->addWidget(wv);
    39. this->setCentralWidget(splitter);
    40.  
    41. /* Load saved rss feeds. */
    42. QSettings settings;
    43. int size = settings.beginReadArray("rssFeeds");
    44. for (int i = 0; i < size; i++)
    45. {
    46. settings.setArrayIndex(i);
    47. QString url = settings.value("url").toString();
    48. combo->addItem(url);
    49. }
    50. settings.endArray();
    51.  
    52. if (size == 0)
    53. {
    54. /* Bonus RSS feed if the list is empty. */
    55. combo->addItem("http://www.qtcentre.org/forum/external.php?type=RSS2");
    56. }
    57.  
    58. /* Set up the network manager. */
    59. manager = new QNetworkAccessManager(this);
    60. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
    61. }
    62.  
    63. MainWindow::~MainWindow()
    64. {
    65. /* Save our list of RSS feeds. */
    66. QSettings settings;
    67. settings.beginWriteArray("rssFeeds");
    68. settings.remove("");
    69. for (int i = 0; i < combo->count(); i++)
    70. {
    71. settings.setArrayIndex(i);
    72. settings.setValue("url", combo->itemText(i));
    73. }
    74. settings.endArray();
    75. }
    76.  
    77. void MainWindow::listViewDoubleClicked(const QModelIndex &index)
    78. {
    79. /* Load article in the web-view. */
    80. QString strLink = index.data(Qt::UserRole).toString();
    81. wv->load(QUrl(strLink));
    82. }
    83.  
    84. void MainWindow::openRssFeed()
    85. {
    86. /* Search the combo box for duplicates before adding this item. */
    87. int i = combo->findText(combo->currentText());
    88.  
    89. if (i != -1)
    90. {
    91. combo->setCurrentIndex(i);
    92. }
    93. else
    94. {
    95. combo->addItem(combo->currentText());
    96. combo->setCurrentIndex(combo->count() - 1);
    97. }
    98.  
    99. reply = manager->get(QNetworkRequest(QUrl(combo->currentText())));
    100. connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
    101. }
    102.  
    103. void MainWindow::downloadProgress(qint64 bytes, qint64 bytesTotal)
    104. {
    105. if (bytesTotal == -1)
    106. {
    107. /* No total bytes available, just set the progress bar to show a busy indicator. */
    108. progress->setMinimum(0);
    109. progress->setMaximum(0);
    110. }
    111. else
    112. {
    113. progress->setMaximum(100);
    114. int percent = bytes * 100 / bytesTotal;
    115. progress->setValue(percent);
    116. }
    117. }
    118.  
    119. void MainWindow::replyFinished(QNetworkReply * netReply)
    120. {
    121. QString str (netReply->readAll());
    122.  
    123. /* If we are redirected, try again. TODO: Limit redirection count. */
    124. QVariant vt = netReply->attribute(QNetworkRequest::RedirectionTargetAttribute);
    125.  
    126. delete reply;
    127.  
    128. if (!vt.isNull())
    129. {
    130. qDebug() << "Redirected to:" << vt.toUrl().toString();
    131. reply = manager->get(QNetworkRequest(vt.toUrl()));
    132. connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
    133. }
    134. else
    135. {
    136. /* We have something. */
    137. QString error;
    138. if (!doc.setContent(str, false, &error))
    139. {
    140. wv->setHtml(QString("<h1>Error</h1>") + error);
    141. }
    142. else
    143. {
    144. QDomElement docElem = doc.documentElement();
    145. QDomNodeList nodeList = docElem.elementsByTagName("item");
    146.  
    147. model->clear();
    148. model->insertColumn(0);
    149.  
    150. for (uint i = 0; i < nodeList.length(); i++)
    151. {
    152. QDomNode node = nodeList.item(i);
    153. QDomElement e = node.toElement();
    154. QString strTitle = e.elementsByTagName("title").item(0).firstChild().nodeValue();
    155. QString strLink = e.elementsByTagName("link").item(0).firstChild().nodeValue();
    156. QString strDescription = e.elementsByTagName("description").item(0).firstChild().nodeValue();
    157. QString strToolTip = "<b>" + strTitle + "</b>" + "<br /><br />" + strDescription + "<br /><br />" + strLink;
    158.  
    159. model->insertRows(model->rowCount(), 1);
    160. QModelIndex index = model->index(model->rowCount() - 1, 0);
    161. model->setData(index, strTitle, Qt::DisplayRole);
    162. model->setData(index, style()->standardIcon(QStyle::SP_FileIcon), Qt::DecorationRole);
    163. model->setData(index, strToolTip, Qt::ToolTipRole);
    164. model->setData(index, strLink, Qt::UserRole);
    165. model->itemFromIndex(index)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
    166. }
    167. }
    168. }
    169. }
    170.  
    171.  
    172. /* Implements a list-view which supports the delete key. */
    173. void ListView::keyPressEvent(QKeyEvent * event)
    174. {
    175. if (event->key() == Qt::Key_Delete)
    176. {
    177. event->accept();
    178. QModelIndexList l = selectedIndexes();
    179. if (l.length() > 0)
    180. {
    181. model()->removeRow(l.at(0).row(), l.at(0).parent());
    182. }
    183. }
    184. else
    185. {
    186. QListView::keyPressEvent(event);
    187. }
    188. }
    189.  
    190. int main(int argc, char *argv[])
    191. {
    192. QApplication a(argc, argv);
    193. MainWindow w;
    194. w.show();
    195. return a.exec();
    196. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images
    Attached Files Attached Files

  2. The following 3 users say thank you to numbat for this useful post:

    cOdE_rEd (24th February 2011), iceheaven31 (6th April 2011), vasanth (23rd July 2010)

  3. #2
    Join Date
    Mar 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: RSS Reader

    Thanks !

  4. #3
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: RSS Reader

    i think the first line in your .pro file should be:

    Qt Code:
    1. QT += webkit network xml
    To copy to clipboard, switch view to plain text mode 

    anyway, thanks for this sample!

Similar Threads

  1. Replies: 4
    Last Post: 27th June 2012, 10:41
  2. reseting reader
    By uchennaanyanwu in forum Newbie
    Replies: 1
    Last Post: 1st August 2008, 20:28
  3. xml reader
    By mickey in forum General Programming
    Replies: 5
    Last Post: 13th January 2008, 18:37
  4. Qt + Barcode Reader
    By sunil.thaha in forum General Discussion
    Replies: 12
    Last Post: 13th November 2007, 12:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.