1.IDE:QTCreator
2.实验:制作一个mps播放器,界面可以实现鼠标拖动
3.记录:
(1)network网络多线程
(2)Http
(3)QJsonObject封装JSON对象
(4)QMediaPlayer/Playist类应用
(5)SQLite存储搜索歌曲
A.画布覆盖
void Widget::paintEvent(QPaintEvent *) //播放器画布
{
QPainter paint(this);
paint.drawPixmap(0,0,width(),height(),QPixmap(":/imag/picture/background.jpg")); //绘制画布
}
B.皮肤,信息,退出按钮
C. 按钮图标透明化
a.勾选flat
b.在style-sheet填入以下代码
background-color:transparent
c.禁止窗口大小改变,关闭窗口标题
this->setFixedSize(this->geometry().size()); //设定窗口固定大小
this->setWindowFlag(Qt::FramelessWindowHint); //关闭窗口标题
此时运行效果
d.加入搜索栏,UI设计
在如图两个按钮的style中加入以下代码,将搜索栏放在一个GroupBox内
color:RGB(255,255,255);
background-color:transparent;
e.在pro文件中加入 network multimedia sql
QT += core gui network multimedia sql
f.将以下库加入到widget.h文件中(QMediaPlaylist已经被移除)
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QEventLoop>
#include <QJsonArray>
#include <QJsonObject>
#include <QMediaPlayer>
//#include <QMediaPlaylist>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QMessageBox>
#include <QTime>
#include <math.h>
#include <QMouseEvent>
4.代码
pro
QT += core gui network multimedia sql
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
FORMS += \
widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += \
img.qrc
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QEventLoop>
#include <QJsonArray>
#include <QJsonObject>
#include <QMediaPlayer>
//#include <QMediaPlaylist>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QMessageBox>
#include <QTime>
#include <math.h>
#include <QMouseEvent>
QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
void paintEvent(QPaintEvent *); //处理背景画布(函数重新定义)
private slots:
void on_leave_pb_clicked(); //离开按钮按下时
private:
Ui::Widget *ui;
protected:
//音乐歌曲下载和播放
void DownloadPlayer(QString album_id,QString hash);
//访问HTTP网页
void HttpAccessFunc(QString);
//音乐的hash播放和ablum_id解析(使用JSON)
void HashJsonAnalysis(QByteArray);
//搜索音乐数据信息的JSON解析,解析出真正的音乐文件和歌词
QString MusicJsonAnalysis(QByteArray);
protected:
//鼠标拖住窗口实现移动
void mouseMoveEvent(QMouseEvent *event); //移动事件
void mousePressEvent(QMouseEvent *event); //按下事件
void mouseReleaseEvent(QMouseEvent *event); //释放事件
private:
//定义坐标
QPoint m_mousepoint;
QPoint movement;
bool mousepress;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QPainter>
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setFixedSize(this->geometry().size()); //设定窗口固定大小
this->setWindowFlag(Qt::FramelessWindowHint); //关闭窗口标题
}
Widget::~Widget()
{
delete ui;
}
void Widget::paintEvent(QPaintEvent *) //播放器画布
{
QPainter paint(this);
paint.drawPixmap(0,0,width(),height(),QPixmap(":/imag/picture/background.jpg")); //绘制画布
}
void Widget::on_leave_pb_clicked() //关闭按钮
{
close();
}
/******************************************************************************************************/
//音乐歌曲下载和播放
void Widget::DownloadPlayer(QString album_id,QString hash)
{
}
//访问HTTP网页
void Widget::HttpAccessFunc(QString)
{
}
//音乐的hash播放和ablum_id解析(使用JSON)
void Widget::HashJsonAnalysis(QByteArray)
{
}
//搜索音乐数据信息的JSON解析,解析出真正的音乐文件和歌词
QString Widget::MusicJsonAnalysis(QByteArray)
{
}
/******************************************************************************************************/
void Widget::mouseMoveEvent(QMouseEvent *event) //移动事件
{
if(mousepress)
{
QPoint movePos=event->globalPos(); //窗口初始位置
move(movePos-m_mousepoint);
}
}
void Widget::mousePressEvent(QMouseEvent *event) //按下事件
{
if(event->button()==Qt::LeftButton)
mousepress=true;
m_mousepoint=event->globalPos()-pos();
}
void Widget::mouseReleaseEvent(QMouseEvent *event) //释放事件
{
Q_UNUSED(event);
// mousepress=false;
}