今天看了一點點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("↘",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(); }
利用QLabeltittleBar)控件,屏蔽了webkugou的上側如下圖的位置。屏蔽系統框架,重寫鼠標事件,實現窗口拖動。同時利用背景漸變,使其很自然過渡到播放界面。
使用了Qt的webkitwidgets,很方便地加載網頁內容。調試的時候需要將NPSWF32_11_8_800_168.dll或者其他版本的
NPSWF32
.dll)拷貝至Release的pugins目錄下,否則flash無法加載。需要使用Release調試,不知什麼原因Debug調試時,在加載flash時會崩潰。
本文出自 “水水的菜鳥” 博客,請務必保留此出處http://cpp51.blog.51cto.com/5346598/1308457