今天看了一点点QWebView,于是有了把web.kugou.com做成客户端的想法。折腾了一天,这是最终效果:
因为默认加载蓝色界面,所以界面适配默认蓝色。
Qt版本为5.1.1,下面是代码:
WebKugouWidget.pro
#------------------------------------------------- #WebKugouWidget.pro #------------------------------------------------- # # Project created by QtCreator 2013-10-13T11:45:54 # #------------------------------------------------- QT += core gui webkitwidgets greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = WebKugouWidget TEMPLATE = app SOURCES += main.cpp \ webkugouwidget.cpp HEADERS += \ webkugouwidget.h
webkugouwidget.h
//webkugouwidget.h-------------------- #ifndef WIDGET_H #define WIDGET_H #include<QtWebKitWidgets> #include <QtWidgets> class WebKugouWidget : public QWidget { Q_OBJECT public: WebKugouWidget(QWidget *parent = 0); ~WebKugouWidget(); private: QWebView* kgwebView; QLabel* tittleBar; QToolButton* closeButton; QPushButton* minToTrayBtn; QPushButton* minButton; private: //重写鼠标事件,实现窗口移动 bool isPress; QPoint dragPosition; void mousePressEvent(QMouseEvent *); void mouseMoveEvent(QMouseEvent *); void mouseReleaseEvent(QMouseEvent *); private slots: void emitminimizedTOTray(); //关闭 void closeEx(); signals: //为了方便这些代码嵌入到别的项目中,没实现最小化到托盘 //点击最小化到托盘,发出以下信号 void minimizedTOTray(); }; #endif // WIDGET_H
webkugouwidget.cpp
//webkugouwidget.cpp---------------------------- #include "webkugouwidget.h" WebKugouWidget::WebKugouWidget(QWidget *parent) : QWidget(parent),isPress(false) { //设置插件路径,fash插件---------------------- //将NPSWF32_11_8_800_168.dll拷贝至pugins目录下 QApplication::addLibraryPath("./plugins"); QWebSettings *settings = QWebSettings::globalSettings(); settings->setAttribute(QWebSettings::PluginsEnabled, true);//允许插件 settings->setAttribute(QWebSettings::JavascriptEnabled, true);//JavaScript settings->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);// settings->setAttribute(QWebSettings::JavascriptCanOpenWindows, true); //将系统边框去掉 setWindowFlags(Qt::FramelessWindowHint); kgwebView = new QWebView(this); kgwebView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks); //关闭水平滚动条 kgwebView->page()->mainFrame()->setScrollBarPolicy(Qt::Horizontal,Qt::ScrollBarAlwaysOff); kgwebView->load(QUrl("http://web.kugou.com/index.html?")); //经过试验如下设置比较好 kgwebView->setGeometry(-1,0,740,508); //默认界面的rgb为rgb(1,160,234) kgwebView->setStyleSheet("rgba(1,160,234,0);"); //用于显示WebKugou,窗口拖动以及屏蔽web酷狗上侧控件; tittleBar=new QLabel(this); tittleBar->setGeometry(0,0,747,30); tittleBar->setText(" WebKugou"); tittleBar->setCursor(Qt::SizeAllCursor); this->resize(740,510); //关闭按钮 closeButton=new QToolButton(this); connect(closeButton,SIGNAL(clicked()),this,SLOT(closeEx())); closeButton->setGeometry(740-25,0,25,25); closeButton->setText("×"); closeButton->setToolTip(tr("关闭")); //最小化到托盘 minToTrayBtn=new QPushButton("�K",this); minToTrayBtn->setGeometry(740-25-25,0,25,25); minToTrayBtn->setToolTip(tr("最小化到托盘")); connect(minToTrayBtn,SIGNAL(clicked()),this,SLOT(emitminimizedTOTray())); //最小化 minButton=new QPushButton("-",this); connect(minButton,SIGNAL(clicked()),this,SLOT(showMinimized())); minButton->setGeometry(740-25-25-25,0,25,25); minButton->setToolTip(tr("最小化")); //设置风格表 this->setStyleSheet(QString("WebKugouWidget{border:5px solid gray;border-color:rgb(1,160,234);font-size:12pt;rgb(1,160,234);color:white}" "QToolButton{font:13pt \"Arial\";color:white;rgba(0,0,0,0);}" "QToolButton::hover{border:0px solid;color:black;background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 rgba(254, 67, 101, 220), stop:1 rgba(254, 67, 101, 0));}" "QPushButton{color:white;rgba(0,0,0,0);}" "QPushButton::hover{border:0px solid;color:black;background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 rgba(170, 138, 87, 220), stop:1 rgba(170, 138, 87, 0));}" "QLabel{color:white;font-size:15pt;background:qlineargradient(x1:0,y1:0,x2:0,y2:1,stop:0 rgb(3,208,234),stop:0.8 rgb(1,160,234),stop:1 rgb(1,160,234));}")); } WebKugouWidget::~WebKugouWidget() { } void WebKugouWidget::closeEx() { this->hide(); exit(0); } void WebKugouWidget::emitminimizedTOTray() { emit minimizedTOTray(); } void WebKugouWidget::mousePressEvent(QMouseEvent *event) { if(event->button()==Qt::LeftButton) { dragPosition=event->globalPos()-this->pos(); isPress=true; event->accept(); } } void WebKugouWidget::mouseMoveEvent(QMouseEvent *event) { if(isPress&&event->buttons()&&Qt::LeftButton) { move(event->globalPos()-dragPosition); event->accept(); } } void WebKugouWidget::mouseReleaseEvent(QMouseEvent *event) { if(event->button()==Qt::LeftButton) { isPress=false; event->accept(); } }
main.cpp
//main.cpp----------------------- #include "webkugouwidget.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); WebKugouWidget w; w.show(); return a.exec(); }
利用QLabel(tittleBar)控件,屏蔽了webkugou的上侧如下图的位置。屏蔽系统框架,重写鼠标事件,实现窗口拖动。同时利用背景渐变,使其很自然过渡到播放界面。
使用了Qt的webkitwidgets,很方便地加载网页内容。调试的时候需要将NPSWF32_11_8_800_168.dll(或者其他版本的
NPSWF32
.dll)拷贝至Release的pugins目录下,否则flash无法加载。需要使用Release调试,不知什么原因Debug调试时,在加载flash时会崩溃。