天天看点

QT mp3音乐播放器实现框架,Qt鼠标事件,网络编程,QSqlite,Json解析,HTTP请求等框架搭建UI设计mp3.hmp3.cpp隐藏窗口标题 最大化 最小化 关闭

QT mp3音乐播放器实现框架,Qt鼠标事件,网络编程,QSqlite,Json解析,HTTP请求等

  • 框架搭建
  • UI设计
  • mp3.h
  • mp3.cpp
  • 隐藏窗口标题 最大化 最小化 关闭

框架搭建

.pro添加

# 网络 添加多媒体 数据库
QT       += network multimedia sql
           

添加头文件

#include <QWidget>
#include <QNetworkRequest>               //HTTP的URL请求管理类
#include <QNetworkAccessManager>   //网络入口
#include <QNetworkReply>                   //网络应答
#include <QEventLoop>                        //网络请求
#include <QJsonArray>                         //解析数组
#include <QJsonObject>                      //解析对象
#include <QMediaPlayer>                    //媒体播放
#include <QMediaPlaylist>                   //媒体播放列表
#include <QSqlDatabase>                    //数据库
#include <QSqlQuery>                         //数据库查询
#include <QSqlQueryModel>               //查询模式
#include <QSqlError>                           //数据库报错
#include <QMessageBox>                   //弹出信息框
#include <QTime>                               //hh:mm:ss
#include <math.h>                              //数学
#include <QDebug>
#include <QMoveEvent>                    //移动事件
#include <QScreen>                          //屏幕
//代码中引入头文件
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))//qt当前版本>=qt5
#include <QtWidgets>
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))//qt当前版本>=qt6
#include <QtCore5Compat>
#endif

//.默认Qt6开启了高分屏支持,界面会变得很大,甚至字体发虚,很多人会不习惯,
//因为这种模式如果程序很多坐标计算没有采用devicePixelRatio进行运算的话,
//100%会出现奇奇怪怪的问题,因为坐标不准确了。要取消这种效果可以设置高分屏缩放因子。
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::Floor);
#endif

#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
    std::sort(ipv4s.begin(), ipv4s.end());
#endif

           

添加资源文件夹

QT mp3音乐播放器实现框架,Qt鼠标事件,网络编程,QSqlite,Json解析,HTTP请求等框架搭建UI设计mp3.hmp3.cpp隐藏窗口标题 最大化 最小化 关闭

UI设计

QT mp3音乐播放器实现框架,Qt鼠标事件,网络编程,QSqlite,Json解析,HTTP请求等框架搭建UI设计mp3.hmp3.cpp隐藏窗口标题 最大化 最小化 关闭
QT mp3音乐播放器实现框架,Qt鼠标事件,网络编程,QSqlite,Json解析,HTTP请求等框架搭建UI设计mp3.hmp3.cpp隐藏窗口标题 最大化 最小化 关闭

按钮风格

QPushButton
QPushButton{
border-radius:0px;
background-repeat:no-repeat;
border:none;
background-position:canter canter;
}
QPushButton:hover
{
	background-color: rgba(230, 0, 0, 5);
}
           
color: rgb(255, 255, 255);/*字体颜色白色*/
background-color: rgba(255, 255, 255,50);/*背景色 透明度50*/
font: 75 12pt "Arial";
           
QLineEdit
color: rgb(255, 255, 255);/*字体颜色白色*/
background-color: rgba(255, 255, 255,50);/*背景色 透明度50*/
font: 75 12pt "Arial";
border:2px solid;
border-color: rgb(0, 255, 0);
           

mp3.h

#ifndef MP3_H
#define MP3_H

#include <QWidget>
#include <QNetworkRequest>               //HTTP的URL请求管理类
#include <QNetworkAccessManager>   //网络入口
#include <QNetworkReply>                   //网络应答
#include <QEventLoop>                        //网络请求
#include <QJsonArray>                         //解析数组
#include <QJsonObject>                      //解析对象
#include <QMediaPlayer>                    //媒体播放
#include <QMediaPlaylist>                   //媒体播放列表
#include <QSqlDatabase>                    //数据库
#include <QSqlQuery>                         //数据库查询
#include <QSqlQueryModel>               //查询模式
#include <QSqlError>                           //数据库报错
#include <QMessageBox>                   //弹出信息框
#include <QTime>                               //hh:mm:ss
#include <math.h>                              //数学
#include <QDebug>
#include <QMoveEvent>                    //移动事件
#include <QScreen>                          //屏幕
//代码中引入头文件
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))//qt当前版本>=qt5
#include <QtWidgets>
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))//qt当前版本>=qt6
#include <QtCore5Compat>
#endif

//.默认Qt6开启了高分屏支持,界面会变得很大,甚至字体发虚,很多人会不习惯,因为这种模式如果程序很多坐标计算没有采用devicePixelRatio进行运算的话,
//100%会出现奇奇怪怪的问题,因为坐标不准确了。要取消这种效果可以设置高分屏缩放因子。
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::Floor);
#endif

#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
    std::sort(ipv4s.begin(), ipv4s.end());
#endif

QT_BEGIN_NAMESPACE
namespace Ui { class MP3; }
QT_END_NAMESPACE

