Qt加载网页(加载浏览器插件)和制作托盘后台运行(南信大财务报账看号)

时间:2021-12-24 16:16:51

程序模块要添加QNetWork和QWebKit模块:

nuistfinancevideo.h文件:

#ifndef NUISTFINANCEVIDEO_H
#define NUISTFINANCEVIDEO_H #include <QtGui/QtGui>
#include <QtNetwork/QNetworkReply>
#include <QtWebKit/QWebView>
#include <QtWebKit/QWebElementCollection>
#include <QtNetwork>
#include <QtWebKit/QWebFrame>
#include <QtWebKit>
#include "ui_nuistfinancevideo.h" class NuistFinanceVideo : public QMainWindow
{
Q_OBJECT public:
NuistFinanceVideo(QWidget *parent = 0, Qt::WFlags flags = 0);
~NuistFinanceVideo(); private:
Ui::NuistFinanceVideoClass ui; void createActions();
void createTrayIcon(); QSystemTrayIcon *trayIcon;//系统托盘对象
QMenu *trayIconMenu; //添加到系统托盘对象的菜单 QAction *minimizeAction;
QAction *restoreAction;
QAction *quitAction; void closeEvent(QCloseEvent *e);//程序关闭到托盘
void changeEvent(QEvent *e);//程序最小化到托盘 public slots:
void iconactivated(QSystemTrayIcon::ActivationReason reason);//响应鼠标点击托盘的事件
}; #endif // NUISTFINANCEVIDEO_H

nuistfinancevideo.cpp文件:

#include "nuistfinancevideo.h"

NuistFinanceVideo::NuistFinanceVideo(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
//设置支持中文语言
QTextCodec::setCodecForTr(QTextCodec::codecForName("system"));
//加载浏览器插件,将所需插件放在和代码同一目录下,这里用到WebVideoActiveX.ocx和npWebVideoPlugin.dll(一般安装在C:/Program Files/Web Components/),用于显示监控视频
ui.webView->page()->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
QWebSettings::globalSettings()->setAttribute(QWebSettings::PluginsEnabled, true);
//加载网页
QString url=tr("http://202.195.237.204/doc/page/login.asp");
ui.webView->load(QUrl(url));
//隐藏网页滚动条
ui.webView->page()->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
ui.webView->page()->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
//建立系统托盘图标
createActions();
createTrayIcon();
} void NuistFinanceVideo::createActions()
{
minimizeAction = new QAction(tr("最小化"), this);
connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
restoreAction = new QAction(tr("还原"), this);
connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
quitAction = new QAction(tr("退出"), this);
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); //设置右键点击时弹出的菜单
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(minimizeAction);
trayIconMenu->addAction(restoreAction);
trayIconMenu->addAction(quitAction);
} void NuistFinanceVideo::createTrayIcon()
{
if (!QSystemTrayIcon::isSystemTrayAvailable()) //判断系统是否支持系统托盘图标
{
return;
}
trayIcon = new QSystemTrayIcon(this);
trayIcon->setIcon(QIcon(tr(":/NuistFinanceVideo/Resources/icon.jpg")));
trayIcon->setToolTip(tr("南信大财务报账看号客户端"));
trayIcon->setContextMenu(trayIconMenu);
trayIcon->show();
connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(iconactivated(QSystemTrayIcon::ActivationReason)));
} //关闭到托盘
void NuistFinanceVideo::closeEvent(QCloseEvent *e)
{
e->ignore();
this->hide();
} //最小化到托盘
void NuistFinanceVideo::changeEvent(QEvent *e)
{
if((e->type()==QEvent::WindowStateChange)&&this->isMinimized())
{
QTimer::singleShot(100, this, SLOT(close()));
}
} //点击托盘事件
void NuistFinanceVideo::iconactivated(QSystemTrayIcon::ActivationReason reason)
{
//触发后台图标执行相应事件
switch (reason)
{
case QSystemTrayIcon::Trigger:
case QSystemTrayIcon::DoubleClick:
{
showNormal();
break;
}
case QSystemTrayIcon::MiddleClick:
break;
default:
break;
}
} NuistFinanceVideo::~NuistFinanceVideo()
{ }

main.cpp文件:

#include "nuistfinancevideo.h"
#include <QtGui/QApplication> int main(int argc, char *argv[])
{
QApplication a(argc, argv); NuistFinanceVideo w;
w.show();
return a.exec();
}