Chinaunix首页 | 论坛 | 博客
  • 博客访问: 323285
  • 博文数量: 50
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1387
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-24 10:12
文章分类
文章存档

2014年(18)

2013年(32)

分类: 网络与安全

2013-10-15 10:23:03

 今天看了一点点QWebView,于是有了把web.kugou.com做成客户端的想法。折腾了一天,这是最终效果:


192957474.jpg


193400888.jpg


因为默认加载蓝色界面,所以界面适配默认蓝色。


Qt版本为5.1.1,下面是代码:

WebKugouWidget.pro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    
#-------------------------------------------------
#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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    
//webkugouwidget.h--------------------
#ifndef WIDGET_H
#define WIDGET_H
#include
#include
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
    
//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(""));
    //经过试验如下设置比较好
    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
1
2
3
4
5
6
7
8
9
10
    
//main.cpp-----------------------
#include "webkugouwidget.h"
#include
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    WebKugouWidget w;
    w.show();
    return a.exec();
}
阅读(1985) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~