好的,你可以将 MainWindow
更名为其他不冲突的类名,例如 MyWindow
。以下是修改后的代码示例:
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QFileDialog>
#include <QSoundEffect>
class MyWindow : public QWidget {
public:
MyWindow(QWidget *parent = nullptr) : QWidget(parent) {
QPushButton *openButton = new QPushButton("Open File", this);
connect(openButton, &QPushButton::clicked, this, &MyWindow::openFile);
QPushButton *closeButton = new QPushButton("Close", this);
connect(closeButton, &QPushButton::clicked, this, &QWidget::close);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(openButton);
layout->addWidget(closeButton);
setLayout(layout);
}
private slots:
void openFile() {
QString filePath = QFileDialog::getOpenFileName(this, "Select WAV File", "", "WAV Files (*.wav)");
if (!filePath.isEmpty()) {
playAudio(filePath);
}
}
void playAudio(const QString& filePath) {
QSoundEffect *effect = new QSoundEffect;
effect->setSource(QUrl::fromLocalFile(filePath));
effect->setVolume(0.5); // 设置音量,范围为0-1
effect->play();
}
};
int main(int argc, char **argv) {
QApplication app(argc, argv);
MyWindow myWindow;
myWindow.show();
return app.exec();
}
在这个示例中,我将 MainWindow
更名为 MyWindow
,并且相应地更新了类名和对象名称。同时,保留了 “Close” 按钮并连接到 close()
槽函数的逻辑,以实现窗口关闭功能。
内容由零声教学AI助手提供,问题来源于学员提问