Qt Code:
  1. //main.cpp
  2. #include <QtGui>
  3.  
  4. #include "window.h"
  5. #include "mainwindow.h"
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9. Q_INIT_RESOURCE(systray);
  10.  
  11. QApplication app(argc, argv);
  12. QApplication::setQuitOnLastWindowClosed(false);
  13.  
  14. Window box;
  15.  
  16. MainWindow window;
  17. window.setParent(&box);
  18. return app.exec();
  19. }
  20.  
  21. //mainwindow.cpp
  22. #include <QtGui>
  23.  
  24. class QWebView;
  25.  
  26.  
  27. //! [1]
  28. class MainWindow : public QMainWindow
  29. {
  30. Q_OBJECT
  31.  
  32. public:
  33. MainWindow();
  34.  
  35. private:
  36. QString jQuery;
  37. QWebView *view;
  38.  
  39. };
  40.  
  41. //window.h
  42. #ifndef WINDOW_H
  43. #define WINDOW_H
  44.  
  45. #include <QDialog>
  46. #include <QSystemTrayIcon>
  47. #include <QMenu>
  48. #include <QLineEdit>
  49. #include <QTextEdit>
  50.  
  51.  
  52. class Window : public QDialog
  53. {
  54. Q_OBJECT
  55. public:
  56. Window();
  57.  
  58. private slots:
  59. void setIcon();
  60. void iconActivated(QSystemTrayIcon::ActivationReason reason);
  61. void showMessage();
  62.  
  63. private:
  64. void createActions();
  65. void createTrayIcon();
  66.  
  67.  
  68. QAction *minimizeAction;
  69. QAction *maximizeAction;
  70. QAction *restoreAction;
  71. QAction *quitAction;
  72. QLineEdit *titleEdit;
  73. QTextEdit *bodyEdit;
  74.  
  75. QSystemTrayIcon *trayIcon;
  76. QMenu *trayIconMenu;
  77.  
  78.  
  79. };
  80.  
  81. #endif
  82.  
  83. //window.cpp
  84.  
  85. #include <QtGui>
  86. #include <QtWebKit>
  87. #include <QTime>
  88.  
  89. #include "window.h"
  90. #include "mainwindow.h"
  91.  
  92. Window::Window()
  93. {
  94. createActions();
  95. createTrayIcon();
  96. setIcon();
  97.  
  98. connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
  99. this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
  100.  
  101.  
  102. trayIcon->show();
  103. setWindowTitle(tr("Blog Search"));
  104. resize(600, 600);
  105. }
  106. MainWindow::MainWindow()
  107. {
  108.  
  109. QFile file;
  110. file.setFileName(":/jquery.min.js");
  111. file.open(QIODevice::ReadOnly);
  112. jQuery = file.readAll();
  113. file.close();
  114.  
  115.  
  116. QNetworkProxyFactory::setUseSystemConfiguration(true);
  117.  
  118.  
  119. QTime time = QTime::currentTime();
  120. qsrand((uint)time.msec());
  121. int randomValue = qrand() % 999;
  122. view = new QWebView(this);
  123.  
  124. QUrl url = QUrl::fromEncoded("http://blogsearch.google.com/blogsearch?hl=tr&ie=UTF-8&q=C%2B%2B&&output=rss");
  125. url.addQueryItem("num", "1");
  126. url.addQueryItem("start" ,QString::number(randomValue));
  127. view->load(url);
  128.  
  129.  
  130. setCentralWidget(view);
  131. }
  132.  
  133. void Window::setIcon()
  134. {
  135. QIcon icon;
  136. icon.addPixmap(QPixmap(":/images/heart.svg"));
  137. trayIcon->setIcon(icon);
  138. trayIcon->setVisible(true);
  139. setWindowIcon(icon);
  140.  
  141. }
  142. void Window::iconActivated(QSystemTrayIcon::ActivationReason reason)
  143. {
  144. switch (reason) {
  145. case QSystemTrayIcon::Trigger:
  146. break;
  147. case QSystemTrayIcon::DoubleClick:
  148. showNormal();
  149. break;
  150. case QSystemTrayIcon::MiddleClick:
  151. showMessage();
  152. break;
  153. default:
  154. ;
  155. }
  156. }
  157.  
  158. void Window::showMessage()
  159. {
  160. titleEdit = new QLineEdit(tr("Blog Search"));
  161. bodyEdit = new QTextEdit;
  162. bodyEdit->setPlainText(trUtf8("Blog okumak icin sag click + Maximize\n yapmaniz yeterli"));
  163.  
  164. trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), QSystemTrayIcon::Information,
  165. 1000);
  166. }
  167.  
  168. void Window::createActions()
  169. {
  170. minimizeAction = new QAction(tr("Mi&nimize"), this);
  171. connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
  172.  
  173. maximizeAction = new QAction(tr("Ma&ximize"), this);
  174. connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
  175.  
  176. restoreAction = new QAction(tr("&Restore"), this);
  177. connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
  178.  
  179. quitAction = new QAction(tr("&Quit"), this);
  180. connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
  181. }
  182.  
  183. void Window::createTrayIcon()
  184. {
  185. trayIconMenu = new QMenu(this);
  186. trayIconMenu->addAction(minimizeAction);
  187. trayIconMenu->addAction(maximizeAction);
  188. trayIconMenu->addAction(restoreAction);
  189. trayIconMenu->addSeparator();
  190. trayIconMenu->addAction(quitAction);
  191.  
  192. trayIcon = new QSystemTrayIcon(this);
  193. trayIcon->setContextMenu(trayIconMenu);
  194. }
To copy to clipboard, switch view to plain text mode