class MP3 : public QWidget
{
    Q_OBJECT

public:
    MP3(QWidget *parent = nullptr);
    ~MP3();
    void paintEvent(QPaintEvent *event);

private slots:
    void on_PB_close_clicked();

    void on_PB_Function_clicked();

private:
    Ui::MP3 *ui;


    //音乐下载播放
    void DownloadPlayer(QString id, QString hash);

    //访问http
    void DashJsonAnalysis(QByteArray json);

    //搜索音乐数据信息的json解析 解析出真正的音乐文件和歌曲
    QString MusicJsonAnalysis(QByteArray musicJson);

    //获取qt版本
    void GetQtVersion();

    //获取当前屏幕尺寸区域
    QRect getScreenRect(bool available);
    int getScreenIndex();



protected:
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void mousePressEvent(QMouseEvent *event);
private:
    //定义坐标
    QPoint m_mousePoint;
    QPoint movepress;
    bool  isMousePress;


};
#endif // MP3_H

           

mp3.cpp

只是实现了鼠标窗口移动事件

#include "mp3.h"
#include "ui_mp3.h"
#include <QPainter>

MP3::MP3(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MP3)
{
    ui->setupUi(this);

    //固定窗口大小 宽 高
    this->setFixedSize(this->geometry().size());

    //隐藏窗口标题
    /*生成一个无边界的窗口。用户不能通过窗口系统移动或调整无边界窗口的大小。
     * 在x11上,标志的结果取决于窗口管理器和它理解主题和/或网络提示的能力。
     * 大多数现有的现代窗口管理器都可以处理这个问题。*/
    this->setWindowFlag(Qt::FramelessWindowHint);
}

MP3::~MP3()
{
    delete ui;
}

//画主题背景
void MP3::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);//画家要操作的部件
    painter.drawPixmap(0,0,QPixmap(":/Images/2041960.jpg"));
}

//关闭当前窗口
void MP3::on_PB_close_clicked()
{
    //this->close();
   qApp->quit();
}

void MP3::DownloadPlayer(QString id, QString hash)
{

}

void MP3::DashJsonAnalysis(QByteArray json)
{

}

QString MP3::MusicJsonAnalysis(QByteArray musicJson)
{

}

void MP3::GetQtVersion()
{
    qDebug()<<"qt当前版本 = "<<QT_VERSION<<"qt 6检查版本 = "<<QT_VERSION_CHECK(6,0,0);
}

void MP3::mouseMoveEvent(QMouseEvent *event)
{
    if(isMousePress)
    {
        QPoint movePos = event->globalPos(); //当前鼠标移动坐标
        qDebug()<<" movePos = "<<event->globalPos();
        this->move(movePos - m_mousePoint);//鼠标当前移动坐标-原始坐标 = 第二个位置
    }
}

void MP3::mouseReleaseEvent(QMouseEvent *event)
{
    Q_UNUSED(event);     //不使用
    isMousePress = false;//鼠标松开
}

void MP3::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
        isMousePress = true;//鼠标按压

    //窗口移动距离
    m_mousePoint = event->globalPos() - this->pos(); //当前鼠标移动坐标-当前窗口坐标(默认固定不变)= 第一个位置
    qDebug()<<" globalPos = "<<event->globalPos();
    qDebug()<<" this->pos = "<<this->pos();               //窗口当前坐标

}
//===========================================================================
//功能页
void MP3::on_PB_Function_clicked()
{
    GetQtVersion();
    getScreenRect(true);
}

//获取当前屏幕索引
int MP3::getScreenIndex()
{
    //需要对多个屏幕进行处理
    int screenIndex = 0;
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
    int screenCount = qApp->screens().count();
    qDebug()<<"screenCount = "<<screenCount;
#else
    int screenCount = qApp->desktop()->screenCount();
#endif

    if (screenCount > 1) {
        //找到当前鼠标所在屏幕
        QPoint pos = QCursor::pos();
        qDebug()<<"pos = "<<pos;
        for (int i = 0; i < screenCount; ++i) {
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
            if (qApp->screens().at(i)->geometry().contains(pos)) {
#else
            if (qApp->desktop()->screenGeometry(i).contains(pos)) {
#endif
                screenIndex = i;
                break;
            }
        }
    }
    qDebug()<<"screenIndex = "<<screenIndex;
    return screenIndex;
}

//获取当前屏幕尺寸区域
QRect MP3::getScreenRect(bool available)
{
    QRect rect;
    int screenIndex = getScreenIndex();
    if (available) {
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
        rect = qApp->screens().at(screenIndex)->availableGeometry();
#else
        rect = qApp->desktop()->availableGeometry(screenIndex);
#endif
    } else {
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
        rect = qApp->screens().at(screenIndex)->geometry();
#else
        rect = qApp->desktop()->screenGeometry(screenIndex);
#endif
    }
    qDebug()<<"rect = "<<rect;
    return rect;
}

           

隐藏窗口标题 最大化 最小化 关闭

生成一个无边界的窗口。用户不能通过窗口系统移动或调整无边界窗口的大小。

在x11上,标志的结果取决于窗口管理器和它理解主题和/或网络提示的能力。

大多数现有的现代窗口管理器都可以处理这个问题。