├── README.md ├── img ├── ex.png ├── lrcwin.gif └── search.png └── src ├── MusicPlayers.pro ├── MusicPlayers.pro.user ├── coin ├── add.png ├── add_c.png ├── begin.png ├── cls.png ├── cls_1.png ├── delete.png ├── delete_c.png ├── like.png ├── like_c.png ├── list.png ├── list_c.png ├── local_add.png ├── local_add_c.png ├── looper.png ├── lrc_word.png ├── max.png ├── max_1.png ├── min.png ├── min_1.png ├── mymusic.png ├── next.png ├── next_c.png ├── no_looper.png ├── pause.png ├── pause_c.png ├── prev.png ├── prev_c.png ├── search.png ├── sign_in.png ├── sign_in_c.png ├── songer.png ├── start.png ├── start_c.png ├── unvol.png ├── unvol_c.png ├── vol.png └── vol_c.png ├── create_list.cpp ├── create_list.h ├── lrc_win_main.cpp ├── lrc_win_main.h ├── lrcwidget.cpp ├── lrcwidget.h ├── lrcwin.cpp ├── lrcwin.h ├── main.cpp ├── mainwindow.cpp ├── mainwindow.h ├── mainwindow.ui ├── mybtn.cpp ├── mybtn.h ├── mylab.cpp ├── mylab.h ├── mylistwidget.cpp ├── mylistwidget.h ├── mytablewidget.cpp ├── mytablewidget.h ├── mywidget.cpp ├── mywidget.h ├── res.qrc ├── show_list.cpp ├── show_list.h ├── sign_in_win.cpp ├── sign_in_win.h ├── sign_up_win.cpp ├── sign_up_win.h ├── startbtn.cpp └── startbtn.h /README.md: -------------------------------------------------------------------------------- 1 | 2024-5-02 2 | 需要的同学可以去[KTVPlayer分支](https://github.com/Sugar0612/music-player/tree/KTVPlayer)使用音乐播放器,目前main分支不再维护了。 3 | ______________________________________________________________________________________________________________________________ 4 | # 糖糖音乐 5 | # ![music](img/ex.png) 6 | # 搜索界面 7 | # ![searchwin](img/search.png) 8 | # 歌词窗口 9 | # ![lrcwin](img/lrcwin.gif) 10 | # 使用说明: 11 | 1. 使用本播放器需要首先注册 和 登录(在此账号创建的歌单 和 获取的音乐将永久保存) 12 | 2. 请不要频繁操作 耐心等待3 ~ 5 秒 (害怕程序崩溃) 13 | 3. 当获取网络音乐时 请点击播放按钮 开始播放 14 | # 功能介绍: 15 | 1.支持网络搜索功能 16 | 2.支持歌单功能 以及增添"我喜爱的音乐" 17 | 3.支持循环播放 18 | 4.支持所有界面的增删改操作 19 | 5.支持歌词界面 以及歌词悬浮界面 20 | 对源代码感兴趣的朋友 请访问我的github: 如果有什么问题欢迎和我私信 21 | # 关于歌曲搜索功能 参考 22 | [https://zxfdog.blog.csdn.net/article/details/107992205](https://zxfdog.blog.csdn.net/article/details/107992205) 23 | 24 | 25 | 本作品只是为了让大家更好的了解qt,熟练其中的机制,不可以用来倒卖和商用,若出现后果与本人无关。 26 | 感谢大家支持和使用 27 | -------------------------------------------------------------------------------- /img/ex.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/img/ex.png -------------------------------------------------------------------------------- /img/lrcwin.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/img/lrcwin.gif -------------------------------------------------------------------------------- /img/search.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/img/search.png -------------------------------------------------------------------------------- /src/MusicPlayers.pro: -------------------------------------------------------------------------------- 1 | QT += core gui sql multimedia network 2 | QT += sql 3 | QT += network 4 | 5 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 6 | 7 | CONFIG += c++11 8 | 9 | # The following define makes your compiler emit warnings if you use 10 | # any Qt feature that has been marked deprecated (the exact warnings 11 | # depend on your compiler). Please consult the documentation of the 12 | # deprecated API in order to know how to port your code away from it. 13 | DEFINES += QT_DEPRECATED_WARNINGS 14 | 15 | # You can also make your code fail to compile if it uses deprecated APIs. 16 | # In order to do so, uncomment the following line. 17 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 18 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 19 | 20 | SOURCES += \ 21 | main.cpp \ 22 | mainwindow.cpp\ 23 | mybtn.cpp\ 24 | mywidget.cpp \ 25 | startbtn.cpp\ 26 | mylab.cpp\ 27 | mylistwidget.cpp\ 28 | sign_in_win.cpp\ 29 | sign_up_win.cpp\ 30 | lrcwidget.cpp\ 31 | mytablewidget.cpp\ 32 | create_list.cpp\ 33 | show_list.cpp\ 34 | lrc_win_main.cpp 35 | 36 | HEADERS += \ 37 | mainwindow.h\ 38 | mybtn.h\ 39 | mywidget.h \ 40 | startbtn.h\ 41 | mylab.h\ 42 | mylistwidget.h\ 43 | sign_in_win.h\ 44 | sign_up_win.h\ 45 | lrcwidget.h\ 46 | mytablewidget.h\ 47 | create_list.h\ 48 | show_list.h\ 49 | lrc_win_main.h 50 | 51 | FORMS += \ 52 | mainwindow.ui 53 | 54 | # Default rules for deployment. 55 | qnx: target.path = /tmp/$${TARGET}/bin 56 | else: unix:!android: target.path = /opt/$${TARGET}/bin 57 | !isEmpty(target.path): INSTALLS += target 58 | 59 | RESOURCES += \ 60 | res.qrc 61 | 62 | CONFIG += resources_big 63 | -------------------------------------------------------------------------------- /src/MusicPlayers.pro.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | EnvironmentId 7 | {9ab11b68-7bfb-4f68-ab2b-cdc65cfdaf63} 8 | 9 | 10 | ProjectExplorer.Project.ActiveTarget 11 | 0 12 | 13 | 14 | ProjectExplorer.Project.EditorSettings 15 | 16 | true 17 | false 18 | true 19 | 20 | Cpp 21 | 22 | CppGlobal 23 | 24 | 25 | 26 | QmlJS 27 | 28 | QmlJSGlobal 29 | 30 | 31 | 2 32 | UTF-8 33 | false 34 | 4 35 | false 36 | 80 37 | true 38 | true 39 | 1 40 | true 41 | false 42 | 0 43 | true 44 | true 45 | 0 46 | 8 47 | true 48 | 1 49 | true 50 | true 51 | true 52 | false 53 | 54 | 55 | 56 | ProjectExplorer.Project.PluginSettings 57 | 58 | 59 | -fno-delayed-template-parsing 60 | 61 | false 62 | {d13385ab-4fa5-4688-959e-c61d52a9dab5} 63 | 64 | 65 | 66 | ProjectExplorer.Project.Target.0 67 | 68 | Desktop Qt 5.14.2 MinGW 64-bit 69 | Desktop Qt 5.14.2 MinGW 64-bit 70 | qt.qt5.5142.win64_mingw73_kit 71 | 1 72 | 0 73 | 0 74 | 75 | E:/QT/file/build-MusicPlayers-Desktop_Qt_5_14_2_MinGW_64_bit-Debug 76 | 77 | 78 | true 79 | QtProjectManager.QMakeBuildStep 80 | true 81 | 82 | false 83 | false 84 | false 85 | 86 | 87 | true 88 | Qt4ProjectManager.MakeStep 89 | 90 | false 91 | 92 | 93 | false 94 | 95 | 2 96 | Build 97 | Build 98 | ProjectExplorer.BuildSteps.Build 99 | 100 | 101 | 102 | true 103 | Qt4ProjectManager.MakeStep 104 | 105 | true 106 | clean 107 | 108 | false 109 | 110 | 1 111 | Clean 112 | Clean 113 | ProjectExplorer.BuildSteps.Clean 114 | 115 | 2 116 | false 117 | 118 | Debug 119 | Qt4ProjectManager.Qt4BuildConfiguration 120 | 2 121 | 122 | 123 | E:/QT/file/build-MusicPlayers-Desktop_Qt_5_14_2_MinGW_64_bit-Release 124 | 125 | 126 | true 127 | QtProjectManager.QMakeBuildStep 128 | false 129 | 130 | false 131 | false 132 | true 133 | 134 | 135 | true 136 | Qt4ProjectManager.MakeStep 137 | 138 | false 139 | 140 | 141 | false 142 | 143 | 2 144 | Build 145 | Build 146 | ProjectExplorer.BuildSteps.Build 147 | 148 | 149 | 150 | true 151 | Qt4ProjectManager.MakeStep 152 | 153 | true 154 | clean 155 | 156 | false 157 | 158 | 1 159 | Clean 160 | Clean 161 | ProjectExplorer.BuildSteps.Clean 162 | 163 | 2 164 | false 165 | 166 | Release 167 | Qt4ProjectManager.Qt4BuildConfiguration 168 | 0 169 | 170 | 171 | E:/QT/file/build-MusicPlayers-Desktop_Qt_5_14_2_MinGW_64_bit-Profile 172 | 173 | 174 | true 175 | QtProjectManager.QMakeBuildStep 176 | true 177 | 178 | false 179 | true 180 | true 181 | 182 | 183 | true 184 | Qt4ProjectManager.MakeStep 185 | 186 | false 187 | 188 | 189 | false 190 | 191 | 2 192 | Build 193 | Build 194 | ProjectExplorer.BuildSteps.Build 195 | 196 | 197 | 198 | true 199 | Qt4ProjectManager.MakeStep 200 | 201 | true 202 | clean 203 | 204 | false 205 | 206 | 1 207 | Clean 208 | Clean 209 | ProjectExplorer.BuildSteps.Clean 210 | 211 | 2 212 | false 213 | 214 | Profile 215 | Qt4ProjectManager.Qt4BuildConfiguration 216 | 0 217 | 218 | 3 219 | 220 | 221 | 0 222 | Deploy 223 | Deploy 224 | ProjectExplorer.BuildSteps.Deploy 225 | 226 | 1 227 | ProjectExplorer.DefaultDeployConfiguration 228 | 229 | 1 230 | 231 | 232 | dwarf 233 | 234 | cpu-cycles 235 | 236 | 237 | 250 238 | 239 | -e 240 | cpu-cycles 241 | --call-graph 242 | dwarf,4096 243 | -F 244 | 250 245 | 246 | -F 247 | true 248 | 4096 249 | false 250 | false 251 | 1000 252 | 253 | true 254 | 255 | false 256 | false 257 | false 258 | false 259 | true 260 | 0.01 261 | 10 262 | true 263 | kcachegrind 264 | 1 265 | 25 266 | 267 | 1 268 | true 269 | false 270 | true 271 | valgrind 272 | 273 | 0 274 | 1 275 | 2 276 | 3 277 | 4 278 | 5 279 | 6 280 | 7 281 | 8 282 | 9 283 | 10 284 | 11 285 | 12 286 | 13 287 | 14 288 | 289 | 2 290 | 291 | MusicPlayers2 292 | Qt4ProjectManager.Qt4RunConfiguration:C:/Users/lenovo/Desktop/buff/music-player/MusicPlayers.pro 293 | C:/Users/lenovo/Desktop/buff/music-player/MusicPlayers.pro 294 | 295 | false 296 | 297 | false 298 | true 299 | true 300 | false 301 | false 302 | true 303 | 304 | E:/QT/file/build-MusicPlayers-Desktop_Qt_5_14_2_MinGW_64_bit-Release 305 | 306 | 1 307 | 308 | 309 | 310 | ProjectExplorer.Project.TargetCount 311 | 1 312 | 313 | 314 | ProjectExplorer.Project.Updater.FileVersion 315 | 22 316 | 317 | 318 | Version 319 | 22 320 | 321 | 322 | -------------------------------------------------------------------------------- /src/coin/add.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/add.png -------------------------------------------------------------------------------- /src/coin/add_c.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/add_c.png -------------------------------------------------------------------------------- /src/coin/begin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/begin.png -------------------------------------------------------------------------------- /src/coin/cls.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/cls.png -------------------------------------------------------------------------------- /src/coin/cls_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/cls_1.png -------------------------------------------------------------------------------- /src/coin/delete.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/delete.png -------------------------------------------------------------------------------- /src/coin/delete_c.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/delete_c.png -------------------------------------------------------------------------------- /src/coin/like.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/like.png -------------------------------------------------------------------------------- /src/coin/like_c.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/like_c.png -------------------------------------------------------------------------------- /src/coin/list.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/list.png -------------------------------------------------------------------------------- /src/coin/list_c.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/list_c.png -------------------------------------------------------------------------------- /src/coin/local_add.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/local_add.png -------------------------------------------------------------------------------- /src/coin/local_add_c.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/local_add_c.png -------------------------------------------------------------------------------- /src/coin/looper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/looper.png -------------------------------------------------------------------------------- /src/coin/lrc_word.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/lrc_word.png -------------------------------------------------------------------------------- /src/coin/max.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/max.png -------------------------------------------------------------------------------- /src/coin/max_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/max_1.png -------------------------------------------------------------------------------- /src/coin/min.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/min.png -------------------------------------------------------------------------------- /src/coin/min_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/min_1.png -------------------------------------------------------------------------------- /src/coin/mymusic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/mymusic.png -------------------------------------------------------------------------------- /src/coin/next.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/next.png -------------------------------------------------------------------------------- /src/coin/next_c.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/next_c.png -------------------------------------------------------------------------------- /src/coin/no_looper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/no_looper.png -------------------------------------------------------------------------------- /src/coin/pause.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/pause.png -------------------------------------------------------------------------------- /src/coin/pause_c.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/pause_c.png -------------------------------------------------------------------------------- /src/coin/prev.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/prev.png -------------------------------------------------------------------------------- /src/coin/prev_c.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/prev_c.png -------------------------------------------------------------------------------- /src/coin/search.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/search.png -------------------------------------------------------------------------------- /src/coin/sign_in.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/sign_in.png -------------------------------------------------------------------------------- /src/coin/sign_in_c.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/sign_in_c.png -------------------------------------------------------------------------------- /src/coin/songer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/songer.png -------------------------------------------------------------------------------- /src/coin/start.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/start.png -------------------------------------------------------------------------------- /src/coin/start_c.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/start_c.png -------------------------------------------------------------------------------- /src/coin/unvol.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/unvol.png -------------------------------------------------------------------------------- /src/coin/unvol_c.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/unvol_c.png -------------------------------------------------------------------------------- /src/coin/vol.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/vol.png -------------------------------------------------------------------------------- /src/coin/vol_c.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sugar0612/music-player/236f75011f90486e3a6c7b27471b7e2f57e1dbfe/src/coin/vol_c.png -------------------------------------------------------------------------------- /src/create_list.cpp: -------------------------------------------------------------------------------- 1 | #include "create_list.h" 2 | #include 3 | create_list::create_list(QWidget *parent) : QWidget(parent) 4 | { 5 | 6 | QFont font; 7 | font.setFamily("幼圆"); 8 | 9 | this ->resize(350, 200); 10 | this ->setWindowTitle("歌单创建窗口"); 11 | this ->setWindowIcon(QIcon(":/coin/mymusic.png")); // 主窗口的图标设置! 12 | 13 | name = new QLineEdit(this); 14 | name ->setGeometry(80, 40, 200, 40); 15 | 16 | list_name = new QLabel("歌单名", this); 17 | list_name->setFont(font); 18 | list_name ->setGeometry(10, 40, 60, 40); 19 | 20 | ok = new QPushButton(this); 21 | ok ->setText("确定"); 22 | ok ->setGeometry(80, 100, 60, 30); 23 | 24 | cancel = new QPushButton(this); 25 | cancel ->setText("取消"); 26 | cancel ->setGeometry(210, 100, 60, 30); 27 | 28 | connect(ok, &QPushButton::clicked, [=] () { 29 | this ->hide(); 30 | emit create_ok(name ->text()); 31 | }); 32 | 33 | connect(cancel, &QPushButton::clicked, [=] () { 34 | this ->hide(); 35 | }); 36 | } 37 | -------------------------------------------------------------------------------- /src/create_list.h: -------------------------------------------------------------------------------- 1 | #ifndef CREATE_LIST_H 2 | #define CREATE_LIST_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "mybtn.h" 9 | 10 | class create_list : public QWidget 11 | { 12 | Q_OBJECT 13 | public: 14 | void initmysql(); 15 | 16 | QPushButton *ok, *cancel; 17 | QLineEdit *name; 18 | QLabel *list_name; 19 | explicit create_list(QWidget *parent = nullptr); 20 | 21 | signals: 22 | void create_ok(QString); 23 | }; 24 | 25 | #endif // CREATE_LIST_H 26 | -------------------------------------------------------------------------------- /src/lrc_win_main.cpp: -------------------------------------------------------------------------------- 1 | #include "lrc_win_main.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | lrc_win_main::lrc_win_main(QWidget *parent) : QWidget(parent) 9 | { 10 | QFont font; 11 | font.setFamily("幼圆"); 12 | font.setPointSize(20); 13 | font.setWeight(25); 14 | setWindowFlag(Qt::FramelessWindowHint); // 删除以前的 最大化 最小化 关闭自己写 15 | 16 | this ->setWindowTitle("歌词窗口"); 17 | this ->setWindowIcon(QIcon(":/coin/mymusic.png")); // 主窗口的图标设置! 18 | this ->resize(500, 80); 19 | 20 | this->setWindowFlags(Qt::FramelessWindowHint); //使得窗体透明 21 | this->setAttribute(Qt::WA_TranslucentBackground, true); // 透明显示 22 | 23 | this->setWindowFlags(this->windowFlags() | Qt::WindowStaysOnTopHint); //窗口最前面 不会因为点击而隐藏 24 | 25 | l = new QLabel(this); 26 | l ->setGeometry(0, 0, this ->width(), this ->height()); 27 | l ->setFont(font); 28 | l ->setStyleSheet("QLabel{color: rgb(128, 0, 128);}"); 29 | l ->setText("糖糖音乐"); 30 | } 31 | 32 | void lrc_win_main::mouseMoveEvent(QMouseEvent *e) { 33 | // 移动窗口 34 | if(leftflag && e ->pos().y() >= 0 && e ->pos().y() <= this ->height()) { 35 | win_p = e ->globalPos(); 36 | this ->move(win_p - glbal_p); 37 | } 38 | } 39 | 40 | void lrc_win_main::mousePressEvent(QMouseEvent *e) { 41 | // 点击窗口 42 | if(e ->button() == Qt::LeftButton && e ->pos().y() >= 0 && e ->pos().y() <= this ->height()) { 43 | leftflag = true; 44 | glbal_p = e ->globalPos() - this ->pos(); 45 | } 46 | } 47 | 48 | //透明设置 可以点击窗口了 49 | void lrc_win_main::paintEvent(QPaintEvent * event) 50 | { 51 | QPainter painter(this); 52 | QPen pen; 53 | pen.setColor(QColor(0, 0, 0, 1)); 54 | painter.setPen(pen); 55 | painter.setBrush(QBrush(QColor(0, 0, 0, 1))); 56 | 57 | painter.drawRect(QRect(0, 0, this ->width(), this ->height())); 58 | 59 | return QWidget::paintEvent(event); 60 | } 61 | -------------------------------------------------------------------------------- /src/lrc_win_main.h: -------------------------------------------------------------------------------- 1 | #ifndef LRC_WIN_MAIN_H 2 | #define LRC_WIN_MAIN_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | class lrc_win_main : public QWidget 10 | { 11 | Q_OBJECT 12 | public: 13 | explicit lrc_win_main(QWidget *parent = nullptr); 14 | 15 | 16 | QLabel *l = nullptr; 17 | 18 | bool leftflag = false; 19 | QPoint glbal_p, win_p; // 用win_p - glabel_p 就是 偏移量 20 | void mouseMoveEvent(QMouseEvent *); 21 | void mousePressEvent(QMouseEvent *); 22 | void paintEvent(QPaintEvent * event); 23 | 24 | signals: 25 | 26 | }; 27 | 28 | #endif // LRC_WIN_MAIN_H 29 | -------------------------------------------------------------------------------- /src/lrcwidget.cpp: -------------------------------------------------------------------------------- 1 | #include "lrcwidget.h" 2 | #include 3 | 4 | lrcwidget::lrcwidget(QWidget *parent) : QWidget(parent) 5 | { 6 | l = new QLabel(this); 7 | l ->setGeometry(50, 50, 400, 400); 8 | l ->setStyleSheet("broder-radius: 30px;}"); 9 | } 10 | -------------------------------------------------------------------------------- /src/lrcwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYWIDGET_H 2 | #define MYWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | class lrcwidget : public QWidget 10 | { 11 | Q_OBJECT 12 | public: 13 | QLabel *l; // 用来绘制歌曲专辑图片 14 | explicit lrcwidget(QWidget *parent = nullptr); 15 | 16 | signals: 17 | 18 | }; 19 | 20 | #endif // MYWIDGET_H 21 | -------------------------------------------------------------------------------- /src/lrcwin.cpp: -------------------------------------------------------------------------------- 1 | #include "lrcwin.h" 2 | 3 | lrcwin_::lrcwin_(QWidget *parent) : QWidget(parent) 4 | { 5 | 6 | } 7 | -------------------------------------------------------------------------------- /src/lrcwin.h: -------------------------------------------------------------------------------- 1 | #ifndef LRCWIN_H 2 | #define LRCWIN_H 3 | 4 | #include 5 | 6 | class lrcwin : public QWidget 7 | { 8 | Q_OBJECT 9 | public: 10 | explicit lrcwin(QWidget *parent = nullptr); 11 | 12 | signals: 13 | 14 | }; 15 | 16 | #endif // LRCWIN_H 17 | -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | QApplication a(argc, argv); 8 | QApplication::addLibraryPath("./plugins"); 9 | MainWindow w; 10 | w.show(); 11 | return a.exec(); 12 | } 13 | -------------------------------------------------------------------------------- /src/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include "ui_mainwindow.h" 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | using namespace std; 24 | 25 | MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) 26 | { 27 | ui->setupUi(this); 28 | 29 | Player = new QMediaPlayer(); // 实体化播放列表 30 | Playlist = new QMediaPlaylist(); 31 | 32 | int x = 1025, y = 700; //记录初始的宽和高 33 | this ->resize(x, y); // 设置主界面的宽高 34 | setWindowFlag(Qt::FramelessWindowHint); // 删除以前的 最大化 最小化 关闭自己写 35 | this ->setMinimumSize(QSize(x, y)); // 窗口最小 size 36 | 37 | this ->X = 63; // 初始化 X 38 | this ->V = 50; // 初始化 V 39 | 40 | this ->setWindowTitle("糖糖音乐"); //主窗口的运行文件名称 41 | this ->setWindowIcon(QIcon(":/coin/mymusic.png")); // 主窗口的图标设置! 42 | 43 | // 固定大小 44 | this ->setFixedSize(x, y); 45 | 46 | time = new QTimer(this); // 初始化定时器 47 | 48 | //关于tablewidget 上面的名字 49 | list_col_table = QStringList() << " " << "歌曲" << "作者" << "专辑" << " " << " " << "时间"; 50 | song_col_name = QStringList() << " "<< "歌曲" << " " << " " << " "; 51 | 52 | initMysql(); 53 | // 以上关于mysql的初始化 54 | 55 | //关于字体 56 | QFont font, font_L; 57 | font.setFamily("幼圆"); 58 | font.setPointSize(10); 59 | font_L.setFamily("楷体"); 60 | font_L.setPointSize(15); 61 | 62 | //窗口上方按钮的封装 63 | btnL = new QLabel(this); 64 | btnL ->setGeometry(0, 0, 1025, 70); 65 | btnL ->setStyleSheet("QLabel{background-color: rgb(174, 205, 210); border: solid;}"); 66 | btnL ->setFont(font_L); 67 | btnL ->setText(" 糖糖音乐"); 68 | 69 | 70 | search_line = new QLineEdit(btnL); // 搜索框 71 | search_line ->setGeometry(180, 18, 180, 40); 72 | search_line ->setStyleSheet("QLineEdit{border-style:none; padding:6px; border-radius: 50px;}"); 73 | 74 | //登录按钮 75 | sign_in_btn = new mybtn(":/coin/sign_in.png", ":/coin/sign_in_c.png"); 76 | sign_in_btn ->setParent(btnL); 77 | sign_in_btn ->move(750, 20); 78 | 79 | //搜索按钮 80 | btn_search = new mybtn(":/coin/search.png", ":/coin/search.png"); 81 | btn_search ->setParent(btnL); 82 | btn_search ->move(360, 20); 83 | 84 | // 主窗口 按钮图标 85 | winicon = new mybtn(":/coin/mymusic.png", ":/coin/mymusic.png"); 86 | winicon ->setParent(this); 87 | winicon ->move(12, 12); 88 | 89 | // 播放按钮的创建 90 | playbt = new startbtn(":/coin/start.png", ":/coin/start_c.png", ":/coin/pause.png", ":/coin/pause_c.png"); 91 | playbt->setParent(this); 92 | playbt ->move(478, 620); 93 | 94 | // 下一首 功能按钮的创建 95 | nextbt = new mybtn(":/coin/next.png", ":/coin/next_c.png"); 96 | nextbt ->setParent(this); 97 | nextbt ->move(545, 623); 98 | 99 | // 上一首 功能的按钮的创建 100 | prevbt = new mybtn(":/coin/prev.png", ":/coin/prev_c.png"); 101 | prevbt ->setParent(this); 102 | prevbt ->move(415, 623); 103 | 104 | // 音量功能 的按钮创建 105 | volbt = new startbtn (":/coin/vol.png", ":/coin/vol_c.png", ":/coin/unvol.png", ":/coin/unvol_c.png"); 106 | volbt ->setParent(this); 107 | volbt ->move(640, 625); 108 | 109 | // 播放列表 按钮 110 | listbtn = new mybtn(":/coin/list.png", ":/coin/list_c.png"); 111 | listbtn ->setParent(this); 112 | listbtn ->move(940, 625); 113 | 114 | //本地音乐 添加按钮 115 | local_btn = new mybtn(":/coin/local_add.png", ":/coin/local_add_c.png"); 116 | local_btn ->setParent(this); //基于this建立 117 | local_btn ->move(550, 300); 118 | local_btn ->hide(); // 先隐藏起来 119 | 120 | //minb init 121 | minb =new mybtn(":/coin/min.png", ":/coin/min_1.png"); 122 | minb ->setParent(this); 123 | minb ->move(this ->width() - 98, 25); 124 | 125 | // maxb init 126 | maxb = new mybtn(":/coin/max.png", ":/coin/max_1.png"); 127 | maxb ->move(this ->width() - 68, 25); 128 | maxb ->setParent(this); 129 | 130 | // clsb init 131 | clsb = new mybtn(":/coin/cls.png", ":/coin/cls_1.png"); 132 | clsb ->setParent(this); 133 | clsb ->move(this ->width() - 38, 25); 134 | 135 | 136 | //将minb maxb clsb 加入到水平布局中. 137 | QHBoxLayout * hlay = new QHBoxLayout(); //设置minb maxb clsb 的水平布局让其对于窗口的相对位置不会改变 138 | hlay ->addWidget(minb); 139 | hlay ->addWidget(maxb); 140 | hlay ->addWidget(clsb); 141 | 142 | // "我的音乐" 标签 143 | mymusic = new mylab("我的音乐", this); 144 | mymusic ->setGeometry(0, 70, 160, 40); 145 | 146 | // 循环播放 按钮 147 | looper_m = new startbtn(":/coin/no_looper.png", ":/coin/no_looper.png", ":/coin/looper.png", ":/coin/looper.png"); 148 | looper_m ->setParent(this); 149 | looper_m ->move(340, 627); 150 | 151 | 152 | // lrc_btn init 153 | lrc_btn = new mybtn(":/coin/lrc_word.png", ":/coin/lrc_word.png"); 154 | lrc_btn ->setParent(this); 155 | lrc_btn ->move(760, 625); 156 | 157 | // 我的歌单标签 158 | mylist = new mylab("我的歌单", this); 159 | mylist ->setGeometry(0, 210, 160, 40); 160 | 161 | //add songlist init 162 | add_songlist = new mybtn(":/coin/add.png", ":/coin/add_c.png"); 163 | add_songlist ->setParent(mylist); 164 | add_songlist ->move(mylist ->width() - 60, mylist ->height() - 30); 165 | 166 | // 用来显示当前播放歌曲 167 | musicL = new QLabel(this); 168 | musicL ->setGeometry(95, 620, 150, 50); 169 | musicL ->setFont(font); 170 | 171 | //用来显示当前播放歌曲的图片 172 | music_map = new QPushButton(this); 173 | music_map ->setGeometry(20, 625, 60, 60); 174 | music_map ->setFlat(true); 175 | 176 | 177 | // 登录标签 178 | sign_L = new QLabel(btnL); 179 | sign_L ->setText("未登录"); 180 | sign_L ->setGeometry(790, 20, 100, 30); 181 | sign_L ->setFont(font); 182 | 183 | // 本地音乐的播放列表 184 | local_w = new QTableWidget(this); 185 | local_w ->setEditTriggers(QAbstractItemView::NoEditTriggers); // 不可编辑 186 | local_w ->setShowGrid(false); // 关闭网格 187 | local_w ->setFocusPolicy(Qt::NoFocus); //去掉虚线格 188 | local_w ->verticalHeader() ->setHidden(true); // 去掉表头行号 189 | local_w ->setColumnCount(5);//设置 列宽 和 列count 190 | 191 | 192 | local_w ->setColumnWidth(0, 40); 193 | local_w ->setColumnWidth(1, 150); 194 | local_w ->setColumnWidth(2, 40); 195 | local_w ->setColumnWidth(3, 40); 196 | local_w ->setColumnWidth(4, 575); 197 | local_w->hide(); //隐藏 198 | 199 | // 定义歌单的播放列表 200 | list_w = new QTableWidget(this); 201 | list_w ->setEditTriggers(QAbstractItemView::NoEditTriggers); // 不可编辑 202 | list_w ->setShowGrid(false); // 关闭网格 203 | list_w ->setFocusPolicy(Qt::NoFocus); //去掉虚线格 204 | list_w ->verticalHeader() ->setHidden(true); // 去掉表头行号 205 | list_w ->setColumnCount(6);//设置 列宽 和 列count 206 | 207 | list_w ->setColumnWidth(0, 40); 208 | list_w ->setColumnWidth(1, 150); 209 | list_w ->setColumnWidth(2, 40); 210 | list_w ->setColumnWidth(3, 40); 211 | list_w ->setColumnWidth(4, 40); 212 | list_w ->setColumnWidth(5, 550); 213 | list_w ->hide(); //隐藏 214 | 215 | //喜欢音乐 216 | like_w = new QTableWidget(this); 217 | like_w ->setEditTriggers(QAbstractItemView::NoEditTriggers); // 不可编辑 218 | like_w ->setShowGrid(false); // 关闭网格 219 | like_w ->setFocusPolicy(Qt::NoFocus); //去掉虚线格 220 | like_w ->verticalHeader() ->setHidden(true); // 去掉表头行号 221 | like_w ->setColumnCount(5);//设置 列宽 和 列count 222 | 223 | like_w ->setColumnWidth(0, 40); // 爱心 224 | like_w ->setColumnWidth(1, 150); // 歌名 225 | like_w ->setColumnWidth(2, 40); // 播放 226 | like_w ->setColumnWidth(3, 40); // 增添到其他歌单 227 | like_w ->setColumnWidth(4, 590); // 空白 228 | like_w ->hide(); //隐藏 229 | 230 | 231 | // 我的音乐 的列表 窗口 232 | musiclist = new mylistwidget(this); 233 | musiclist ->setGeometry(0, 110, 160, 100); 234 | musiclist ->setFocusPolicy(Qt::NoFocus); // 去掉虚线框 235 | 236 | // 歌单列表 237 | songlist = new mylistwidget(this); 238 | songlist ->setGeometry(0, 250, 160, 350); 239 | songlist ->setFocusPolicy(Qt::NoFocus); 240 | 241 | 242 | // 歌词主窗口 243 | lrc = new lrc_win_main(); 244 | lrc ->hide(); 245 | 246 | //本地音乐列表 247 | mylocalmusic = new QListWidgetItem(musiclist); 248 | mylocalmusic ->setText("本地音乐"); 249 | mylocalmusic ->setFont(font); 250 | 251 | // 歌曲的队列 252 | songqueue = new mytablewidget(this); 253 | songqueue ->setGeometry(1053, this ->height() - btnL ->height(), 250, 485 - btnL ->height()); 254 | songqueue ->horizontalHeader()->setHidden(true); // 去掉表头 255 | songqueue ->setHorizontalHeaderLabels(song_col_name); 256 | 257 | // 喜欢的音乐列表 258 | likemusiclist = new QListWidgetItem(musiclist); 259 | likemusiclist ->setText("喜爱的音乐"); 260 | likemusiclist ->setFont(font); 261 | 262 | // 显示搜索音乐的窗口 263 | tab_search = new QTableWidget(); 264 | tab_search ->setParent(this); 265 | tab_search ->setColumnCount(7); // 设置列数 266 | tab_search ->setHorizontalHeaderLabels(list_col_table);// 设置列数的 名字 267 | tab_search ->setGeometry(musiclist->width(), btnL ->height(), this ->width() - musiclist ->width(), this ->height() - btnL ->height() - 100); 268 | tab_search ->stackUnder(songlist); // 优先级在songlist 之下 269 | tab_search ->hide(); 270 | 271 | tab_search ->setEditTriggers(QAbstractItemView::NoEditTriggers); // 不可编辑 272 | tab_search ->setShowGrid(false); // 关闭网格 273 | tab_search ->setFocusPolicy(Qt::NoFocus); //去掉虚线格 274 | tab_search ->verticalHeader() ->setHidden(true); // 去掉表头行号 275 | tab_search ->setStyleSheet("QTableWidget::Item::selected{background: white;}" 276 | "QHeaderView::section{border: 0px solid white};" 277 | "QTableWidget::Item::hover{background: black;}"); 278 | 279 | 280 | // 歌词地基窗口 281 | lrc_w = new lrcwidget(); 282 | lrc_w ->setGeometry(-this ->width(), btnL ->height(), this ->width(), this ->height() - btnL ->height() - 100); 283 | 284 | 285 | // 歌曲显示窗口 286 | lrcwin = new QTabWidget(this); 287 | lrcwin ->setGeometry(-this ->width(), btnL ->height(), this ->width(), this ->height() - btnL ->height() - 100); 288 | lrcwin ->addTab(lrc_w,""); 289 | lrcwin ->findChildren().at(0)->hide(); // 隐藏widget 上面的tab 290 | lrcwin ->show(); 291 | 292 | 293 | //获得屏幕的分辨率 294 | QDesktopWidget* desk = QApplication::desktop(); 295 | desk ->screenGeometry(); 296 | 297 | // http init 298 | net_manager = new QNetworkAccessManager(); 299 | net_request = new QNetworkRequest(); 300 | network_manager2 = new QNetworkAccessManager(); 301 | network_request2 = new QNetworkRequest(); 302 | network_manager3 = new QNetworkAccessManager(); 303 | network_request3 = new QNetworkRequest(); 304 | 305 | //sign init 306 | sign = new sign_in_win(); 307 | 308 | connect(minb, &QPushButton::clicked, [=]() { 309 | showMinimized(); 310 | }); // 点击 minb 按钮 最小化 311 | 312 | // 关闭窗口按钮 313 | connect(clsb, &QPushButton::clicked, [=](){ 314 | this ->close(); 315 | lrc ->hide(); 316 | Player ->stop(); 317 | exit(0); 318 | }); 319 | 320 | 321 | // 当点击playbt 时 开始 播放 或者 暂停音乐 322 | connect(playbt, &QPushButton::clicked, [=] () { 323 | if (!Playlist ->isEmpty()) { 324 | if (emit_i == 0) { 325 | emit beginplay(); 326 | emit_i++; 327 | } 328 | int idx = Playlist ->currentIndex(); 329 | QString textL = nowlist[idx]; 330 | musicL ->setText(getMName(textL) + "
" + getPName(textL)); //用来显示当前播放歌曲 331 | this ->setWindowTitle(textL); // 用来 改变窗口的 名称 332 | if (playf == false && nowlist.size() != 0) { 333 | Player ->play(); 334 | this ->druntime = Player ->duration(); // 获得总时长 335 | time ->start(); 336 | playf = true; 337 | } else { 338 | Player ->pause(); 339 | time ->stop(); 340 | playf = false; 341 | } 342 | } else { 343 | QMessageBox::information(this, "warning", "请录入音乐", QMessageBox::Ok); // 提示用户应该录入 音乐 344 | } 345 | }); 346 | 347 | 348 | // 当点击nextbt 时 播放下一首 音乐 349 | connect(nextbt, &QPushButton::clicked, [=](){ 350 | if (!Playlist ->isEmpty()) { 351 | int idx = Playlist ->currentIndex(); 352 | if(idx + 1 < nowlist.size()) { 353 | Player ->pause(); 354 | Playlist ->next(); 355 | QTimer ::singleShot(200, this, [=] () { 356 | Player ->play(); // 播放 357 | }); 358 | initPlayer(); // 初始化播放 359 | update(); 360 | } 361 | else { 362 | Playlist ->setCurrentIndex(0); 363 | QTimer ::singleShot(200, this, [=] () { 364 | Player ->play(); // 播放 365 | }); 366 | update(); 367 | } 368 | } 369 | }); 370 | 371 | 372 | // 当点击prevbt 时 播放上一首 音乐 373 | connect(prevbt, &QPushButton::clicked, [=](){ 374 | if (!Playlist ->isEmpty()) { 375 | int idx = Playlist ->currentIndex(); 376 | if(idx - 1 >= 0) { 377 | Player ->pause(); 378 | Playlist ->previous(); // 上一首 379 | QTimer ::singleShot(200, this, [=] () { 380 | Player ->play(); // 播放 381 | }); 382 | initPlayer(); // init 播放btn 383 | update(); 384 | } 385 | else { 386 | Playlist ->setCurrentIndex(nowplaylist.size() - 1); 387 | QTimer ::singleShot(200, this, [=] () { 388 | Player ->play(); // 播放 389 | }); 390 | update(); 391 | } 392 | } 393 | }); 394 | 395 | 396 | // 当 点击 音量按钮时 volF = false 时 静音 volF = true 时 返回当时 的 音量 397 | connect(volbt, &QPushButton::clicked, [=] () { 398 | if (!volF) { 399 | volF = true; 400 | bufferVol = this -> V; // 用来 记录还没有静音时 V 的值 401 | this ->V = 0; 402 | Player ->setVolume(V); // 音量变为 0; 403 | update(); // 重绘 painter 404 | } else { 405 | volF = false; 406 | this ->V= bufferVol; // 将静音前 V 的值还给V 407 | Player ->setVolume(2 * V); 408 | update(); 409 | } 410 | }); 411 | 412 | 413 | //点击 listbtn 出现 播放列表 414 | connect(listbtn, &QPushButton::clicked, [=] () { 415 | songqueue ->raise(); 416 | 417 | if (!is_open) { // 打开 播放列表 418 | is_open = true; 419 | QPropertyAnimation *an = new QPropertyAnimation(songqueue, "geometry"); 420 | an ->setDuration(200); 421 | an ->setStartValue(QRect(1053, btnL ->height(), 250, this->height() - btnL ->height() - 100)); 422 | an ->setEndValue(QRect(780, btnL ->height(), 250, this->height() - btnL ->height() - 100)); 423 | an ->setEasingCurve(QEasingCurve::Linear); 424 | an ->start(); 425 | } else { // 关闭播放列表 426 | is_open = false; 427 | QPropertyAnimation* an = new QPropertyAnimation(songqueue, "geometry"); 428 | an ->setDuration(200); 429 | an ->setStartValue(QRect(780, btnL ->height(), 250, this->height() - btnL ->height() - 100)); 430 | an ->setEndValue(QRect(1053, btnL ->height(), 250, this->height() - btnL ->height() - 100)); 431 | an ->setEasingCurve(QEasingCurve::Linear); 432 | an ->start(); 433 | } 434 | }); 435 | 436 | 437 | // 点击 add_songlist 触发信号 438 | connect(add_songlist, &mybtn::clicked, [=] () { 439 | if (user_id == -1) { 440 | QMessageBox::information(this, "提示", "请先登录", QMessageBox::Ok); 441 | return; 442 | } 443 | 444 | // list_create name init 445 | n_list = new create_list(); 446 | n_list ->show(); 447 | 448 | // 当输入名字完成后, 开始构造新的歌单 449 | connect(n_list, &create_list::create_ok, this, &MainWindow::songlist_add); 450 | }); 451 | 452 | 453 | // 当点击 本地音乐item 时显示本地音乐 454 | connect(musiclist, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(showlocal(QListWidgetItem*))); 455 | 456 | //如果 为空则添加录入音乐按钮 457 | connect(this, &MainWindow::file_null, [=] () { 458 | local_btn ->show(); // 显示 添加按钮的图标 459 | }); 460 | 461 | //当点击btn_search 开始搜索 462 | connect(btn_search, &mybtn::clicked, this, [=] () { 463 | if(sign_in) { 464 | v_hash.clear(); 465 | v_id.clear(); 466 | index = 0; 467 | for(int i = 1; i <= 10; ++i) { 468 | search(search_line ->text(), i); 469 | } 470 | 471 | tab_search ->setColumnWidth(0, 5); 472 | tab_search ->setColumnWidth(1, (this ->width() - musiclist ->width()) / 5); 473 | tab_search ->setColumnWidth(2, (this ->width() - musiclist ->width()) / 5); 474 | tab_search ->setColumnWidth(3, (this ->width() - musiclist ->width()) / 5); 475 | tab_search ->setColumnWidth(4, 15); 476 | tab_search ->setColumnWidth(5, 15); 477 | tab_search ->setColumnWidth(6, (this ->width() - musiclist ->width()) / 5 + 55); 478 | 479 | tab_search ->show(); // 显示搜索的结果 480 | } else QMessageBox::information(this, "提示", "请先登录", QMessageBox::Ok); 481 | }); 482 | 483 | //登录按钮的响应 484 | connect(sign_in_btn, &mybtn::clicked, [=] () { 485 | sign ->show(); 486 | }); 487 | 488 | 489 | //添加 按钮的功能 490 | connect(local_btn, &mybtn::clicked, [=] () { 491 | this ->filem = QFileDialog::getExistingDirectory(this,"", "",QFileDialog::ShowDirsOnly); 492 | filemlist = getfileName(this ->filem); // 用来存储载入歌曲的 歌名 493 | if(this ->filem.size() == 0) return; 494 | 495 | // 如果 filem 有效 那么插入数据库 并且绘制item 496 | QSqlQuery sql; 497 | QString user_id_qstr = QString("%1").arg(user_id); 498 | sql.exec("insert into localmusic values(" + user_id_qstr + ",\"" + this ->filem + "\");"); 499 | local_w ->setGeometry(musiclist ->width(), btnL->height(), this->width() - musiclist ->width(), this ->height() - btnL->height() - 100); 500 | local_w ->setStyleSheet("QListWidget::Item{height: 60px;}"); 501 | local_w ->setHorizontalHeaderLabels(QStringList() << " " << "歌曲" << " " << " " << " "); 502 | localinit(local_w); // init QlistWidget 503 | local_w ->show(); 504 | tab_search ->hide(); 505 | }); 506 | 507 | // 如果登录成功 508 | connect(sign, &sign_in_win::sign_in_success, [=] () { 509 | // 登录成功~ 510 | sign_in = true; 511 | //先清空初始化整个播放器 512 | songqueue ->clear(); 513 | songqueue ->setRowCount(0); 514 | 515 | local_w ->clear(); 516 | local_w ->setRowCount(0); 517 | 518 | like_w ->clear(); 519 | like_w ->setRowCount(0); 520 | 521 | songlist ->clear(); 522 | 523 | nowplaylist.clear(); // 初始化nowplay 524 | nowlist.clear(); 525 | nowlist_im.clear(); 526 | nowlist_lrc.clear(); 527 | filem.clear(); // 本地文件目录清空 528 | local_w ->hide(); 529 | time ->stop(); // 计时器也停止 530 | Player ->stop(); // 音乐停止 531 | 532 | Playlist = new QMediaPlaylist(); 533 | Player = new QMediaPlayer(); 534 | 535 | // 导入id 开始导入对应的歌曲 和 歌单 536 | this ->user_id = sign ->user_id; 537 | this ->song_list_id = sign ->song_list_cnt; 538 | innowplay(); // 初始化 该用户的歌单 539 | initsonglist(); // 初始化 该用户的自定义歌单 540 | init_local(); // 初始化 本地音乐 541 | init_like(); // init 喜爱音乐 542 | sign_L ->setText(sign ->user_name ->text()); 543 | }); 544 | 545 | // 点击 music_map 打开 歌词窗口 546 | connect(music_map, &QPushButton::clicked, [=] () { 547 | if(!lrc_open) { 548 | lrc_open = true; 549 | QPropertyAnimation *an = new QPropertyAnimation(lrcwin, "geometry"); 550 | an ->setDuration(300); 551 | an ->setStartValue(QRect(-lrcwin ->width(), lrcwin ->y(), lrcwin->width(), lrcwin->height())); 552 | an ->setEndValue(QRect(0, lrcwin->y(), lrcwin->width(), lrcwin->height())); 553 | an ->setEasingCurve(QEasingCurve::Linear); 554 | an ->start(); 555 | } else { 556 | lrc_open = false; 557 | QPropertyAnimation *an = new QPropertyAnimation(lrcwin, "geometry"); 558 | an ->setDuration(300); 559 | an ->setStartValue(QRect(0, lrcwin->y(), lrcwin->width(), lrcwin->height())); 560 | an ->setEndValue(QRect(-lrcwin ->width(), lrcwin ->y(), lrcwin->width(), lrcwin->height())); 561 | an ->setEasingCurve(QEasingCurve::Linear); 562 | an ->start(); 563 | } 564 | }); 565 | 566 | 567 | // 点击 songlist item 显示 该自定义歌单的音乐 568 | connect(songlist, &QListWidget::itemClicked, this, &MainWindow::show_List_music); 569 | 570 | 571 | //songqueue 点击触发 572 | connect(songqueue, &QTableWidget::cellClicked, this, &MainWindow::songqueue_fun); 573 | 574 | //local 点击触发 575 | connect(local_w, &QTableWidget::cellClicked, this, &MainWindow::local_fun); 576 | 577 | //like 点击触发 578 | connect(like_w, &QTableWidget::cellClicked, this, &MainWindow::fun_like_w); 579 | 580 | // list_w 点击触发 581 | connect(list_w, &QTableWidget::cellClicked, this, &MainWindow::fun_list_w); 582 | 583 | //改变模式 584 | connect(looper_m, &startbtn::clicked, [=] () { 585 | is_looper = 1 ^ is_looper; // 变成循环 586 | }); 587 | 588 | //lrc_btn 点击显示歌词床口 589 | connect(lrc_btn, &mybtn::clicked, [=] () { 590 | if (is_show) { 591 | is_show = false; 592 | lrc ->hide(); 593 | } else { 594 | is_show = true; 595 | lrc ->show(); 596 | } 597 | }); 598 | 599 | // 关于http 网络歌曲的 json 解析 和相应播放 600 | connect(net_manager, &QNetworkAccessManager::finished, this, &MainWindow::reply); 601 | connect(network_manager2, &QNetworkAccessManager::finished, this, &MainWindow::reply2); 602 | connect(network_manager3, &QNetworkAccessManager::finished, this, &MainWindow::reply3); 603 | connect(tab_search, &QTableWidget::cellDoubleClicked, this, &MainWindow::play_net_Music); 604 | 605 | 606 | initPro(); // 开始初始化进度条 607 | } 608 | 609 | //################################################################################################################################### 610 | 611 | // 初始化进度条 612 | void MainWindow::initPro() { 613 | QPalette pal; // 调色板 614 | pal.setColor(QPalette::WindowText,QColor(0,0,0)); // 调颜色 615 | QFont font("Courier",10); // 设置字体 616 | 617 | liftLabel=new QLabel(this); 618 | rightLabel=new QLabel(this); 619 | // 创建 标签 620 | 621 | liftLabel->setAlignment(Qt::AlignCenter); 622 | rightLabel->setAlignment(Qt::AlignCenter); 623 | // text 居中 624 | 625 | liftLabel->setGeometry(160, 643,50,50); 626 | rightLabel->setGeometry(815,643, 50,50); 627 | // 设置位置 和 大小 628 | 629 | rightLabel->setFont(font); 630 | liftLabel->setFont(font); 631 | // 设置 字体 632 | 633 | liftLabel->setPalette(pal); 634 | rightLabel->setPalette(pal); 635 | liftLabel ->setText("00:00"); 636 | rightLabel ->setText("00:00"); 637 | // 设置 颜色 638 | 639 | 640 | // 定时器 641 | connect(this, &MainWindow::beginplay, [=] () { 642 | // 歌词动态播放 643 | if(!Playlist ->isEmpty()) { 644 | time -> setInterval(1000); // 1000 毫秒触发一次 645 | connect(time, &QTimer::timeout, this, [=]() { 646 | // 歌词动态滚动 647 | if(lrcMap.size() != 0) { 648 | for(QMap::iterator j = lrcMap.begin(); j != lrcMap.end(); ++j) { 649 | bool is_back = false; // is_back 为true时 退出 650 | if(j != lrcMap.end() && j.key() <= this ->positontime + 1000 && j.key() >= this ->positontime - 1000) { 651 | // qDebug() << j.key() << endl; 652 | QFont font("幼圆"), p_font("幼圆"); // font 目前播放的歌词 p_font之前播放的歌词 恢复原样 653 | // 放大正在播放的歌词 654 | font.setWeight(90); 655 | font.setPointSize(15); 656 | 657 | //恢复已经放大过的歌词 658 | p_font.setWeight(50); 659 | p_font.setPointSize(13); 660 | 661 | int idx = lrc_idx[j.key()]; // 找到item 662 | int p_idx = p_lrcit == lrcMap.begin() - 1 ? -1 : lrc_idx[p_lrcit.key()]; // 找到上一个item 663 | lrc ->l ->setText(lrc_itm[idx] ->text()); // 将内嵌歌词窗口的 歌词 给主窗口歌词 664 | //恢复之前歌词字体大小 665 | if(p_idx >= 0) { 666 | lrc_itm[p_idx] ->setFont(p_font); 667 | lrc_itm[p_idx] ->setText(p_lrcit.value()); 668 | } 669 | //新的 p_lrcit 670 | p_lrcit = j; 671 | 672 | //滚动播放歌词 673 | if(idx >= 4) lrc_l ->verticalScrollBar() ->setValue(idx - 4); 674 | lrc_itm[idx] ->setFont(font); 675 | lrc_itm[idx] ->setText(j.value()); 676 | is_back = true; 677 | } 678 | if(is_back) break; 679 | } 680 | } 681 | //进度条更新 682 | updatepos(); 683 | // 当Player 播放完最后一首时 让palylist 的index 变成0, Qt的QPlayMedia 真是博大精深 684 | if (Playlist ->currentIndex() == Playlist ->mediaCount() - 1 && Player ->duration() / 1000 == this ->positontime / 1000) { 685 | Playlist ->setCurrentIndex(0); 686 | } 687 | }); // 1秒 触发一次 updatepos(); 688 | } 689 | }); 690 | } 691 | 692 | 693 | void MainWindow::initMysql() { 694 | db = QSqlDatabase::addDatabase("QMYSQL"); // 加载mysql 驱动 695 | db.setHostName("rm-uf6to17061qpnj06goo.mysql.rds.aliyuncs.com"); // 主机名 696 | db.setPort(3306); // 端口 697 | db.setDatabaseName("musicbase"); // 库名 698 | db.setUserName("user"); //用户名 699 | db.setPassword("Tsy20010612"); // 密码 700 | db.open(); //打开数据库 701 | 702 | } 703 | 704 | // 用来 单独封装控制 播放 705 | void MainWindow::initPlayer() { 706 | int _idx = Playlist ->isEmpty() ? 0 : Playlist ->currentIndex(); //获取 播放位置 707 | QString textL = nowlist[_idx]; 708 | musicL ->setText(getMName(textL) + "
" + getPName(textL)); //用来显示当前播放歌曲 709 | this ->setWindowTitle(textL); // 用来 改变窗口的 名称 710 | 711 | if (playf == false && filemlist.size() != 0) { 712 | playbt ->is_play = true; 713 | playbt ->setIcon(QIcon(":/coin/pause.png")); // 改变图标 714 | this ->druntime = Player ->duration(); // 获得总时长 715 | playf = true; 716 | } 717 | } 718 | 719 | //让QstringList 获得所有选中的歌曲文件路径 720 | QStringList MainWindow::getfileName(const QString& file) { 721 | QDir dir(file); 722 | QStringList filelist; 723 | filelist << "*.mp3"; 724 | QStringList res = dir.entryList(filelist, QDir::Files | QDir::Readable, QDir::Name); 725 | return res; 726 | } 727 | 728 | 729 | void MainWindow::showPlayMedia() { 730 | reinit(1); //初始化 731 | //判断是不是net_music 732 | int net_idx = Playlist->currentIndex() == -1 && Playlist ->mediaCount() == 1 ? 0 : Playlist->currentIndex(); 733 | if(Playlist ->currentIndex() == -1 && Playlist ->mediaCount() == 1) Playlist ->setCurrentIndex(0); // 如果index 变成-1了 那么直接setCurrentIndex 为0 734 | if (net_idx == -1 && Playlist ->mediaCount() == 0) return; // 防止索引越界 735 | 736 | bool net = is_net_music(nowplaylist[net_idx]); 737 | int idx = Playlist ->currentIndex(); 738 | QString textL = nowlist[idx]; 739 | //不同类型的 歌曲显示的名字不同 740 | if(net) { 741 | network_request3->setUrl(QUrl(nowlist_im[idx])); 742 | network_manager3->get(*network_request3); // 反馈信号 准备解析json 743 | buildlrc(nowlist_lrc[idx]); 744 | } else { 745 | // 载入本地音乐的图片 746 | local_img(":/coin/songer.png"); 747 | buildlrc(nowlist_lrc[idx]); 748 | } 749 | 750 | musicL ->setText(getMName(textL) + "
" + getPName(textL)); 751 | this ->setWindowTitle(textL); // 用来 改变窗口的 名称 752 | } // 改变label 显示新的 歌曲名字 753 | 754 | 755 | 756 | 757 | void MainWindow::paintEvent(QPaintEvent *event) { 758 | //绘制分割线 759 | QPainter paint(this); 760 | paint.setPen(QColor(Qt::black)); 761 | paint.drawLine(musiclist ->width(), btnL ->height(), musiclist ->width(), this ->height() - btnL->height() - 30); 762 | paint.drawLine(0, this ->height() - btnL->height() - 30, this ->width(), this ->height() - btnL->height() - 30); 763 | 764 | 765 | Q_UNUSED(event); // 避免编译器警告 766 | QPainter painter(this); 767 | painter.setRenderHint(QPainter::SmoothPixmapTransform); // 使用平滑的pixmap变换算法 768 | painter.setRenderHint(QPainter::Antialiasing, true); // 反走样 769 | 770 | QColor baseColor(0,20,20); // 进度条基底颜色 771 | QColor inColor(255,0,0); // 显示播放了多少的红色进度条 772 | QColor outColor(174, 205, 210); // 小球 773 | 774 | // qDebug() << "X is : " << this ->X << endl; 775 | // 进度条的基底 776 | painter.save(); 777 | painter.setPen(Qt::NoPen); 778 | painter.setBrush(baseColor); 779 | QRectF rect=QRectF(220,665,580,6); 780 | painter.drawRoundedRect(rect,3,3); 781 | painter.restore(); 782 | 783 | // 在基底上 加入红色进度条 784 | painter.save(); 785 | painter.setPen(Qt::NoPen); 786 | painter.setBrush(inColor); 787 | QRectF playrect = QRectF(220,665, X - 63, 6); 788 | painter.drawRoundedRect(playrect,0,3); 789 | painter.restore(); 790 | 791 | // 加入进度条上的 圆形 792 | painter.save(); 793 | painter.setPen(Qt::NoPen); 794 | painter.setBrush(outColor); 795 | painter.drawEllipse(X + 150, 658,20,20); 796 | painter.restore(); 797 | 798 | // 在圆形上加入红点 799 | painter.save(); 800 | painter.setPen(Qt::NoPen); 801 | painter.setBrush(inColor); 802 | painter.drawEllipse(X + 157, 665, 6, 6); 803 | painter.restore(); 804 | 805 | 806 | // 构造 声音进度条 807 | QPainter volumeP(this); 808 | volumeP.setRenderHint(QPainter::SmoothPixmapTransform); // 使用平滑的pixmap变换算法 809 | volumeP.setRenderHint(QPainter::Antialiasing, true); // 反走样 810 | 811 | // 同绘制 进度条一样 812 | volumeP.save(); 813 | volumeP.setPen(Qt::NoPen); 814 | volumeP.setBrush(baseColor); 815 | QRectF baserect = QRectF(670, 635, 50, 4); 816 | volumeP.drawRoundedRect(baserect, 3, 3); 817 | volumeP.restore(); 818 | 819 | volumeP.save(); 820 | volumeP.setPen(Qt::NoPen); 821 | volumeP.setBrush(inColor); 822 | QRectF inrect = QRectF(670, 635, V, 4); 823 | volumeP.drawRoundedRect(inrect, 0, 3); 824 | volumeP.restore(); 825 | 826 | volumeP.save(); 827 | volumeP.setPen(Qt::NoPen); 828 | volumeP.setBrush(inColor); 829 | volumeP.drawEllipse(V + 670, 633, 8, 8); 830 | volumeP.restore(); 831 | } 832 | 833 | void MainWindow::updatepos() { 834 | if (is_change == 1) time ->stop(); 835 | 836 | 837 | // druntime: 总进度 positontime: 当前进度 838 | this ->positontime += 1000; // 每过一秒 加一秒 839 | float a; 840 | a = (float)positontime / (float)Player ->duration(); 841 | X = a * 580 + 58; 842 | if (X > 635) X = 635; 843 | 844 | qint64 time = Player ->duration(); // 毫秒单位 845 | double secondDouble = time / 1000; // 转化成秒 846 | int minint = secondDouble / 60; // 总分钟 847 | int secondint = secondDouble - minint * 60; // 总秒 848 | 849 | double secondtime1 = positontime / 1000; // 当前播放的总秒数 850 | int minint1 = secondtime1 / 60; // 当前播放的分钟 851 | int secondint1 = secondtime1 - minint1 * 60; // 当前播放的秒 852 | 853 | int curra = minint1 / 10; 854 | int currb = minint1 % 10; 855 | int currc = secondint1 / 10; 856 | int currd = secondint1 % 10; 857 | 858 | int alla = minint / 10; 859 | int allb = minint % 10; 860 | int allc = secondint / 10; 861 | int alld = secondint % 10; 862 | if (is_looper == 1 && curra == alla && currb == allb && currc == allc && currd == alld) { 863 | Player ->stop(); 864 | int idx = Playlist ->currentIndex(); 865 | Playlist ->setCurrentIndex(idx); 866 | Player ->play(); 867 | this ->positontime = 0; 868 | } 869 | current = QString("%0%1:%2%3").arg(curra) .arg(currb).arg(currc). arg(currd); 870 | all = QString("%0%1:%2%3").arg(alla) .arg(allb) .arg(allc) .arg(alld); 871 | rightLabel->setText(all); 872 | liftLabel->setText(current); 873 | update(); 874 | if (is_change == 1) { 875 | this ->time ->start(); 876 | is_change = 0; 877 | } 878 | } 879 | 880 | 881 | // 初始化X 882 | void MainWindow::reinit(int status) { 883 | time ->stop(); 884 | this ->druntime = Player ->duration(); // 获得总时长 885 | this ->positontime = 0; // 初始化 886 | this -> X = 63; 887 | if(status == 1) time ->start(); 888 | } 889 | 890 | // 初始化 tablewidgetitem 891 | void MainWindow::boxitem(int i, QString text, QTableWidget* lw) { 892 | if(text == "") { 893 | qDebug() << "null filename!" << endl; 894 | return; 895 | } 896 | 897 | QFont font; 898 | font.setFamily("幼圆"); 899 | font.setPointSize(10); 900 | 901 | // 新的一首歌 902 | QString song_like = is_like(nowlist[i]); 903 | lw ->insertRow(i); 904 | lw ->setRowHeight(i, 45); 905 | 906 | 907 | for(int j = 0; j <= 4; ++j) lw ->setItem(i, j, new QTableWidgetItem()); 908 | 909 | if(song_like == "0") lw ->item(i, 0) ->setIcon(QIcon(":/coin/like_c.png")); 910 | else lw ->item(i, 0) ->setIcon(QIcon(":/coin/like.png")); 911 | lw ->item(i, 1) ->setText(getPName(text) + "\n" + getMName(text)); 912 | lw ->item(i, 2) ->setIcon(QIcon(":/coin/begin.png")); 913 | lw ->item(i, 3) ->setIcon(QIcon(":/coin/delete.png")); 914 | lw ->item(i, 4) ->setIcon(QIcon(":/coin/add.png")); 915 | } 916 | 917 | 918 | // 加入到歌曲队列 919 | void MainWindow::queuefun(mytablewidget* lw, QString file, QString m_name) { 920 | bool is_http = is_net_music(file); 921 | int idx = Playlist ->isEmpty() ? -1 : Playlist ->currentIndex(); 922 | if(file != "") { 923 | if(!is_http) Playlist ->insertMedia(idx + 1, QUrl::fromLocalFile(file));// 加入到多媒体 插入到下一首歌后面 924 | else Playlist ->insertMedia(idx + 1, QUrl(file)); 925 | Player ->setPlaylist(Playlist); 926 | Playlist ->setCurrentIndex(idx + 1); // 重新设置 多媒体位置 927 | time ->setInterval(1000); 928 | if(!is_http) { 929 | Player ->play(); 930 | reinit(1); // 初始化 931 | } else { 932 | reinit(0); 933 | } 934 | } 935 | boxitem(idx + 1, m_name, lw); // 加入队列 增加 item 936 | 937 | } 938 | 939 | // 歌手名字 940 | QString MainWindow::getPName(QString file) { 941 | bool is_ok = false; 942 | QString res; 943 | for(int i = 0; i < file.size(); ++i) { 944 | if(is_ok && file[i] != ' ') res += file[i]; 945 | if(file[i] == '-') is_ok = true; 946 | if(i + 1 < file.size() && file[i + 1] == '.') break; 947 | } 948 | return res; 949 | } 950 | 951 | // 音乐名字 952 | QString MainWindow::getMName(QString file) { 953 | QString res; 954 | for(int i = 0; i < file.size(); ++i) { 955 | res += file[i]; 956 | if(i + 1 < file.size() && file[i + 1] == '-') break; 957 | } 958 | return res; 959 | } 960 | 961 | void MainWindow::showlocal(QListWidgetItem* i) { 962 | if(user_id == -1) { 963 | QMessageBox::information(this, "提示", "请先登录", QMessageBox::Ok); // 提醒用户先登录 在使用 964 | return; 965 | } 966 | 967 | // qDebug() << "filem is : " << this ->filem << endl; 968 | int idx = musiclist ->row(i); //点击的是我的音乐的第几行item (共两行) 969 | if (idx == 0) { // idx == 0 说明点击的时 本地音乐 970 | if (this ->filem.size() != 0) local_w ->show(); 971 | like_w ->hide(); 972 | list_w ->hide(); 973 | tab_search ->hide(); 974 | } 975 | 976 | if (idx == 1) { // idx == 1 说明点击的时 喜爱音乐 977 | like_w ->show(); 978 | local_w ->hide(); 979 | list_w ->hide(); 980 | tab_search ->hide(); 981 | } 982 | } 983 | 984 | // init 本地音乐 当点击item时 播放 那个item 对应的歌曲 985 | void MainWindow::localinit(QTableWidget* lw) { 986 | for(int i = 0; i < filemlist.size(); ++i) { 987 | if(filemlist[i] == "") { 988 | qDebug() << "null filename!" << endl; 989 | return; 990 | } 991 | QString local_like = is_like(filemlist[i]); 992 | 993 | QFont font; 994 | font.setFamily("幼圆"); 995 | font.setPointSize(10); 996 | 997 | // 新的一首歌 998 | int row = lw ->rowCount(); 999 | lw ->setFont(font); 1000 | lw ->setRowCount(row + 1); 1001 | lw ->setRowHeight(row, 45); 1002 | 1003 | for(int j = 0; j < 5; ++j) lw ->setItem(row, j, new QTableWidgetItem()); 1004 | 1005 | if(local_like == "0") lw ->item(row, 0) ->setIcon(QIcon(":/coin/like_c.png")); 1006 | else lw ->item(row, 0) ->setIcon(QIcon(":/coin/like.png")); 1007 | 1008 | lw ->item(row, 1) ->setText(filemlist[i]); 1009 | lw ->item(row, 2) ->setIcon(QIcon(":/coin/begin.png")); // 播放 1010 | lw ->item(i, 3) ->setIcon(QIcon(":/coin/add.png")); 1011 | } // 录入本地音乐text 1012 | } 1013 | 1014 | // 从数据库读出当前播放音乐的路径 1015 | void MainWindow::innowplay() { 1016 | // 将mysql中的音乐路径打印出来 1017 | QSqlQuery selectq; 1018 | selectq.exec("select * from nowplay;"); 1019 | // 开始导入 音乐队列(路径 和 歌名) 1020 | while (selectq.next()) { 1021 | if (user_id == selectq.value(0).value()) { 1022 | this ->nowplaylist.push_back(selectq.value(1).value()); // 歌曲路径 1023 | this ->nowlist.push_back(selectq.value(2).value()); // 歌曲名字 1024 | this ->nowlist_im.push_back(selectq.value(3).value()); // 歌曲图片 1025 | this ->nowlist_lrc.push_back(selectq.value(4).value()); // 歌曲歌词 1026 | } 1027 | } 1028 | 1029 | //初始化播放列表 1030 | for(int i = 0; i < nowplaylist.size(); ++i) { 1031 | readmysql(songqueue, nowplaylist[i], nowlist[i]); // 开始将歌曲一个一个的加入播放队列 1032 | } 1033 | 1034 | // 初始化第一首歌 1035 | if (nowplaylist.size() > 0) { 1036 | bool is_net = is_net_music(nowplaylist[0]); 1037 | if(is_net) { 1038 | network_request3->setUrl(QUrl(nowlist_im[0])); //设置歌曲图片 1039 | network_manager3->get(*network_request3); // 反馈信号 准备解析json 1040 | } else { 1041 | local_img(nowlist_im[0]); 1042 | } 1043 | } 1044 | // 当音乐改变初始化 进度条 以及 显示音乐标签 1045 | connect(Player, &QMediaPlayer::currentMediaChanged, this, [=]() { 1046 | reinit(1); 1047 | MainWindow::showPlayMedia(); 1048 | }); 1049 | } 1050 | 1051 | 1052 | // songlist init 1053 | void MainWindow::initsonglist() { 1054 | QFont font_l; 1055 | font_l.setFamily("幼圆"); 1056 | font_l.setPointSize(11); 1057 | songlist->setFont(font_l); 1058 | 1059 | QString user_id_s = QString("%0").arg(user_id); 1060 | QSqlQuery songlist_sql; 1061 | songlist_sql.exec("select name from listname where id = " + user_id_s + ";"); 1062 | while(songlist_sql.next()) { 1063 | QString listname = songlist_sql.value(0).value(); 1064 | songlist ->addItem(new QListWidgetItem(listname)); 1065 | } 1066 | 1067 | connect(songlist, &mylistwidget::itemClicked, this, &MainWindow::show_list_music); // 点击 不同的歌单 显示不同的歌曲 1068 | } 1069 | 1070 | 1071 | // 显示 该自定义歌单的歌曲 1072 | void MainWindow::show_list_music(QListWidgetItem *item) { 1073 | int listsong_id; 1074 | QSqlQuery sql(db); 1075 | QString text = item ->text(); 1076 | 1077 | sql.exec("select * from listname where name = \"" + text + "\"and id = " + QString("%0").arg(user_id) + ";"); 1078 | while(sql.next()) { 1079 | listsong_id = sql.value(2).value(); 1080 | break; 1081 | } 1082 | 1083 | list_w->clear(); 1084 | list_w ->setRowCount(0); 1085 | list_w ->setGeometry(musiclist->width(), btnL->height(),this ->width() - musiclist->width(), this ->height() - btnL->height() - 100); 1086 | list_w ->setHorizontalHeaderLabels(QStringList() << " " << "歌曲" << " " << " " << " " << " "); 1087 | list_w ->setStyleSheet("QTableWidget::Item::selected{background: white;}" 1088 | "QHeaderView::section{border: 0px solid white};"); 1089 | sql.exec("select name from songlist where id = " + QString("%0").arg(user_id) + " and listid = " + QString("%0").arg(listsong_id) + ";"); 1090 | while(sql.next()) { 1091 | int row = list_w ->rowCount(); 1092 | list_w ->setRowCount(row + 1); 1093 | for(int i = 0; i < 5; ++i) list_w ->setItem(row, i, new QTableWidgetItem()); 1094 | 1095 | QString like = is_like(sql.value(0).value()); 1096 | if(like == "0") list_w ->item(row, 0) ->setIcon(QIcon(":/coin/like_c.png")); 1097 | else list_w ->item(row, 0) ->setIcon(QIcon(":/coin/like.png")); 1098 | 1099 | list_w ->item(row, 1) ->setText(sql.value(0).value()); 1100 | list_w ->item(row, 2) ->setIcon(QIcon(":/coin/begin.png")); 1101 | list_w ->item(row, 3) ->setIcon(QIcon(":/coin/delete.png")); 1102 | list_w ->item(row, 4) ->setIcon(QIcon(":/coin/add.png")); 1103 | } 1104 | } 1105 | 1106 | 1107 | // local init 1108 | void MainWindow::init_local() { 1109 | // 将mysql中的音乐路径打印出来 1110 | QSqlQuery selectq; 1111 | selectq.exec("select * from localmusic;"); 1112 | 1113 | // 开始 录入此用户id 对应的 音乐文件 1114 | while(selectq.next()) { 1115 | if(user_id == selectq.value(0).value()) { 1116 | this ->filem = selectq.value(1).value(); 1117 | // qDebug() << "its id filem is: " << this ->filem << endl; 1118 | break; 1119 | } 1120 | } 1121 | 1122 | if(this ->filem.size() == 0) { 1123 | emit file_null(); 1124 | local_w ->hide(); 1125 | return; 1126 | } 1127 | 1128 | local_w ->clear(); 1129 | local_w ->setRowCount(0); 1130 | 1131 | filemlist = getfileName(this ->filem); // 用来存储载入歌曲的 歌名 1132 | local_w ->setGeometry(musiclist->width(), btnL->height(),this ->width() - musiclist->width(), this ->height() - btnL->height() - 100); 1133 | local_w ->setHorizontalHeaderLabels(QStringList() << " " << "歌曲" << " " << " " << " "); 1134 | local_w ->setStyleSheet("QTableWidget::Item::selected{background: white;}" 1135 | "QHeaderView::section{border: 0px solid white};"); 1136 | 1137 | localinit(local_w); // init QlistWidget 1138 | } 1139 | 1140 | // like_w init 1141 | void MainWindow::init_like() { 1142 | like_w ->clear(); 1143 | like_w ->setRowCount(0); 1144 | 1145 | like_w ->setGeometry(musiclist->width(), btnL->height(),this ->width() - musiclist->width(), this ->height() - btnL->height() - 100); 1146 | like_w ->setHorizontalHeaderLabels(QStringList() << " " << "歌曲" << " " << " " << " "); 1147 | like_w ->setStyleSheet("QTableWidget::Item::selected{background: white;}" 1148 | "QHeaderView::section{border: 0px solid white};"); 1149 | 1150 | //init mymusic list 1151 | QFont font; 1152 | font.setFamily("幼圆"); 1153 | font.setWeight(10); 1154 | 1155 | QSqlQuery sql; 1156 | sql.exec("select * from liketb;"); 1157 | while(sql.next()) { 1158 | if(sql.value(0).value() == user_id) { 1159 | int row = like_w->rowCount(); 1160 | like_w ->setRowCount(row + 1); 1161 | for(int i = 0; i <= 3; ++i) like_w ->setItem(row, i, new QTableWidgetItem()); 1162 | 1163 | like_w ->item(row, 0) ->setIcon(QIcon(":/coin/like.png")); 1164 | like_w ->item(row, 1) ->setText(sql.value(2).value()); 1165 | like_w ->item(row, 2) ->setIcon(QIcon(":/coin/begin.png")); //播放 1166 | like_w ->item(row, 3) ->setIcon(QIcon(":/coin/add.png")); // 添加到歌单 1167 | } 1168 | } 1169 | } 1170 | 1171 | // 插入到数据库 和 音乐队列中 1172 | void MainWindow::insert_nowplay(QString name, QString m_name, QString p_name, QString lrc_name) { 1173 | update(); // 刷新 1174 | bool is_http = is_net_music(name); 1175 | QString user_id_qstr = QString("%0").arg(user_id); 1176 | // 如果播放列表已经存在这首歌 那么只需要完成相对应的插入操作 1177 | if (nowlist.count(m_name) != 0) { 1178 | int idx = std::find(nowlist.begin(), nowlist.end(), m_name) - nowlist.begin(); // 找到这首歌的索引 1179 | int c_idx = Playlist ->currentIndex() > idx ? Playlist ->currentIndex() - 1 : Playlist ->currentIndex(); // 当前播放的索引 1180 | 1181 | if(Playlist ->currentIndex() == idx) return; // 如果你想插入的歌就是 现在的歌 那么不需要插入 1182 | 1183 | // 重新插入歌曲 1184 | if(idx == 0 && c_idx == Playlist ->mediaCount() - 2) { 1185 | Playlist ->setCurrentIndex(Playlist ->currentIndex() - 1); 1186 | if(!is_http) { 1187 | Playlist ->addMedia(QUrl::fromLocalFile(nowplaylist[idx])); 1188 | // 载入本地音乐的图片 1189 | local_img(p_name); 1190 | } else { 1191 | Playlist ->addMedia(QUrl(nowplaylist[idx])); 1192 | network_request3->setUrl(QUrl(p_name)); 1193 | network_manager3->get(*network_request3); // 反馈信号 准备解析json 1194 | } 1195 | Playlist ->removeMedia(0); 1196 | QTimer::singleShot(200, [=] () { 1197 | Player ->play(); // 就是 qt player 这个机制过于博大精深 我吐了真的.......不得不这样写了 1198 | }); 1199 | } else { 1200 | Playlist ->removeMedia(idx); 1201 | if(!is_http) { 1202 | Playlist ->insertMedia(c_idx + 1, QUrl::fromLocalFile(nowplaylist[idx])); 1203 | // 载入本地音乐的图片 1204 | local_img(p_name); 1205 | } else { 1206 | Playlist ->insertMedia(c_idx + 1, QUrl(nowplaylist[idx])); 1207 | network_request3->setUrl(QUrl(p_name)); 1208 | network_manager3->get(*network_request3); // 反馈信号 准备解析json 1209 | } 1210 | Playlist ->setCurrentIndex(c_idx + 1); 1211 | QTimer::singleShot(200, [=] () { 1212 | Player ->play(); // 就是 qt player 这个机制过于博大精深 我吐了真的.......不得不这样写了 1213 | }); 1214 | } 1215 | 1216 | 1217 | // 重新插入 item 1218 | songqueue ->removeRow(idx); 1219 | buildlrc(lrc_name); 1220 | boxitem(c_idx + 1, nowlist[idx], songqueue); // 加入队列 增加 item 1221 | 1222 | //重新插入nowlist nowplaylist nowlist_im 1223 | QString nowlist_s = nowlist[idx]; 1224 | QString nowplaylist_s = nowplaylist[idx]; 1225 | QString nowlist_im_s = nowlist_im[idx]; 1226 | QString nowlist_lrc_s = nowlist_lrc[idx]; 1227 | nowlist.erase(nowlist.begin() + idx, nowlist.begin() + idx + 1); 1228 | nowplaylist.erase(nowplaylist.begin() + idx, nowplaylist.begin() + idx + 1); 1229 | nowlist_im.erase(nowlist_im.begin() + idx, nowlist_im.begin() + idx + 1); 1230 | nowlist_lrc.erase(nowlist_lrc.begin() + idx, nowlist_lrc.begin() + idx + 1); 1231 | 1232 | nowlist.insert(nowlist.begin() + c_idx + 1, nowlist_s); 1233 | nowplaylist.insert(nowplaylist.begin() + c_idx + 1, nowplaylist_s); 1234 | nowlist_im.insert(nowlist_im.begin() + c_idx + 1, nowlist_im_s); 1235 | nowlist_lrc.insert(nowlist_lrc.begin() + c_idx + 1, nowlist_lrc_s); 1236 | return; 1237 | } 1238 | 1239 | // 如果is_delete = false 那么调用readmysql 里面的deletenowplay 否则调用queuefun里面的deletenowplay 1240 | is_delete = true; 1241 | // 插入到数据库中 1242 | QSqlQuery sql; 1243 | QString sfile = QString("insert into nowplay values(" + user_id_qstr + ",\"" + name + "\"," + "\"" + m_name + "\"" + ",\"" + p_name + "\"" + ",\"" + lrc_name + "\")"); // 插入从本地音乐目录路径 1244 | sql.exec(sfile); // 执行 1245 | if(nowlist.count(m_name) == 0) { 1246 | if(!is_http) { 1247 | QPixmap pix; // 载入本地音乐的图片 1248 | local_img(p_name); 1249 | } else { 1250 | network_request3->setUrl(QUrl(p_name)); 1251 | network_manager3->get(*network_request3); // 反馈信号 准备解析json 1252 | } 1253 | int idx = Playlist ->currentIndex(); // 插入到此时播放音乐的后面 1254 | nowplaylist.insert(nowplaylist.begin() + idx + 1, name); 1255 | nowlist.insert(nowlist.begin() + idx + 1, m_name); 1256 | nowlist_im.insert(nowlist_im.begin() + idx + 1, p_name); 1257 | nowlist_lrc.insert(nowlist_lrc.begin() + idx + 1, lrc_name); 1258 | queuefun(songqueue, name, m_name); //插入新的歌曲 1259 | } 1260 | } 1261 | 1262 | 1263 | // 获取当前音乐文件的文件名 1264 | QString MainWindow::getname(QString file) { 1265 | QString ans; 1266 | for(int i = file.size() - 1; i >= 0; --i) { 1267 | if(file[i] != '\\' && file[i] != '/') ans.push_front(file[i]); 1268 | else break; 1269 | } 1270 | return ans; 1271 | } 1272 | 1273 | 1274 | // 初始化音乐队列 (仅在开启播放器的调用) 1275 | void MainWindow::readmysql(mytablewidget* lw, QString file, QString name) { 1276 | // qDebug() << "in readmysql!!" << endl; 1277 | int idx = Playlist ->isEmpty() ? 0 : Playlist ->mediaCount(); 1278 | is_net = is_net_music(file); // 判断是不是 网络音乐 1279 | if(file != "" && nowlist_lrc.size() != 0) { 1280 | if(!is_net) Playlist ->addMedia(QUrl::fromLocalFile(file));// 加入到多媒体 插入到下一首歌后面 1281 | else Playlist ->addMedia(QUrl(file)); 1282 | Playlist ->setCurrentIndex(0); // 重新设置 多媒体位置 1283 | Player ->setPlaylist(Playlist); 1284 | buildlrc(nowlist_lrc[0]); // 设置歌词 1285 | boxitem(idx, name, lw); // 加入队列 增加 item 1286 | reinit(0); // 初始化 1287 | } else return; 1288 | } 1289 | 1290 | // fun of songqueue 1291 | void MainWindow::songqueue_fun(int row, int col) { 1292 | g_file = nowplaylist[row]; 1293 | g_name = nowlist[row]; 1294 | g_image = nowlist_im[row]; 1295 | g_lrc = nowlist_lrc[row]; 1296 | 1297 | QSqlQuery like_db; 1298 | QString user_id_s = QString("%1").arg(user_id); // id 字符化 1299 | bool net = is_net_music(nowplaylist[row]); 1300 | 1301 | // 添加到 我的喜欢 1302 | if (col == 0) { 1303 | QString song_like = is_like(nowlist[row]); 1304 | if(song_like == "0") { // 是否和本地音乐一起消失 1305 | songqueue ->item(row, 0) ->setIcon(QIcon(":/coin/like.png")); 1306 | if (!net) { 1307 | int idx = std::find(filemlist.begin(), filemlist.end(), nowlist[row]) - filemlist.begin(); 1308 | local_w ->item(idx, 0) ->setIcon(QIcon(":/coin/like.png")); 1309 | } 1310 | like_db.exec("insert into liketb values(" + user_id_s + ",\"" + nowplaylist[row] + "\",\"" + nowlist[row] + "\",\"" + nowlist_im[row] + "\", \"" + nowlist_lrc[row] + "\");"); 1311 | fun_like(nowlist[row], 0); 1312 | //初始化 1313 | init_local(); 1314 | init_like(); 1315 | } else if (song_like == "1") { 1316 | songqueue ->item(row, 0) ->setIcon(QIcon(":/coin/like_c.png")); 1317 | if (!net) { 1318 | int idx = std::find(filemlist.begin(), filemlist.end(), nowlist[row]) - filemlist.begin(); 1319 | local_w ->item(idx, 0) ->setIcon(QIcon(":/coin/like_c.png")); 1320 | } 1321 | like_db.exec("delete from liketb where id = " + user_id_s + " and music = \"" + nowplaylist[row] + "\";"); 1322 | fun_like(nowlist[row], 1); 1323 | //初始化 1324 | init_local(); 1325 | init_like(); 1326 | } 1327 | return; 1328 | } 1329 | 1330 | // 关于 播放列表的播放操作 1331 | if (col == 1 || col == 2) { 1332 | Player ->pause(); 1333 | Playlist ->setCurrentIndex(row); 1334 | QTimer::singleShot(200, [=] () { // 缓冲一下 在播放 1335 | Player ->play(); 1336 | }); 1337 | return; 1338 | } 1339 | 1340 | 1341 | //删除操作 1342 | if (col == 3) { 1343 | QSqlQuery sql; 1344 | QString file = nowplaylist[row]; 1345 | QString user_id_s = QString("%1").arg(user_id); 1346 | this ->songqueue -> removeRow(row); 1347 | Playlist ->removeMedia(row); // 多媒体删除 音乐列表 歌曲 1348 | nowplaylist.erase(nowplaylist.begin() + row, nowplaylist.begin() + row + 1); 1349 | nowlist.erase(nowlist.begin() + row, nowlist.begin() + row + 1); 1350 | nowlist_im.erase(nowlist_im.begin() + row, nowlist_im.begin() + row + 1); 1351 | nowlist_lrc.erase(nowlist_lrc.begin() + row, nowlist_lrc.begin() + row + 1); 1352 | sql.exec("delete from nowplay where music = '" + file + "' and id = " + user_id_s + ";"); 1353 | return; 1354 | } 1355 | 1356 | 1357 | // 收藏操作 1358 | if (col == 4) { 1359 | QSqlQuery sql(db); 1360 | show_w = new show_list(user_id); 1361 | show_w ->show(); 1362 | 1363 | g_row = row; 1364 | connect(show_w, &show_list::return_item, this, &MainWindow::this_songlist); 1365 | return; 1366 | } 1367 | return; 1368 | } 1369 | 1370 | 1371 | //local的功能 1372 | void MainWindow::local_fun(int row, int col) { 1373 | g_file = this ->filem + filemlist[row]; 1374 | g_name = filemlist[row]; 1375 | g_image = ":/coin/songer.png"; 1376 | g_lrc = "本地音乐暂无歌词"; 1377 | 1378 | QSqlQuery like_db; 1379 | QString user_id_s = QString("%1").arg(user_id); // id 字符化 1380 | 1381 | // 当col == 0 时 关于喜爱音乐 1382 | if(col == 0) { 1383 | QString local_like = is_like(filemlist[row]); 1384 | bool isplay = false; // 是否在播放队列 1385 | if (nowplaylist.count(this ->filem + "/" + filemlist[row]) != 0) isplay = true; 1386 | if(local_like == "0") { 1387 | local_w ->item(row, 0) ->setIcon(QIcon(":/coin/like.png")); 1388 | if (isplay) { 1389 | int idx = std::find(nowplaylist.begin(), nowplaylist.end(), this ->filem + "/" + filemlist[row]) - nowplaylist.begin(); 1390 | songqueue ->item(idx, 0) ->setIcon(QIcon(":/coin/like.png")); 1391 | } 1392 | like_db.exec("insert into liketb values(" + user_id_s + ",\"" + this ->filem + "/" + filemlist[row] + "\",\"" + getname(filemlist[row]) + "\",\"" + ":/coin/songer.png" + "\", \"本地音乐暂无歌词\");"); 1393 | fun_like(filemlist[row], 0); 1394 | //初始化 1395 | init_local(); 1396 | init_like(); 1397 | } else{ 1398 | local_w ->item(row, 0) ->setIcon(QIcon(":/coin/like_c.png")); 1399 | if (isplay) { 1400 | int idx = std::find(nowplaylist.begin(), nowplaylist.end(), this ->filem + "/" + filemlist[row]) - nowplaylist.begin(); 1401 | songqueue ->item(idx, 0) ->setIcon(QIcon(":/coin/like_c.png")); 1402 | } 1403 | like_db.exec("delete from liketb where id = " + user_id_s + " and music = \"" + this ->filem + "/" + filemlist[row] + "\";"); 1404 | fun_like(filemlist[row], 1); 1405 | //初始化 1406 | init_local(); 1407 | init_like(); 1408 | } 1409 | return; 1410 | } 1411 | 1412 | // 当col == 1 时播放歌曲 1413 | if(col == 1 || col == 2) { 1414 | insert_nowplay(this ->filem + "/" + filemlist[row], filemlist[row], ":/coin/songer.png", "本地音乐暂无歌词"); // 插入到数据库 音乐队列中 1415 | reinit(1); 1416 | if(emit_i == 0) { 1417 | emit beginplay(); 1418 | emit_i++; 1419 | } // 防止重复激发 timer 1420 | initPlayer(); // 初始化播放按钮 1421 | } 1422 | 1423 | //收藏 歌曲 1424 | if(col == 3) { 1425 | QSqlQuery sql(db); 1426 | show_w = new show_list(user_id); 1427 | show_w ->show(); 1428 | 1429 | g_row = row; 1430 | connect(show_w, &show_list::return_item, this, &MainWindow::this_songlist); 1431 | return; 1432 | } 1433 | } 1434 | 1435 | 1436 | //like_w 点击 1437 | void MainWindow::fun_like_w(int row, int col) { 1438 | QString user_id_s = QString("%1").arg(user_id); // id 字符化 1439 | QSqlQuery like_p; 1440 | like_p.exec("select * from liketb where id = " + user_id_s + ";"); 1441 | while(like_p.next()) { 1442 | if (like_w ->item(row, 1) ->text() == like_p.value(2).value()) { 1443 | // 插入 1444 | g_file = like_p.value(1).value(); 1445 | g_name = like_p.value(2).value(); 1446 | g_image = like_p.value(3).value(); 1447 | g_lrc = like_p.value(4).value(); 1448 | break; 1449 | } 1450 | } 1451 | 1452 | 1453 | // 关于爱心歌曲 保存与删除 1454 | if(col == 0) { 1455 | QSqlQuery like_db; 1456 | like_db.exec("delete from liketb where id = " + user_id_s + " and name = \"" + like_w ->item(row, 1) ->text() + "\";"); 1457 | int lc_idx = std::find(filemlist.begin(), filemlist.end(), like_w ->item(row, 1) ->text()) - filemlist.begin(); 1458 | int np_idx = std::find(nowlist.begin(), nowlist.end(), like_w ->item(row, 1) ->text()) - nowlist.begin(); 1459 | // 不再是 爱心了 1460 | if(filemlist.count(like_w ->item(row, 1) ->text()) != 0) local_w ->item(lc_idx, 0) ->setIcon(QIcon(":/coin/like_c.png")); 1461 | if(nowlist.count(like_w ->item(row, 1) ->text()) != 0) songqueue ->item(np_idx, 0) ->setIcon(QIcon(":/coin/like_c.png")); 1462 | 1463 | like_w ->removeRow(row); 1464 | for(int j = 0; j < 5; ++j) like_w ->takeItem(row, j); 1465 | 1466 | 1467 | //初始化 1468 | init_like(); 1469 | init_local(); 1470 | return; 1471 | } 1472 | 1473 | 1474 | // 点击 播放音乐 1475 | if (col == 1 || col == 2) { 1476 | QSqlQuery like_p; 1477 | like_p.exec("select * from liketb where id = " + user_id_s + ";"); 1478 | while(like_p.next()) { 1479 | if (like_w ->item(row, 1) ->text() == like_p.value(2).value()) { 1480 | // 插入 1481 | insert_nowplay(like_p.value(1).value(), like_p.value(2).value(), like_p.value(3).value(), like_p.value(4).value()); 1482 | break; 1483 | } 1484 | } 1485 | } 1486 | 1487 | 1488 | // 加入歌单 1489 | if (col == 3) { 1490 | QSqlQuery sql(db); 1491 | show_w = new show_list(user_id); 1492 | show_w ->show(); 1493 | 1494 | g_row = row; 1495 | connect(show_w, &show_list::return_item, this, &MainWindow::this_songlist); 1496 | return; 1497 | } 1498 | } 1499 | 1500 | 1501 | // 播放歌曲发出信号 1502 | void MainWindow::play_net_Music(int row, int col) { 1503 | // 爱心操作 1504 | if(col == 0) { 1505 | is_insert = 0; // 不插入 1506 | //歌曲请求 row 是行号 1507 | } 1508 | 1509 | else if(col == 1 || col == 4) { 1510 | is_insert = 1; // 插入 1511 | } 1512 | 1513 | // 收藏到 歌单 1514 | else if (col == 5) { 1515 | in_list = 1; // 收藏不插入播放队列 1516 | is_insert = 2; 1517 | g_row = row; 1518 | } else return; 1519 | 1520 | //歌曲请求 row 是行号 1521 | QString KGAPISTR1 =QString("http://www.kugou.com/yy/index.php?r=play/getdata" 1522 | "&hash=%1&album_id=%2&_=1497972864535").arg(v_hash.at(row)) .arg(v_id.at(row)); 1523 | network_request2->setUrl(QUrl(KGAPISTR1)); 1524 | //这句话很重要,我们手动复制url放到浏览器可以获取json,但是通过代码不行,必须加上下面这句才可以 1525 | network_request2->setRawHeader("Cookie","kg_mid=2333"); 1526 | network_request2->setHeader(QNetworkRequest::CookieHeader, 2333); 1527 | network_manager2->get(*network_request2); 1528 | } 1529 | 1530 | 1531 | // 每个 歌单的 功能 1532 | void MainWindow::fun_list_w(int row, int col) { 1533 | QSqlQuery list_db; 1534 | QString user_id_s = QString("%1").arg(user_id); // id 字符化 1535 | QString list_id_s = QString("%0").arg(g_list_id); 1536 | 1537 | list_db.exec("select * from songlist where id = " + user_id_s + " and listid = " + list_id_s + " and name = \"" + list_w ->item(row, 1) ->text() + "\";"); 1538 | while (list_db.next()) { 1539 | g_file = list_db.value(2).value(); 1540 | g_name = list_db.value(3).value(); 1541 | g_image = list_db.value(4).value(); 1542 | g_lrc = list_db.value(5).value(); 1543 | break; 1544 | } 1545 | 1546 | // 爱心 1547 | if (col == 0) { 1548 | QString list_like = is_like(list_w ->item(row, 1) ->text()); 1549 | bool isplay = false; // 是否在播放队列 1550 | bool islocal = false; // 是否在local中 1551 | if (nowlist.count(list_w ->item(row, 1) ->text()) != 0) isplay = true; 1552 | if (filemlist.count(list_w ->item(row, 1) ->text()) != 0) islocal = true; 1553 | if(list_like == "0") { 1554 | list_w ->item(row, 0) ->setIcon(QIcon(":/coin/like.png")); 1555 | if (isplay) { 1556 | int idx = std::find(nowlist.begin(), nowlist.end(), list_w ->item(row, 1) ->text()) - nowlist.begin(); 1557 | songqueue ->item(idx, 0) ->setIcon(QIcon(":/coin/like.png")); 1558 | } 1559 | if (islocal) { 1560 | int idx = std::find(filemlist.begin(), filemlist.end(), list_w ->item(row, 1) ->text()) - filemlist.begin(); 1561 | local_w ->item(idx, 0) ->setIcon(QIcon(":/coin/like.png")); 1562 | } 1563 | 1564 | QSqlQuery list_q; 1565 | list_q.exec("select music from songlist where id = " + user_id_s + " and listid = " + list_id_s + " and name = \"" + list_w ->item(row, 1) ->text() + "\";"); 1566 | list_q.next(); 1567 | list_db.exec("insert into liketb values(" + user_id_s + ",\"" + list_q.value(0).value() + "\",\"" + list_w ->item(row, 1) ->text() + "\",\"" + ":/coin/songer.png" + "\", \"本地音乐暂无歌词\");"); 1568 | fun_like(list_w ->item(row, 1) ->text(), 0); 1569 | 1570 | //初始化 1571 | init_local(); 1572 | init_like(); 1573 | } else { 1574 | list_w ->item(row, 0) ->setIcon(QIcon(":/coin/like_c.png")); 1575 | if (isplay) { 1576 | int idx = std::find(nowlist.begin(), nowlist.end(), list_w ->item(row, 1) ->text()) - nowlist.begin(); 1577 | songqueue ->item(idx, 0) ->setIcon(QIcon(":/coin/like_c.png")); 1578 | } 1579 | if (islocal) { 1580 | int idx = std::find(filemlist.begin(), filemlist.end(), list_w ->item(row, 1) ->text()) - filemlist.begin(); 1581 | local_w ->item(idx, 0) ->setIcon(QIcon(":/coin/like_c.png")); 1582 | } 1583 | list_db.exec("delete from liketb where id = " + user_id_s + " and name = \"" + list_w ->item(row, 1) ->text() + "\";"); 1584 | fun_like(list_w ->item(row, 1) ->text(), 1); 1585 | 1586 | //初始化 1587 | init_local(); 1588 | init_like(); 1589 | } 1590 | return; 1591 | } 1592 | 1593 | 1594 | 1595 | //播放 1596 | if(col == 1 || col == 2) { 1597 | QSqlQuery list_q; 1598 | list_q.exec("select * from songlist where id = " + user_id_s + " and listid = " + list_id_s + " and name = \"" + list_w ->item(row, 1) ->text() + "\";"); 1599 | while(list_q.next()) { 1600 | QString f = list_q.value(2).value(); 1601 | QString n = list_q.value(3).value(); 1602 | QString im = list_q.value(4).value(); 1603 | QString l = list_q.value(5).value(); 1604 | insert_nowplay(f, n, im, l); 1605 | } 1606 | } 1607 | 1608 | 1609 | // 删除 1610 | if (col == 3) { 1611 | QSqlQuery list_q; 1612 | QString ex = QString("delete from songlist where id = " + user_id_s + " and listid = " + list_id_s + " and name = \"" + list_w ->item(row, 1) ->text() + "\";"); 1613 | list_q.exec(ex); 1614 | list_w ->removeRow(row); 1615 | } 1616 | 1617 | 1618 | // 收藏 1619 | if(col == 4) { 1620 | QSqlQuery sql(db); 1621 | show_w = new show_list(user_id); 1622 | show_w ->show(); 1623 | 1624 | g_row = row; 1625 | connect(show_w, &show_list::return_item, this, &MainWindow::this_songlist); 1626 | return; 1627 | } 1628 | 1629 | } 1630 | 1631 | 1632 | // 判断是否是网络音乐 1633 | bool MainWindow::is_net_music(QString file) { 1634 | QString buf; 1635 | for(int i = 0; i < file.size(); ++i) { 1636 | buf += file[i]; 1637 | if(buf == "http") return true; 1638 | } 1639 | return false; 1640 | } 1641 | 1642 | 1643 | // like_w的显示 1644 | void MainWindow::fun_like(QString name, int type) { 1645 | int row = like_w ->rowCount(); 1646 | 1647 | // 添加 1648 | if(type == 0) { 1649 | like_w ->setRowCount(row + 1); 1650 | 1651 | for(int i = 0; i <= 4; ++i) like_w ->setItem(row, i, new QTableWidgetItem()); 1652 | like_w ->item(row, 0) ->setIcon(QIcon(":/coin/like.png")); 1653 | like_w ->item(row, 1) ->setText(name); 1654 | like_w ->item(row, 2) ->setIcon(QIcon(":/coin/begin.png")); 1655 | like_w ->item(row, 3) ->setIcon(QIcon(":/coin/add.png")); 1656 | } else { // 删除 1657 | for(int i = 0; i < row; ++i) { 1658 | if (name == like_w ->item(i, 1) ->text()) { 1659 | for(int j = 0; j < 5; ++j) like_w ->takeItem(i, j); 1660 | } 1661 | } 1662 | } 1663 | } 1664 | 1665 | // add to songlist 1666 | void MainWindow::this_songlist(QListWidgetItem *item) { 1667 | QString user_id_s = QString("%1").arg(user_id); // id 字符化 1668 | QString name = item ->text(); // 获取这个 歌单的名字 1669 | QString list_id_lim; 1670 | QSqlQuery sql(db); 1671 | 1672 | sql.exec("select * from listname where name = \"" + name + "\"and id = " + user_id_s + ";"); 1673 | while(sql.next()) { 1674 | list_id_lim = QString("%0").arg(sql.value(2).value()); 1675 | qDebug() << "list_id is: " << list_id_lim << endl; 1676 | } 1677 | 1678 | 1679 | sql.exec("select name from songlist where id = " + user_id_s + " and listid = " + list_id_lim + ";"); 1680 | while(sql.next()) { 1681 | if (sql.value(0).value() == g_name) { 1682 | QMessageBox::information(this, "提示", "该首歌已经录入了~", QMessageBox::Ok); 1683 | return; 1684 | } 1685 | } 1686 | 1687 | 1688 | if (g_row != -1) { 1689 | QString ex = QString("insert into songlist values(" + user_id_s + ", " + list_id_lim + ", \"" + g_file + "\", \"" + g_name + "\", \"" + g_image + "\", \"" + g_lrc + "\");"); 1690 | g_row = -1; 1691 | qDebug() << ex << endl; 1692 | bool ok = sql.exec(ex); 1693 | if (ok) qDebug() << "insert list ok" << endl; 1694 | else qDebug() << "no insert list" << endl; 1695 | } 1696 | return; 1697 | } 1698 | 1699 | 1700 | //将图片变成圆角 1701 | QPixmap MainWindow::PixmapToRound(QPixmap &pix, int radius) { 1702 | QSize size(2 * radius, 2 * radius); 1703 | QBitmap bit(size); 1704 | QPainter painter(&bit); 1705 | 1706 | //经典抗锯齿 1707 | painter.setRenderHint(QPainter::Antialiasing); 1708 | painter.setRenderHint(QPainter::SmoothPixmapTransform); 1709 | 1710 | painter.fillRect(0, 0, size.width(), size.height(), Qt::white); 1711 | painter.setBrush(Qt::black); 1712 | painter.drawRoundedRect(0, 0, size.width(), size.height(), 10, 10); 1713 | QPixmap ans = pix.scaled(size); 1714 | ans.setMask(bit); 1715 | return ans; 1716 | } 1717 | 1718 | 1719 | // 搜索 text 通过 http网络, 给予finish 相应 1720 | void MainWindow::search(QString text, int idx) { 1721 | QString net_search = QString("http://mobilecdn.kugou.com/api/v3/search/song?format=json&keyword=%1&page=%2&pagesize=18a").arg(text).arg(idx); 1722 | net_request ->setUrl(QUrl(net_search)); 1723 | net_manager ->get(*net_request); 1724 | } 1725 | 1726 | 1727 | // 判断是不是喜欢音乐 1728 | QString MainWindow::is_like(QString file) { 1729 | 1730 | QSqlQuery sql; 1731 | sql.exec("select * from liketb;"); 1732 | while (sql.next()) { 1733 | if (sql.value(0).value() == user_id && sql.value(2).value() == file) return "1"; 1734 | } 1735 | return "0"; 1736 | } 1737 | 1738 | // net_manager finish 响应reply 开始在tableWidget 上显示 1739 | void MainWindow::reply(QNetworkReply* re) { 1740 | QVariant status_code = re ->attribute(QNetworkRequest::HttpStatusCodeAttribute); 1741 | 1742 | if (re ->error() == QNetworkReply::NoError) { 1743 | QByteArray bytes = re ->readAll(); 1744 | QString res(bytes); 1745 | parseJson(res, index); 1746 | index += 20; 1747 | } else { 1748 | qDebug() << "error!" << endl; 1749 | } 1750 | } 1751 | 1752 | // 解析 json 显示到 tablewidget上 1753 | void MainWindow::parseJson(QString json, int idx) { 1754 | QString songname_original; //歌曲名 1755 | QString singername; //歌手 1756 | QString hashStr; //hash 1757 | QString album_name; //专辑 1758 | int duration; //时间 1759 | QByteArray byte_array; 1760 | QJsonParseError json_error; 1761 | QJsonDocument parse_doucment = QJsonDocument::fromJson(byte_array.append(json), &json_error); 1762 | if (json_error.error == QJsonParseError::NoError) 1763 | { 1764 | if (parse_doucment.isObject()) 1765 | { 1766 | QJsonObject rootObj = parse_doucment.object(); 1767 | if (rootObj.contains("data")) 1768 | { 1769 | QJsonValue valuedata = rootObj.value("data"); 1770 | if (valuedata.isObject()) 1771 | { 1772 | QJsonObject valuedataObject = valuedata.toObject(); 1773 | if (valuedataObject.contains("info")) 1774 | { 1775 | QJsonValue valueArray = valuedataObject.value("info"); 1776 | if (valueArray.isArray()) 1777 | { 1778 | QJsonArray array = valueArray.toArray(); 1779 | int size = array.size(); 1780 | for (int i = idx, j = 0; i < size + idx && j < size; i++, ++j) 1781 | { 1782 | QJsonValue value = array.at(j); 1783 | if (value.isObject()) 1784 | { 1785 | QJsonObject object = value.toObject(); 1786 | if (object.contains("songname_original"))//歌曲名 1787 | { 1788 | QJsonValue AlbumID_value = object.take("songname_original"); 1789 | if (AlbumID_value.isString()) 1790 | { 1791 | songname_original = AlbumID_value.toString(); 1792 | } 1793 | } 1794 | if (object.contains("singername"))//歌手 1795 | { 1796 | QJsonValue AlbumID_value = object.take("singername"); 1797 | if (AlbumID_value.isString()) 1798 | { 1799 | singername = AlbumID_value.toString(); 1800 | } 1801 | } 1802 | if (object.contains("album_name"))//专辑 1803 | { 1804 | QJsonValue AlbumID_value = object.take("album_name"); 1805 | if (AlbumID_value.isString()) 1806 | { 1807 | album_name = AlbumID_value.toString(); 1808 | } 1809 | } 1810 | if (object.contains("hash")) //hash 1811 | { 1812 | QJsonValue FileHash_value = object.take("hash"); 1813 | if (FileHash_value.isString()) 1814 | { 1815 | hashStr = FileHash_value.toString(); 1816 | //用Vector保存每首歌曲的hash 1817 | v_hash.push_back(FileHash_value.toString()); 1818 | } 1819 | } 1820 | if (object.contains("album_id")) 1821 | { 1822 | QJsonValue FileHash_value = object.take("album_id"); 1823 | if (FileHash_value.isString()) 1824 | { 1825 | //用Vector保存每首歌曲的album_id 1826 | v_id.push_back(FileHash_value.toString()); 1827 | } 1828 | } 1829 | if (object.contains("duration"))//时长 1830 | { 1831 | QJsonValue AlbumID_value = object.take("duration").toInt(); 1832 | duration = AlbumID_value.toInt(); 1833 | } 1834 | //将解析出的内容放到列表中 1835 | tab_search ->setRowCount(i + 1); 1836 | 1837 | // 爱心 1838 | QString is_like_ = is_like(songname_original); 1839 | tab_search ->setItem(i, 0, new QTableWidgetItem()); 1840 | if(is_like_ == "0") tab_search ->item(i, 0) ->setIcon(QIcon(":/coin/like_c.png")); 1841 | else tab_search ->item(i, 0) ->setIcon(QIcon(":/coin/like_c.png")); 1842 | 1843 | tab_search ->setItem(i,1,new QTableWidgetItem(songname_original)); 1844 | //文字居中 1845 | tab_search->item(i,1)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter); 1846 | 1847 | 1848 | tab_search ->setItem(i,2,new QTableWidgetItem(singername)); 1849 | tab_search ->item(i,2)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter); 1850 | 1851 | tab_search ->setItem(i,3,new QTableWidgetItem(album_name)); 1852 | tab_search ->item(i,3)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter); 1853 | 1854 | tab_search ->setItem(i,4,new QTableWidgetItem()); 1855 | tab_search ->item(i,4) ->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter); 1856 | tab_search ->item(i, 4) ->setIcon(QIcon(":/coin/begin.png")); 1857 | 1858 | tab_search ->setItem(i,5,new QTableWidgetItem()); 1859 | tab_search ->item(i,5) ->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter); 1860 | tab_search ->item(i, 5) ->setIcon(QIcon(":/coin/add.png")); 1861 | 1862 | QString time = QString("%1:%2").arg(duration/60).arg(duration%60); 1863 | tab_search ->setItem(i,6,new QTableWidgetItem(time)); 1864 | tab_search ->item(i,6)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter); 1865 | } 1866 | } 1867 | } 1868 | } 1869 | } 1870 | } 1871 | } 1872 | } 1873 | } 1874 | 1875 | 1876 | void MainWindow::reply2(QNetworkReply *re) // 如果net_messageer finish 判断是否载入 然后进行js 1877 | { 1878 | //获取响应的信息,状态码为200表示正常 1879 | QVariant status_code = re->attribute(QNetworkRequest::HttpStatusCodeAttribute); 1880 | 1881 | //无错误返回 1882 | if(re->error() == QNetworkReply::NoError) 1883 | { 1884 | QByteArray bytes = re->readAll(); //获取字节 1885 | QString result(bytes); //转化为字符串 1886 | parseJson2(result);//解析json 1887 | } 1888 | else 1889 | { 1890 | //处理错误 1891 | qDebug()<<"歌曲播放失败"; 1892 | } 1893 | } 1894 | 1895 | 1896 | //解析 json 1897 | void MainWindow::parseJson2(QString json) { 1898 | QString audio_name;//歌手-歌名 1899 | QString play_url;//播放地址 1900 | QString img; 1901 | QByteArray byte_array; 1902 | QJsonParseError json_error; 1903 | QJsonDocument parse_doucment = QJsonDocument::fromJson(byte_array.append(json), &json_error); 1904 | if(json_error.error == QJsonParseError::NoError) { 1905 | if(parse_doucment.isObject()) 1906 | { 1907 | QJsonObject rootObj = parse_doucment.object(); 1908 | if(rootObj.contains("data")) 1909 | { 1910 | QJsonValue valuedata = rootObj.value("data"); 1911 | if(valuedata.isObject()) 1912 | { 1913 | QJsonObject valuedataObject = valuedata.toObject(); 1914 | QString play_urlStr(""); 1915 | if(valuedataObject.contains("play_url")) 1916 | { 1917 | QJsonValue play_url_value = valuedataObject.take("play_url"); 1918 | if(play_url_value.isString()) 1919 | { 1920 | play_urlStr = play_url_value.toString(); //歌曲的url 1921 | if(play_urlStr!="") 1922 | { 1923 | net_file = play_urlStr; 1924 | // Player ->setMedia(QUrl(play_urlStr)); 1925 | // Player->play(); 1926 | } 1927 | } 1928 | } 1929 | if(valuedataObject.contains("audio_name")) 1930 | { 1931 | QJsonValue play_name_value = valuedataObject.take("audio_name"); 1932 | if(play_name_value.isString()) 1933 | { 1934 | QString audio_name = play_name_value.toString(); //歌曲名字 1935 | if(audio_name!="") 1936 | { 1937 | //显示 1938 | qDebug()<net_image = audio_name; // 保存图片路径 1956 | network_request3->setUrl(QUrl(audio_name)); 1957 | network_manager3->get(*network_request3); // 反馈信号 准备解析json 1958 | } 1959 | } 1960 | } 1961 | // 歌词显示 1962 | idd = 0; // 初始化 lrc_itm 的索引 1963 | if (valuedataObject.contains("lyrics")) //lrc 1964 | { 1965 | QJsonValue play_url_value = valuedataObject.take("lyrics"); 1966 | if (play_url_value.isString()) 1967 | { 1968 | QString play_lrcStr = play_url_value.toString(); 1969 | this ->net_lrc = play_lrcStr; // 保存http 歌词 1970 | if (play_urlStr != "") 1971 | { 1972 | if (play_lrcStr != "") 1973 | { //将整个歌词给s 1974 | QString s = play_lrcStr; 1975 | buildlrc(s); 1976 | p_lrcit = lrcMap.begin() - 1; 1977 | } 1978 | else 1979 | { 1980 | //没有歌词; 1981 | } 1982 | } 1983 | } 1984 | } 1985 | g_file = net_file; 1986 | g_name = net_name; 1987 | g_image = net_image; 1988 | g_lrc = net_lrc; 1989 | 1990 | if(is_insert == 1) insert_nowplay(net_file, net_name, net_image, net_lrc); //将网络歌曲插入到队列 1991 | else if (is_insert == 0) { // 插入到喜爱音乐里面 1992 | QSqlQuery like_db; 1993 | int row = -1; 1994 | for(int i = 0; i < tab_search->rowCount(); ++i) { 1995 | if (getPName(net_name) == tab_search ->item(i, 1) ->text()) { 1996 | row = i; 1997 | break; 1998 | } 1999 | } 2000 | 2001 | if (row == -1) return; 2002 | QString user_id_s = QString("%1").arg(user_id); // id 字符化 2003 | QString is_like_net = is_like(net_name); 2004 | int np_idx = std::find(nowlist.begin(), nowlist.end(), net_name) - nowlist.begin(); 2005 | if (is_like_net == "0") { 2006 | tab_search ->item(row, 0) ->setIcon(QIcon(":/coin/like.png")); 2007 | if(nowlist.count(tab_search->item(row, 1) ->text()) != 0) songqueue ->item(np_idx, 0) ->setIcon(QIcon(":/coin/like.png")); 2008 | like_db.exec("insert into liketb values(" + user_id_s + ",\"" + net_file + "\",\"" + net_name + "\",\"" + net_image + "\", \"" + net_lrc + "\");"); 2009 | fun_like(net_name, 0); 2010 | } else { 2011 | tab_search ->item(row, 0) ->setIcon(QIcon(":/coin/like_c.png")); 2012 | if(nowlist.count(tab_search->item(row, 1) ->text()) != 0) songqueue ->item(np_idx, 0) ->setIcon(QIcon(":/coin/like_c.png")); 2013 | like_db.exec("delete from liketb where id = " + user_id_s + " and music = \"" + net_file + "\";"); 2014 | fun_like(net_name, 1); 2015 | } 2016 | } 2017 | 2018 | if (in_list == 1) { 2019 | in_list = 0; // 变回0 避免不必要的错误 2020 | 2021 | // 执行 收藏歌曲操作 2022 | QSqlQuery sql(db); 2023 | show_w = new show_list(user_id); 2024 | show_w ->show(); 2025 | 2026 | connect(show_w, &show_list::return_item, this, &MainWindow::this_songlist); 2027 | } 2028 | } 2029 | //下一篇的歌词获取也是在这里添加代码 2030 | else { 2031 | qDebug()<<"出错"; 2032 | } 2033 | } 2034 | } 2035 | } 2036 | } 2037 | 2038 | // 图片获取 触发信号开始解析 2039 | void MainWindow::reply3(QNetworkReply *reply) 2040 | { 2041 | //获取响应的信息,状态码为200表示正常 2042 | QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute); 2043 | 2044 | //无错误返回 2045 | if(reply->error() == QNetworkReply::NoError && is_insert == 1) 2046 | { 2047 | 2048 | QByteArray bytes = reply->readAll(); //获取字节 2049 | //由于获取的图片像素过大,而我们显示的图片很小,所以我们需要压缩图片的像素,我们label的大小为45*45,所以我们把图片压缩为45*45 2050 | QSize size(50, 50); 2051 | QBitmap bit(size); 2052 | QPixmap pixmap, s_pix, w_l_pixmap; 2053 | pixmap.loadFromData(bytes); 2054 | w_l_pixmap.loadFromData(bytes); 2055 | w_l_pixmap = PixmapToRound(w_l_pixmap, 180); 2056 | 2057 | // 歌曲图标 2058 | s_pix = PixmapToRound(pixmap, 30); 2059 | music_map ->setFixedSize(s_pix.width(), s_pix.height()); 2060 | music_map ->setIconSize(QSize(s_pix.width(), s_pix.height())); 2061 | music_map ->setIcon(QIcon(s_pix)); 2062 | music_map ->setStyleSheet("QPushButton{border: 0px;}"); 2063 | music_map ->show(); 2064 | 2065 | // 歌词窗口 绘制图片 2066 | lrc_w->l->setPixmap(w_l_pixmap); 2067 | } 2068 | else 2069 | { 2070 | //处理错误 2071 | qDebug()<<"NET_图片显示错误"; 2072 | } 2073 | } 2074 | 2075 | // 同上 只不过是本地的音乐图片 处理 2076 | void MainWindow::local_img(QString im) { 2077 | QPixmap pix, w_p; 2078 | pix.load(im); 2079 | pix = PixmapToRound(pix, 30); 2080 | 2081 | w_p.load(im); 2082 | w_p = PixmapToRound(w_p, 180); 2083 | 2084 | music_map ->setFixedSize(pix.width(), pix.height()); 2085 | music_map ->setIconSize(QSize(pix.width(), pix.height())); 2086 | music_map ->setIcon(QIcon(pix)); 2087 | music_map ->setStyleSheet("QPushButton{border: 0px;}"); 2088 | music_map ->show(); 2089 | 2090 | lrc_w ->l ->setPixmap(w_p); 2091 | } 2092 | 2093 | // 初始化歌词播放 2094 | void MainWindow::initlrc_win() { 2095 | // 初始化roll_cnt roll lrc_itm 2096 | lrc_itm.clear(); 2097 | roll_cnt = 0; 2098 | roll = 0; 2099 | 2100 | lrc_l = new QListWidget(lrc_w); 2101 | lrc_l ->setStyleSheet("QListWidget::item{height: 45px;}" 2102 | "QListWidget::item::hover{background: rgb(228, 228, 228);}"); 2103 | lrc_l ->setGeometry(650, 80, 340, 360); 2104 | lrc_l ->setFrameShape(QListWidget::NoFrame); // 无边框 2105 | lrc_l ->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); //无进度条 2106 | lrc_l ->setFocusPolicy(Qt::NoFocus); // 点击无 solid 2107 | 2108 | //准备载入歌词 到窗口 2109 | QMap::iterator it = lrcMap.begin(); 2110 | 2111 | while(it != lrcMap.end()) { 2112 | QFont font; 2113 | font.setFamily("幼圆"); 2114 | font.setPointSize(13); 2115 | font.setWeight(60); 2116 | 2117 | QString lrc = it.value(); 2118 | QListWidgetItem *lrc_it = new QListWidgetItem(it.value(), lrc_l); 2119 | lrc_it ->setFont(font); 2120 | lrc_itm.push_back(lrc_it); 2121 | lrc_l ->addItem(lrc_it); 2122 | it++; 2123 | } 2124 | lrc_l ->show(); 2125 | } 2126 | 2127 | // 解析构建歌词 2128 | void MainWindow::buildlrc(QString s) { 2129 | // 初始化 2130 | lrcMap.clear(); 2131 | lrc_idx.clear(); 2132 | idd = 0; 2133 | 2134 | // 本地音乐处理 2135 | if(s == "本地音乐暂无歌词") { 2136 | // qDebug() << "local lrc!" << endl; 2137 | lrcMap[0] = s; 2138 | lrc_idx[0] = 0; 2139 | initlrc_win(); 2140 | return; 2141 | } 2142 | 2143 | QStringList s1 = s.split("\n"); 2144 | for (int i = 3; i < s1.size() - 1; i++) 2145 | { 2146 | QString ss1 = s1[i]; 2147 | //歌词中开头有一些是无意义的字符,用正则表达式判断,只保存包含有时间戳的字符串。 2148 | QRegExp ipRegExp = QRegExp("\\[\\d\\d\\S\\d\\d\\S\\d\\d\\]"); 2149 | //若包含则返回flase 2150 | bool match = ipRegExp.indexIn(ss1); 2151 | if (match == false) 2152 | { 2153 | //时间解析格式(分*60+秒)*100+厘秒 2154 | int s_1 = ss1.mid(1, 2).toInt(); //分 2155 | int s_2 = ss1.mid(4, 2).toInt(); //秒 2156 | int s_3 = ss1.mid(7, 2).toInt(); //厘秒 2157 | int s_count = ((s_1 * 60 + s_2) * 100 + s_3) * 10; //毫秒换算 2158 | 2159 | int lrctime = s_count; 2160 | QString lrcstr = ss1.mid(10); // 歌词载入 2161 | // qDebug() << "durtion: " << s_count << " " << lrcstr << endl; 2162 | //用Qmap来保存 2163 | 2164 | lrcMap[lrctime] = lrcstr; 2165 | lrc_idx[lrctime] = idd++; 2166 | } 2167 | } 2168 | initlrc_win(); 2169 | } 2170 | 2171 | 2172 | void MainWindow::songlist_add(QString name) { 2173 | QFont font; 2174 | font.setFamily("幼圆"); 2175 | font.setPointSize(11); 2176 | 2177 | QString user_id_s = QString("%0").arg(user_id); 2178 | QString list_id_s = QString("%0").arg(song_list_id); 2179 | 2180 | QSqlQuery l_sql(db); 2181 | l_sql.exec("select name from listname where id = " + user_id_s + ";"); 2182 | while(l_sql.next()) { 2183 | if (l_sql.value(0).value() == name) { 2184 | QMessageBox::information(this, "提示", "失败(可能已经有该名字的歌单了)!", QMessageBox::Ok); 2185 | return; 2186 | } 2187 | } 2188 | 2189 | song_list_id += 1; 2190 | QString n_list_id = QString("%0").arg(song_list_id); 2191 | l_sql.exec("insert into listname values(" + user_id_s + ", \"" + name + "\", " + list_id_s + ");"); 2192 | l_sql.exec("update user set cnt = " + n_list_id + " where id = " + user_id_s + ";"); 2193 | 2194 | 2195 | songlist ->addItem(new QListWidgetItem(name)); 2196 | } 2197 | 2198 | 2199 | void MainWindow::show_List_music(QListWidgetItem *c_item) { 2200 | // 初始化 2201 | list_w ->clear(); 2202 | list_w ->setRowCount(0); 2203 | 2204 | list_w ->setGeometry(musiclist->width(), btnL->height(),this ->width() - musiclist->width(), this ->height() - btnL->height() - 100); 2205 | list_w ->setHorizontalHeaderLabels(QStringList() << " " << "歌曲" << " " << " " << " " << " "); 2206 | list_w ->setStyleSheet("QTableWidget::Item::selected{background: white;}" 2207 | "QHeaderView::section{border: 0px solid white};"); 2208 | 2209 | 2210 | //delete init 2211 | if (delete_btn) delete_btn ->hide(); // 上一个 消失 2212 | 2213 | QWidget *w = new QWidget(); 2214 | delete_btn = new mybtn(":/coin/cls.png", ":/coin/cls_1.png"); 2215 | delete_btn ->setParent(w); 2216 | delete_btn ->move(90, 10); 2217 | songlist ->setItemWidget(c_item, w); 2218 | ///////////////////////////////////////////////////////////////////////// 2219 | 2220 | QString user_id_s = QString("%0").arg(user_id); 2221 | QString name = c_item ->text(); 2222 | 2223 | QSqlQuery sql; 2224 | sql.exec("select listid from listname where id = " + user_id_s + " and name = \"" + name + "\";"); 2225 | sql.next(); 2226 | QString list_id = QString("%0").arg(sql.value(0).value()); 2227 | 2228 | QSqlQuery i_sql; 2229 | i_sql.exec("select * from songlist where id = " + user_id_s + ", and listid = " + list_id + ";"); 2230 | 2231 | while(i_sql.next()) { 2232 | int row = list_w ->rowCount(); 2233 | list_w ->setRowCount(row + 1); 2234 | for (int i = 0; i < 6; ++i) list_w ->setItem(row, i, new QTableWidgetItem()); 2235 | 2236 | list_w ->item(row, 0) ->setIcon(QIcon(":/coin/like.png")); // 爱心 2237 | list_w ->item(row, 1) ->setText(i_sql.value(3).value()); // 歌名 2238 | list_w ->item(row, 2) ->setIcon(QIcon(":/coin/begin.png")); // 播放 2239 | list_w ->item(row, 3) ->setIcon(QIcon(":/coin/delete.png")); // 删除 2240 | list_w ->item(row, 4) ->setIcon(QIcon(":/coin/add.png")); // 增添 2241 | } 2242 | 2243 | local_w ->hide(); 2244 | tab_search ->hide(); 2245 | like_w ->hide(); 2246 | list_w ->show(); 2247 | g_list_id = list_id; // 全局变量 2248 | 2249 | connect(delete_btn, &mybtn::clicked, [=] () { // 当点击delete_btn 删除这个歌单 2250 | QMessageBox::StandardButton b = QMessageBox::question(this, "询问", "是否删除这个歌单?", QMessageBox::Yes | QMessageBox::No); 2251 | if (b == QMessageBox::Yes) { 2252 | int row = songlist ->row(c_item); 2253 | songlist ->takeItem(row); 2254 | delete_btn = nullptr; 2255 | 2256 | 2257 | QSqlQuery sql_l; 2258 | sql_l.exec("delete from songlist where id = " + user_id_s + " and listid = " + list_id + ";"); 2259 | sql_l.exec("delete from listname where id = " + user_id_s + " and listid = " + list_id + ";"); 2260 | } 2261 | }); 2262 | } 2263 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 2264 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 2265 | 2266 | 2267 | void MainWindow::mousePressEvent(QMouseEvent * e) { 2268 | // 音量进度条 2269 | if (e ->pos().x() >= 670 && e ->pos().x() < 720 && e ->pos().y() >= 635 && e ->pos().y() <= 640) { 2270 | V = e ->pos().x() - 670; 2271 | Player ->setVolume(V * 2); // 调整音量 2272 | if (this ->volF) { 2273 | volF = false; 2274 | volbt->is_play = false; 2275 | volbt ->setIcon(QIcon(":/coin/vol.png")); 2276 | } 2277 | update(); 2278 | } 2279 | 2280 | //点击进度条 2281 | if( e->pos().y() > 640 && e->pos().y() < 675 && e ->pos().x() >= 220 && e ->pos().x() < 785) 2282 | { 2283 | int value; 2284 | value = e->pos().x(); 2285 | time ->stop(); 2286 | if(value < 220) 2287 | { 2288 | X = 56; 2289 | } 2290 | else if (value > 780) 2291 | { 2292 | X = 635; 2293 | } 2294 | else 2295 | { 2296 | X = value - 150; 2297 | } 2298 | if(X <= 620) 2299 | { 2300 | changePro(); 2301 | } 2302 | update(); // 重绘 2303 | setCursor(Qt::PointingHandCursor); 2304 | if(playf) time ->start(); 2305 | } 2306 | 2307 | // 点击窗口 2308 | if(e ->button() == Qt::LeftButton && e ->pos().y() >= 0 && e ->pos().y() < 50) { 2309 | leftflag = true; 2310 | } 2311 | glbal_p = e ->globalPos() - this ->pos(); 2312 | } // init mousePressEvent 2313 | 2314 | void MainWindow::mouseMoveEvent(QMouseEvent *e) { 2315 | // 音量进度条 2316 | if (e ->pos().x() >= 670 && e ->pos().x() < 720 && e ->pos().y() >= 635 && e ->pos().y() <= 640) { 2317 | V = e ->pos().x() - 670; 2318 | Player ->setVolume(V * 2); // 调整音量 2319 | if (this ->volF) { 2320 | volF = false; 2321 | volbt->is_play = false; 2322 | volbt ->setIcon(QIcon(":/coin/vol.png")); 2323 | } 2324 | update(); 2325 | } 2326 | 2327 | // 移动进度条 2328 | if (e->pos().y() > 640 && e->pos().y() < 675 && e ->pos().x() >= 220 && e ->pos().x() < 785) { 2329 | time ->stop(); 2330 | int value = e->pos().x(); 2331 | if (value < 220) 2332 | { 2333 | X = 56; 2334 | } 2335 | else if (value > 780) 2336 | { 2337 | X = 635; 2338 | } 2339 | else 2340 | { 2341 | X = e->pos().x() - 150; 2342 | } 2343 | 2344 | changePro(); 2345 | update(); 2346 | setCursor(Qt::PointingHandCursor); 2347 | if(playf) time ->start(); 2348 | } 2349 | 2350 | // 移动窗口 2351 | if(leftflag && e ->pos().y() >= 0 && e ->pos().y() < 50) { 2352 | win_p = e ->globalPos(); 2353 | this ->move(win_p - glbal_p); 2354 | } 2355 | } // init mouseMoveEvent 2356 | 2357 | // 通过拖动进度条 改变歌曲进度 2358 | void MainWindow::changePro() { 2359 | this ->positontime = (float)((X - 56) / (float)580.0) * Player ->duration(); //根据鼠标的坐标 反推出此时 音乐的进度 2360 | Player ->setPosition(this ->positontime); // 改变音乐的进度 2361 | double secondtime1 = positontime / 1000; // 当前播放的总秒数 2362 | int minint1 = secondtime1 / 60; // 当前播放的分钟 2363 | int secondint1 = secondtime1 - minint1 * 60; // 当前播放的秒 2364 | 2365 | int curra = minint1 / 10; 2366 | int currb = minint1 % 10; 2367 | int currc = secondint1 / 10; 2368 | int currd = secondint1 % 10; 2369 | current = QString("%0%1:%2%3").arg(curra) .arg(currb).arg(currc). arg(currd); 2370 | } 2371 | 2372 | 2373 | MainWindow::~MainWindow() 2374 | { 2375 | delete ui; 2376 | } 2377 | 2378 | -------------------------------------------------------------------------------- /src/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include // 实现多媒体的 应用 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include // 用来显示当前播放歌曲 19 | #include 20 | #include 21 | #include // 音量进度条 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include "mybtn.h" 28 | #include"startbtn.h" 29 | #include "mylab.h" 30 | #include "mylistwidget.h" 31 | #include "sign_in_win.h" 32 | #include "lrcwidget.h" 33 | #include "mytablewidget.h" 34 | #include "create_list.h" 35 | #include "show_list.h" 36 | #include "lrc_win_main.h" 37 | 38 | QT_BEGIN_NAMESPACE 39 | namespace Ui { class MainWindow; } 40 | QT_END_NAMESPACE 41 | 42 | class MainWindow : public QMainWindow 43 | { 44 | Q_OBJECT 45 | 46 | public: 47 | bool f; 48 | QVector l_vb = QVector(10000, nullptr); // 用来存储 音乐item 里的按钮vb 49 | QVector l_vi = QVector(10000, nullptr), lrc_itm; // 用来存储 一首歌的item 50 | QVector v_hash, v_id; // 保存 hash 和 id 用来 播放歌曲 51 | QMap lrcMap; // 歌词保存 52 | QMap lrc_idx; // 存储 itme idx 53 | QMap::iterator p_lrcit; // 歌词上一个保存的it 54 | 55 | MainWindow(QWidget *parent = nullptr); 56 | ~MainWindow(); 57 | QString current="00:00"; // 目前播放的时间 58 | QString all="00:00"; // 该歌曲的 时间 59 | int bufferVol; // 用来 在 valbt 的点击事件中 记录 还未变成 0 的THIS ->V 也会在之后的 点击中 将 V 的值 重新 赋值给V 60 | bool playf = false; // 当点击playf 时 如果是false 则为 播放音乐 否则为 停止音乐 61 | bool leftflag = false; // 如果 leftflag == true 那么开始移动屏幕 62 | bool volF = false; //volF = false 时 静音 volF = true 时 返回当时 的 音量 63 | bool is_open = false; // 当为false 时 点击 打开列表 否则关闭列表 64 | bool is_delete = false; // 如果is_delete = false 那么调用readmysql 里面的deletenowplay 否则调用queuefun里面的deletenowplay 65 | bool is_net = false; // 根据 is_net 的不同判断是否时 网络音乐 从而实现不同的操作 66 | bool sign_in = false; // 只有登录成功才可以听歌 67 | bool lrc_open = false; // 如果false 则显示lrc_win 如果true 则关闭 68 | int flag = 0; // 当flag = 0 时 maxb 让窗口变大 flag = 1 时让窗口变成原来的样子 69 | int index = 0; // 用来在tablewidget 中显示歌曲 70 | int is_insert = 1; // 是否真的插入net_music 如果为 0 no , 1 yes! 71 | int emit_i = 0; // 如何 初始化完成 则为1 否则如果使用 本地音乐初始化 则为 0 可以调用 beginplay信号 72 | int roll, roll_cnt; // roll 代表真实歌词下滚行数, 而 roll_cnt 代表歌词读取次数 当达到4次时 才允许歌词下滚 73 | int idd; // 歌词item idx 74 | int user_id = -1; // 用户的id 75 | int song_list_id = 0; // 用户歌单号 或者是 歌单数量 76 | int is_change = 0; // 当等于 1 时 说明是 改变了歌曲那么 要singleshot 77 | QString list_id; // 记录 list_id 78 | int g_row = -1; // 用于 加入收藏队列时 使用保存 item的行号 79 | int in_list = 0; //当 为1 表示 要执行 收藏操作, 为 0时 则不执行 80 | int is_looper = 0; // 是否歌曲单曲播放 81 | 82 | 83 | 84 | sign_in_win* sign; // 登录界面 85 | mybtn *minb, *maxb, *clsb, *nextbt, *prevbt, *winicon, *listbtn, *btn_search, *sign_in_btn, *local_btn; // 窗口的最大化 最小化 关闭 下一首 上一首 的按钮 主窗口图标 音乐列表图标, 搜索按钮 86 | //登录按钮 本地音乐添加窗口 87 | mybtn *add_songlist, *delete_btn = nullptr, *lrc_btn; // 添加歌单按钮 删除歌单按钮 点击在windows 窗口显示歌词 88 | QSqlDatabase db; // 用来连接数据库 来调用歌曲 89 | QMediaPlayer *Player; // 实现多媒体操控 90 | QMediaPlaylist *Playlist; // 多媒体列表 91 | QString filem; // 音乐文件路径 "F:\\qq音乐\\Music"; 92 | QString pname, mname; // 歌手名字 和 歌曲名字 93 | QString net_file, net_name, net_image, net_lrc; // 记录http 的路径 歌曲名字 歌曲图片 歌词 94 | QStringList filemlist, nowplaylist, nowlist, nowlist_im, list_col_table, nowlist_lrc, song_col_name, likelist; // 本地音乐文件路径, 当前播放音乐路径 歌词 当前的播放音乐名字(当放入songqueue 后可以clear) 95 | startbtn* playbt ,*volbt, *looper_m; // 音乐的播放按钮 音量按钮 播放模式按钮 96 | QLabel* musicL, *btnL ,*liftLabel ,*rightLabel, *PlayL, *sign_L; // 用来显示当前播放歌曲 窗口按钮的封装 左时间显示 右边时间显示 播放控件的封装 登录标签 97 | // mytabwidget *mainmusic; // 设置音乐播放器 主窗口! 98 | mylab* mymusic, *mylist; // 我的音乐的标签 我的歌单标签 99 | mylistwidget *musiclist ,*songlist; // 喜爱歌单 和 本地音乐的创建 100 | mytablewidget *songqueue; //播放队列列表 101 | QTableWidget *local_w, *like_w, *list_w; //本地音乐列表 喜爱音乐窗口 自定义歌单窗口 102 | QListWidgetItem* likemusiclist, *mylocalmusic; // 喜爱的歌单 和 本地音乐 103 | QLineEdit* search_line; // 搜索框 104 | QTableWidget *tab_search; //用来显示歌曲 105 | QTabWidget *lrcwin; // 歌词显示窗口 106 | QPushButton *music_map; //当前播放音乐的图标 107 | lrcwidget* lrc_w; // 歌词窗口(地基) 108 | QListWidget* lrc_l; // 歌词显示 109 | create_list *n_list; // 显示窗口输入歌单的名字然后创建 110 | show_list *show_w; // 当点击收藏时 显示所有的自定义歌单 111 | QString g_file, g_name, g_image, g_lrc; // 用于 收藏歌单 112 | QString g_list_id; // 用于歌单操作使用 113 | lrc_win_main *lrc; // 歌词主窗口 114 | bool is_show = false; // 当为true 时显示歌词主窗口 115 | 116 | 117 | //http 118 | QNetworkAccessManager *net_manager, *network_manager2, *network_manager3; 119 | QNetworkRequest *net_request, *network_request2, *network_request3; 120 | 121 | 122 | //实现窗口的 任意拖动 123 | QPoint glbal_p, win_p; // 用win_p - glabel_p 就是 偏移量 124 | void changePro(); // 通过拖动进度条 改变歌曲进度 125 | void mouseMoveEvent(QMouseEvent*); // 鼠标移动事件 126 | void mousePressEvent(QMouseEvent*); // 鼠标点击事件 127 | 128 | private slots: 129 | // QPushButton *localbnt; // 点击录入本地音乐 130 | void initMysql(); 131 | void initPlayer(); // 用来根据 playf 来改变播放器图标 132 | void initPro(); // 初始化进度条 133 | void showPlayMedia(); // 将 当前播放歌的名称 在label 的 text 中显示 134 | void readmysql(mytablewidget*, QString, QString); // 初始化音乐队列 (仅在开启播放器的调用) 135 | QStringList getfileName(const QString& file); // 将所有的 歌的文件路径都记录到 QstrinList中 136 | void reinit(int); // x 初始化 137 | void boxitem(int i, QString text, QTableWidget*); // 去在音乐列表里(QListWidget) 中实现一个item 138 | void queuefun(mytablewidget*, QString, QString); // 封装 boxitem 139 | QString getPName(QString filename); // 歌手名字 140 | QString getMName(QString filename); // 音乐名字 141 | QString getname(QString); // 获取当前音乐文件的文件名 142 | void showlocal(QListWidgetItem*); // 显示本地音乐 143 | void localinit(QTableWidget*); // 初始化本地音乐列表 144 | void insert_nowplay(QString, QString, QString, QString); // 插入到音乐队列中 145 | void innowplay(); // 将当前播放音乐路径录入 146 | void songqueue_fun(int, int); // songqueue功能 147 | void local_fun(int, int); // local 功能 148 | void reply(QNetworkReply*); // 如果net_messager finish 判断是否载入 然后进行js 149 | void search(QString, int); // 连接http 搜索歌曲 150 | void parseJson(QString, int); // 将歌曲放入 tableWidget 151 | void reply2(QNetworkReply *reply); // 如果net_messageer finish 判断是否载入 然后进行js 152 | void parseJson2(QString json); //播放音乐解析Json 153 | void reply3(QNetworkReply *); 154 | void local_img(QString); // 同上 只不过是本地的音乐图片 处理 155 | void play_net_Music(int, int); //点击歌曲tableWidget item 播放音乐 156 | bool is_net_music(QString); // 判断是不是 net_music 157 | void initlrc_win(); // 初始化歌词播放窗口 158 | void buildlrc(QString); 159 | QPixmap PixmapToRound(QPixmap&, int); // 将图片变成圆角 160 | QString is_like(QString); // 判断是不是喜欢音乐 161 | void fun_like(QString, int); // like_w的显示 162 | void fun_like_w(int, int); // 关于like_w的点击 163 | void init_local(); // 本地init 164 | void init_like(); // 喜爱init 165 | void songlist_add(QString); // 添加 自定义歌单 166 | void initsonglist(); //初始化歌单 167 | void show_List_music(QListWidgetItem *); // 展示自定义歌单音乐 168 | void this_songlist(QListWidgetItem *); // 获得 点击的歌单 并存储歌曲 169 | void show_list_music(QListWidgetItem *); // 显示 该自定义歌单的歌曲 170 | void fun_list_w(int, int); // 关于list_w 的点击 171 | 172 | 173 | public slots: 174 | void updatepos(); // 更新 播放时间 175 | 176 | signals: 177 | void beginplay(); // 开始重新构造 进度条 178 | void file_null(); // 当mfile为空时 触发 179 | void is_sign_in(); //当登录成功 更新列表 180 | 181 | private: 182 | int X; // 进度条总长度 防止出现越界情况 183 | int V; //音量总长度 防止出现越界情况 184 | qint64 druntime;//总进度 185 | qint64 positontime = 0;//当前进度 186 | QTimer *time; //刷新 187 | 188 | void paintEvent(QPaintEvent* event); // 绘制 进度条 189 | Ui::MainWindow *ui; 190 | }; 191 | #endif // MAINWINDOW_H 192 | -------------------------------------------------------------------------------- /src/mainwindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MainWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 853 10 | 433 11 | 12 | 13 | 14 | MainWindow 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/mybtn.cpp: -------------------------------------------------------------------------------- 1 | #include "mybtn.h" 2 | #include 3 | #include 4 | mybtn::mybtn(const QString a, const QString b){ 5 | this ->enter = a; 6 | this ->back = b; // 录入enter back 7 | QPixmap ap; //录入第一张图片 8 | bool ok = ap .load(enter); 9 | if(!ok) { 10 | QMessageBox::information(this, "Warning", "录入图片错误!", QMessageBox::Ok); 11 | return; 12 | } // 如果录入失败 那么 停止程序 13 | 14 | this -> setIcon(ap); //设置图标 15 | this ->setFixedSize(ap.width(), ap.height()); //设置按钮大小 16 | this ->setStyleSheet("QPushButton{border:0px;}"); // 设置按钮风格 17 | this ->setIconSize(QSize(ap.width(), ap.height())); // 设置图标大小 18 | } 19 | 20 | void mybtn::mousePressEvent(QMouseEvent * e) { 21 | QPixmap bp; // 开始录入 被点击时的图片 22 | bool ok = bp.load(back); 23 | if(ok == false) { 24 | QMessageBox::information(this, "Warning", "录入图片错误!", QMessageBox::Ok); 25 | return ; 26 | } 27 | 28 | this ->setIcon(bp); 29 | QPushButton::mousePressEvent(e); 30 | } // 点击事件转换为新的图片 31 | 32 | void mybtn::mouseReleaseEvent(QMouseEvent *e) { 33 | QPixmap ap; 34 | ap.load(enter); 35 | this ->setIcon(ap); 36 | QPushButton::mouseReleaseEvent(e); 37 | } // 重新录入一开始的图片 38 | -------------------------------------------------------------------------------- /src/mybtn.h: -------------------------------------------------------------------------------- 1 | #ifndef MYBTN_H 2 | #define MYBTN_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | class mybtn : public QPushButton 9 | { 10 | Q_OBJECT 11 | public: 12 | // explicit mybtn(QObject *parent = nullptr); 13 | QString enter, back; // 点击状态显示的两种图片 enter back 14 | mybtn(const QString a, const QString b = ""); // 重新写 mybtn的构造函数 15 | 16 | void mousePressEvent(QMouseEvent*); // 当点击按钮时变换图片 17 | void mouseReleaseEvent(QMouseEvent*); // 当松开按钮图片返回到 enter 状态 18 | 19 | signals: 20 | 21 | }; 22 | 23 | #endif // MYBTN_H 24 | -------------------------------------------------------------------------------- /src/mylab.cpp: -------------------------------------------------------------------------------- 1 | #include "mylab.h" 2 | #include 3 | mylab::mylab(QString text, QWidget *parent) : QLabel(parent) 4 | { 5 | this ->setStyleSheet("QLabel{color: rgb(128, 128, 128); padding-left: 30px;}"); // 改变label 的风格 6 | this ->setText(text); // 内容 7 | QFont font; // 修改字体 8 | font.setFamily("幼圆"); 9 | this ->setFont(font); 10 | } 11 | -------------------------------------------------------------------------------- /src/mylab.h: -------------------------------------------------------------------------------- 1 | #ifndef MYLAB_H 2 | #define MYLAB_H 3 | 4 | #include 5 | #include 6 | class mylab : public QLabel 7 | { 8 | Q_OBJECT 9 | public: 10 | explicit mylab(QString text, QWidget *parent = nullptr); 11 | 12 | signals: 13 | 14 | }; 15 | 16 | #endif // MYLAB_H 17 | -------------------------------------------------------------------------------- /src/mylistwidget.cpp: -------------------------------------------------------------------------------- 1 | #include "mylistwidget.h" 2 | 3 | mylistwidget::mylistwidget(QWidget *parent) : QListWidget(parent) 4 | { 5 | // 关于 mylistwidget的 风格 (qss) 6 | this -> setStyleSheet("QListWidget{color:rgb(0, 0, 0); background:rgb(255, 255, 255);border:0px solid black;}" 7 | "QListWidget::Item{height:40px; border:0px solid white;padding-left:15px; border: 3px solid white; margin-left: 10px}" 8 | "QListWidget::Item:hover{color:rgb(0, 0, 0);background: rgb(228, 228, 228); border: 0px solid gray; border: 3px solid white;}" 9 | "QListWidget::Item:selected{color:rgb(0, 0, 0); background: rgb(228, 228, 228); border:1px solid gray; border: 3px solid white;}"); 10 | this ->setFocusPolicy(Qt::NoFocus); 11 | } 12 | -------------------------------------------------------------------------------- /src/mylistwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYLISTWIDGET_H 2 | #define MYLISTWIDGET_H 3 | 4 | #include 5 | #include 6 | class mylistwidget : public QListWidget 7 | { 8 | Q_OBJECT 9 | public: 10 | explicit mylistwidget(QWidget *parent = nullptr); 11 | 12 | signals: 13 | 14 | }; 15 | 16 | #endif // MYLISTWIDGET_H 17 | -------------------------------------------------------------------------------- /src/mytablewidget.cpp: -------------------------------------------------------------------------------- 1 | #include "mytablewidget.h" 2 | #include 3 | #include 4 | 5 | 6 | mytablewidget::mytablewidget(QWidget *parent) : QTableWidget(parent) 7 | { 8 | this ->setEditTriggers(QAbstractItemView::NoEditTriggers); // 不可编辑 9 | this ->setShowGrid(false); // 关闭网格 10 | this ->setFocusPolicy(Qt::NoFocus); //去掉虚线格 11 | this ->verticalHeader() ->setHidden(true); // 去掉表头行号 12 | 13 | //设置 列宽 和 列count 14 | this ->setColumnCount(5); 15 | 16 | 17 | this ->setColumnWidth(0, 40); // 爱心 18 | this ->setColumnWidth(1, 100); // 名字 19 | this ->setColumnWidth(2, 30); // 播放 20 | this ->setColumnWidth(3, 30); // 删除 21 | this ->setColumnWidth(4, 30); // 增添 22 | this ->setMouseTracking(true); //开启捕获鼠标功能 23 | this ->setSelectionBehavior(QAbstractItemView::SelectRows); //设置整行选择 24 | this ->setStyleSheet("QTableWidget::Item::selected{background: rgb(228, 228, 228);}"); 25 | connect(this, &QTableWidget::cellEntered, this, &mytablewidget::changeColor); // 当鼠标hover itme 这一行 使一整行 都变色 26 | } 27 | 28 | 29 | void mytablewidget::changeColor(int row, int col) { 30 | QTableWidgetItem *item = this ->item(pre_row, 0); 31 | if (item) { 32 | this ->hover_row_color(pre_row, QColor(255, 255, 255)); 33 | } 34 | 35 | this ->hover_row_color(row, QColor(228, 228, 228)); 36 | pre_row = row; 37 | return; 38 | } 39 | 40 | 41 | void mytablewidget::hover_row_color(int row, QColor color) { 42 | for(int i = 0; i < 5; ++i) { 43 | this ->item(row, i) ->setBackgroundColor(color); 44 | } 45 | return; 46 | } 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /src/mytablewidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYTABLEWIDGET_H 2 | #define MYTABLEWIDGET_H 3 | 4 | #include 5 | #include 6 | class mytablewidget : public QTableWidget 7 | { 8 | Q_OBJECT 9 | public: 10 | explicit mytablewidget(QWidget *parent = nullptr); 11 | void hover_row_color(int, QColor); // 改变相应行的颜色 12 | void changeColor(int, int); // 获取需要改变颜色的 item 的行数 13 | 14 | int pre_row; // 上一行的颜色 15 | 16 | }; 17 | 18 | #endif // MYTABLEWIDGET_H 19 | -------------------------------------------------------------------------------- /src/mywidget.cpp: -------------------------------------------------------------------------------- 1 | #include "mywidget.h" 2 | #include 3 | 4 | mywidget::mywidget(QWidget *parent) : QWidget(parent) 5 | { 6 | l = new QLabel(this); 7 | l ->setGeometry(50, 50, 400, 400); 8 | l ->setStyleSheet("broder-radius: 30px;}"); 9 | } 10 | -------------------------------------------------------------------------------- /src/mywidget.h: -------------------------------------------------------------------------------- 1 | #ifndef MYWIDGET_H 2 | #define MYWIDGET_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | class mywidget : public QWidget 10 | { 11 | Q_OBJECT 12 | public: 13 | QLabel *l; // 用来绘制歌曲专辑图片 14 | explicit mywidget(QWidget *parent = nullptr); 15 | 16 | signals: 17 | 18 | }; 19 | 20 | #endif // MYWIDGET_H 21 | -------------------------------------------------------------------------------- /src/res.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | coin/cls.png 4 | coin/cls_1.png 5 | coin/max.png 6 | coin/max_1.png 7 | coin/min.png 8 | coin/min_1.png 9 | coin/pause.png 10 | coin/pause_c.png 11 | coin/start.png 12 | coin/start_c.png 13 | coin/prev.png 14 | coin/prev_c.png 15 | coin/next.png 16 | coin/next_c.png 17 | coin/unvol.png 18 | coin/vol.png 19 | coin/unvol_c.png 20 | coin/vol_c.png 21 | coin/list.png 22 | coin/list_c.png 23 | coin/delete.png 24 | coin/delete_c.png 25 | coin/begin.png 26 | coin/search.png 27 | coin/sign_in.png 28 | coin/sign_in_c.png 29 | coin/local_add.png 30 | coin/local_add_c.png 31 | coin/songer.png 32 | coin/like.png 33 | coin/like_c.png 34 | coin/add.png 35 | coin/add_c.png 36 | coin/looper.png 37 | coin/no_looper.png 38 | coin/lrc_word.png 39 | coin/mymusic.png 40 | 41 | 42 | -------------------------------------------------------------------------------- /src/show_list.cpp: -------------------------------------------------------------------------------- 1 | #include "show_list.h" 2 | 3 | 4 | 5 | show_list::show_list(int id, QWidget *parent) : QWidget(parent), user_id(id) 6 | { 7 | 8 | setWindowFlag(Qt::FramelessWindowHint); // 删除以前的 最大化 最小化 关闭自己写 9 | this ->setFixedSize(200, 350); 10 | 11 | // connect mysql 12 | initMysql(); 13 | 14 | // 显示所有的 歌单 15 | show_song_list(); 16 | 17 | connect(list, &mylistwidget::itemDoubleClicked, this, &show_list::item_show); // 当点击 list 时, 开始返回item 18 | } 19 | 20 | 21 | void show_list::initMysql() { 22 | db = QSqlDatabase::addDatabase("QMYSQL"); // 加载mysql 驱动 23 | db.setHostName("rm-uf6to17061qpnj06goo.mysql.rds.aliyuncs.com"); // 主机名 24 | db.setPort(3306); // 端口 25 | db.setDatabaseName("musicbase"); // 库名 26 | db.setUserName("user"); //用户名 27 | db.setPassword("Tsy20010612"); // 密码 28 | bool ok = db.open(); //打开数据库 29 | if (ok) qDebug() << "connect aliyun ok!" << endl; 30 | } 31 | 32 | 33 | void show_list::show_song_list() { 34 | QFont font; 35 | font.setFamily("幼圆"); 36 | font.setPointSize(12); 37 | 38 | list = new mylistwidget(this); 39 | list ->setFont(font); 40 | list ->setGeometry(0, 0, this ->width(), this ->height()); 41 | QString user_id_s = QString("%0").arg(user_id); 42 | 43 | QSqlQuery sql_l(db); 44 | sql_l.exec("select name from listname where id = " + user_id_s + ";"); 45 | while(sql_l.next()) { 46 | list ->addItem(sql_l.value(0).value()); 47 | } 48 | } 49 | 50 | // 当点击 list 时, 开始返回item 51 | void show_list::item_show(QListWidgetItem *item) { 52 | emit return_item(item); 53 | this ->hide(); 54 | } 55 | -------------------------------------------------------------------------------- /src/show_list.h: -------------------------------------------------------------------------------- 1 | #ifndef SHOW_LIST_H 2 | #define SHOW_LIST_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "sign_in_win.h" 10 | #include "mylistwidget.h" 11 | 12 | class show_list : public QWidget 13 | { 14 | Q_OBJECT 15 | public: 16 | explicit show_list(int id, QWidget *parent = nullptr); 17 | 18 | 19 | int user_id; // 用户id 20 | QSqlDatabase db; // connect sql db 21 | mylistwidget *list; 22 | 23 | 24 | void initMysql(); 25 | void show_song_list(); // 展示 所有的歌单 26 | void item_show(QListWidgetItem *); // 传递item 27 | signals: 28 | void return_item(QListWidgetItem *); 29 | }; 30 | 31 | #endif // SHOW_LIST_H 32 | -------------------------------------------------------------------------------- /src/sign_in_win.cpp: -------------------------------------------------------------------------------- 1 | #include "sign_in_win.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | sign_in_win::sign_in_win(QWidget *parent) : QWidget(parent) 8 | { 9 | this ->setWindowTitle("登录窗口"); 10 | this ->setWindowIcon(QIcon(":/coin/mymusic.png")); // 主窗口的图标设置! 11 | this ->setFixedSize(350, 200); 12 | user_name = new QLineEdit(this); 13 | user_password = new QLineEdit(this); 14 | 15 | user_name ->setGeometry(80, 30, 200, 30); 16 | user_password ->setGeometry(80, 80, 200, 30); 17 | 18 | sign_in_btn = new QPushButton("登录", this); 19 | sign_up_btn = new QPushButton("注册", this); 20 | sign_in_btn ->setGeometry(80, 120, 90, 30); 21 | sign_up_btn ->setGeometry(190, 120, 90, 30); 22 | 23 | user_name_l = new QLabel("用户名" ,this); 24 | user_password_l = new QLabel("密码", this); 25 | 26 | user_name_l ->setGeometry(30, 35, 40, 20); 27 | user_password_l ->setGeometry(35, 85, 40, 20); 28 | 29 | connect(sign_in_btn, &QPushButton::clicked, this, &sign_in_win::user_sign_in); 30 | connect(sign_up_btn, &QPushButton::clicked, this, [=] () { 31 | login_win = new sign_up_win(); 32 | login_win ->show(); 33 | }); 34 | 35 | initMysql(); 36 | } 37 | 38 | void sign_in_win::user_sign_in() { 39 | QSqlQuery selectq = QSqlQuery(db); 40 | selectq.exec("select * from user"); 41 | while(selectq.next()) { 42 | QString name = selectq.value(1).value(); 43 | QString password = selectq.value(2).value(); 44 | if(name == user_name ->text() && password == user_password ->text()) { 45 | this ->user_id = selectq.value(0).value(); 46 | this ->song_list_cnt = selectq.value(3).value(); 47 | break; 48 | } 49 | } 50 | if(user_id == -1) QMessageBox::information(this, "提示", "登录失败", QMessageBox::Ok); 51 | else { 52 | QMessageBox::information(this, "提示", "登录成功", QMessageBox::Ok); 53 | emit sign_in_success(); 54 | this ->hide(); 55 | user_id = -1; 56 | } 57 | } 58 | 59 | void sign_in_win::initMysql() { 60 | db = QSqlDatabase::addDatabase("QMYSQL"); // 加载mysql 驱动 61 | db.setHostName("rm-uf6to17061qpnj06goo.mysql.rds.aliyuncs.com"); // 主机名 62 | db.setPort(3306); // 端口 63 | db.setDatabaseName("musicbase"); // 库名 64 | db.setUserName("user"); //用户名 65 | db.setPassword("Tsy20010612"); // 密码 66 | bool ok = db.open(); //打开数据库 67 | if (ok) qDebug() << "connect aliyun ok!" << endl; 68 | } 69 | -------------------------------------------------------------------------------- /src/sign_in_win.h: -------------------------------------------------------------------------------- 1 | #ifndef SIGN_IN_WIN_H 2 | #define SIGN_IN_WIN_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include "sign_up_win.h" 12 | 13 | class sign_in_win : public QWidget 14 | { 15 | Q_OBJECT 16 | public: 17 | explicit sign_in_win(QWidget *parent = nullptr); 18 | 19 | QSqlDatabase db; // mysql 20 | int user_id = -1; // 用户登录的id 21 | int song_list_cnt = 0; // 用户歌单号 或者是 歌单数量 22 | QLabel *user_name_l, *user_password_l; 23 | QLineEdit *user_name, *user_password; 24 | QPushButton *sign_in_btn, *sign_up_btn; // 登录 和 注册 界面 25 | 26 | sign_up_win* login_win = nullptr; // 注册界面 27 | 28 | void initMysql(); // 连接mysql 29 | void user_sign_in(); // 登录响应 30 | 31 | signals: 32 | void sign_in_success(); //登录成功触发信号 33 | }; 34 | 35 | #endif // SIGN_IN_WIN_H 36 | -------------------------------------------------------------------------------- /src/sign_up_win.cpp: -------------------------------------------------------------------------------- 1 | #include "sign_up_win.h" 2 | #include 3 | #include 4 | #include 5 | sign_up_win::sign_up_win(QWidget *parent) : QWidget(parent) 6 | { 7 | // 初始化 8 | _initMysql(); 9 | 10 | this ->setWindowTitle("注册窗口"); 11 | this ->setWindowIcon(QIcon(":/coin/mymusic.png")); // 主窗口的图标设置! 12 | 13 | 14 | this ->setFixedSize(350, 200); 15 | login_user_name = new QLineEdit(this); 16 | login_user_password = new QLineEdit(this); 17 | 18 | login_user_name ->setGeometry(80, 30, 200, 30); 19 | login_user_password ->setGeometry(80, 80, 200, 30); 20 | 21 | ok = new QPushButton("确定", this); 22 | cancel = new QPushButton("取消", this); 23 | ok ->setGeometry(80, 120, 90, 30); 24 | cancel ->setGeometry(190, 120, 90, 30); 25 | 26 | user_name_l = new QLabel("用户名" ,this); 27 | user_password_l = new QLabel("密码", this); 28 | 29 | user_name_l ->setGeometry(30, 35, 40, 20); 30 | user_password_l ->setGeometry(35, 85, 40, 20); 31 | 32 | connect(ok, &QPushButton::clicked, this, &sign_up_win::user_sign_up); 33 | connect(cancel, &QPushButton::clicked, this, [=] () { 34 | this ->hide(); 35 | }); 36 | } 37 | 38 | // 注册响应 39 | void sign_up_win::user_sign_up() { 40 | QSqlQuery selectq = QSqlQuery(db); 41 | selectq.exec("select count(*) from user"); 42 | selectq.next(); 43 | QString count = QString("%1").arg(selectq.value(0).value()); 44 | 45 | QString _user_name = login_user_name ->text(); 46 | QString _user_password = login_user_password ->text(); 47 | QString cnt_list = QString("%0").arg(0); 48 | QString exec = QString("insert into user values(" + count + ",\"" + _user_name + "\"," + "\"" + _user_password + "\", " + cnt_list + ");"); 49 | 50 | bool ok = selectq.exec(exec); 51 | if(ok) QMessageBox::information(this, "提示", "注册成功!", QMessageBox::Ok); 52 | else QMessageBox::information(this, "提示", "注册失败(可能是用户名重复)!", QMessageBox::Ok); 53 | this ->hide(); 54 | } 55 | 56 | void sign_up_win::_initMysql() { 57 | db = QSqlDatabase::addDatabase("QMYSQL"); // 加载mysql 驱动 58 | db.setHostName("rm-uf6to17061qpnj06goo.mysql.rds.aliyuncs.com"); // 主机名 59 | db.setPort(3306); // 端口 60 | db.setDatabaseName("musicbase"); // 库名 61 | db.setUserName("user"); //用户名 62 | db.setPassword("Tsy20010612"); // 密码 63 | bool ok = db.open(); //打开数据库 64 | if (ok) qDebug() << "connect aliyun ok!" << endl; 65 | } 66 | -------------------------------------------------------------------------------- /src/sign_up_win.h: -------------------------------------------------------------------------------- 1 | #ifndef SIGN_UP_WIN_H 2 | #define SIGN_UP_WIN_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | class sign_up_win : public QWidget 12 | { 13 | Q_OBJECT 14 | public: 15 | explicit sign_up_win(QWidget *parent = nullptr); 16 | 17 | 18 | QSqlDatabase db; // mysql 19 | QLabel *user_name_l, *user_password_l; 20 | QLineEdit *login_user_name, *login_user_password; 21 | QPushButton *ok, *cancel; 22 | 23 | void _initMysql(); 24 | void user_sign_up(); // 注册响应 25 | 26 | signals: 27 | 28 | }; 29 | 30 | #endif // SIGN_UP_WIN_H 31 | -------------------------------------------------------------------------------- /src/startbtn.cpp: -------------------------------------------------------------------------------- 1 | #include "startbtn.h" 2 | #include 3 | #include 4 | startbtn::startbtn(const QString a, const QString b, const QString c, const QString d) { 5 | // 关于 enter, enter_c, back, back_c 查看startbtn.h 6 | this ->enter = a; 7 | this ->enter_c = b; 8 | this ->back = c; 9 | this ->back_c = d; 10 | 11 | QPixmap ep; // 载入图标1 12 | bool ok = ep.load(this ->enter); 13 | if (!ok){ 14 | QMessageBox::information(this, "Warning", "图片为载入!", QMessageBox::Ok); 15 | return; 16 | } 17 | 18 | // 将图片载入到按钮中 19 | this ->setIcon(ep); 20 | 21 | // 设置图标大小 22 | this -> setIconSize(QSize(ep.width(), ep.height())); 23 | 24 | // 设置图标大小 25 | this ->setFixedSize(ep.width(), ep.height()); 26 | 27 | //设置按钮风格 28 | this ->setStyleSheet("QPushButton{border: 0px;}"); 29 | 30 | } 31 | 32 | 33 | // 点击事件 34 | void startbtn::mousePressEvent(QMouseEvent * ev) { 35 | QPixmap p_c; // 载入当鼠标按下时 的 图标 36 | if(!is_play) { 37 | p_c.load(this ->enter_c); 38 | this ->setIcon(QIcon(p_c)); 39 | 40 | } else { 41 | p_c.load(this ->back_c); 42 | this ->setIcon(QIcon(p_c)); 43 | } 44 | 45 | QPushButton::mousePressEvent(ev); 46 | } 47 | 48 | 49 | // 鼠标松开事件 50 | void startbtn::mouseReleaseEvent(QMouseEvent *ev) { 51 | QPixmap p; 52 | if(!is_play) { 53 | p.load(this ->back); 54 | 55 | this ->setIcon(QIcon(p)); 56 | is_play = true; // 播放器现在处于暂停状态 57 | } else { 58 | p.load(this ->enter); 59 | 60 | this ->setIcon(QIcon(p)); 61 | is_play = false; // 播放器现在处于播放状态 62 | } 63 | QPushButton::mouseReleaseEvent(ev); 64 | } 65 | -------------------------------------------------------------------------------- /src/startbtn.h: -------------------------------------------------------------------------------- 1 | #ifndef STARTBTN_H 2 | #define STARTBTN_H 3 | 4 | #include 5 | #include 6 | #include 7 | class startbtn : public QPushButton 8 | { 9 | Q_OBJECT 10 | public: 11 | // explicit startbtn(QPushButton *parent = nullptr); 12 | explicit startbtn(const QString a, const QString b, const QString c, const QString d); // startbtn 构造函数 13 | QString enter, enter_c, back, back_c; // enter 初始图标, enter_c 当鼠标点击btn上时 改变其图标, back 当鼠标放开时改变图标, back_c 当鼠标点击btn上时改变 14 | bool is_play = false; //当 is_play = false时, 为播放图标, is_play = true 时为暂停图标 15 | 16 | void mousePressEvent(QMouseEvent*); // 点击事件 17 | void mouseReleaseEvent(QMouseEvent*); // 鼠标松开事件 18 | signals: 19 | 20 | }; 21 | 22 | #endif // STARTBTN_H 23 | --------------------------------------------------------------------------------