在Qt中播放背景音乐可以通过QSound类实现,具体步骤如下:
- 引入QSound头文件
#include <QSound>
- 在需要播放音乐的地方创建QSound对象并指定音乐文件路径
QSound *bgMusic = new QSound("music.mp3");
- 调用play()函数开始播放音乐
bgMusic->play();
完整的代码示例:
#include <QApplication>
#include <QSound>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSound *bgMusic = new QSound("music.mp3");
bgMusic->play();
return a.exec();
}
注意:QSound只支持播放WAV格式的音频文件,如果要播放其他格式的音频文件,需要使用其他库或工具。