我正试图从QSound对象播放声音 . 我看不出为什么它不起作用,看看前面的例子
这是 Headers
#ifndef ACCESSPANEL_H
#define ACCESSPANEL_H
#include <QWidget>
#include "theislandmain.h"
#include <QString>
#include <QtMultimedia/QSound>
namespace Ui {
class accessPanel;
}
class accessPanel : public QWidget
{
Q_OBJECT
public:
explicit accessPanel(QWidget *parent = 0);
~accessPanel();
private slots:
QString getCode();
void on_one_clicked();
void on_two_clicked();
void on_three_clicked();
void on_four_clicked();
void on_five_clicked();
void on_six_clicked();
void on_seven_clicked();
void on_eight_clicked();
void on_nine_clicked();
void on_cancel_clicked();
void on_zero_clicked();
void on_confirm_clicked();
private:
Ui::accessPanel *ui;
QString input;
QSound *keypad;
};
#endif // ACCESSPANEL_H
这是主要的
#include "accesspanel.h"
#include "ui_accesspanel.h"
#include <QtMultimedia/QSound>
accessPanel::accessPanel(QWidget *parent) :
QWidget(parent),
ui(new Ui::accessPanel)
{
ui->setupUi(this);
keypad = new QSound("../Island/Sounds/keypad.wav", this);
}
accessPanel::~accessPanel()
{
delete ui;
}
QString accessPanel::getCode()
{
return input;
}
void accessPanel::on_one_clicked()
{
keypad->play();
input = input + "1";
ui->codeDisplay->setText(input);
}
void accessPanel::on_two_clicked()
{
input = input + "2";
ui->codeDisplay->setText(input);
}
void accessPanel::on_three_clicked()
{
input = input + "3";
ui->codeDisplay->setText(input);
}
void accessPanel::on_four_clicked()
{
input = input + "4";
ui->codeDisplay->setText(input);
}
void accessPanel::on_five_clicked()
{
input = input + "5";
ui->codeDisplay->setText(input);
}
void accessPanel::on_six_clicked()
{
input = input + "6";
ui->codeDisplay->setText(input);
}
void accessPanel::on_seven_clicked()
{
input = input + "7";
ui->codeDisplay->setText(input);
}
void accessPanel::on_eight_clicked()
{
input = input + "8";
ui->codeDisplay->setText(input);
}
void accessPanel::on_nine_clicked()
{
input = input + "9";
ui->codeDisplay->setText(input);
}
void accessPanel::on_cancel_clicked()
{
this->close();
input = "";
ui->codeDisplay->clear();
}
void accessPanel::on_zero_clicked()
{
input = input + "0";
ui->codeDisplay->setText(input);
}
void accessPanel::on_confirm_clicked()
{
ui->codeDisplay->setText(input);
TheIslandMain::setInput(input);
this->close();
}
我收到以下错误 .
accesspanel.obj:-1:错误:LNK2019:未解析的外部符号“_declspec(dllimport)public:void thiscall QSound :: play(void)”( imp?play @ QSound @@ QAEXXZ)在函数“private:void __thiscall”中引用accessPanel :: on_one_clicked(void)“(?on_one_clicked @ accessPanel @@ AAEXXZ)accesspanel.obj:-1:错误:LNK2019:未解析的外部符号”declspec(dllimport)public: thiscall QSound :: QSound(class QString const&,class QObject *)“(_imp ?? 0QSound @@ QAE @ ABVQString @@ PAVQObject @@@ Z)在函数”public:__thiscall accessPanel :: accessPanel(class QWidget *)“中引用(?? 0accessPanel @@ QAE @ PAVQWidget @@ @Z)accesspanel.obj:-1:错误:LNK2001:未解析的外部符号“public:virtual struct QMetaObject const * __thiscall QSound :: metaObject(void)const”(?metaObject @ QSound @@ UBEPBUQMetaObject @@ XZ)
还有3个与之相似的人 .
我尝试了以下内容
QSound::play("filename");
- 同样的问题
我用静态参考搞砸了,但得到了类似的东西
2 回答
问题是我的.pro文件 . 我需要将QT =多媒体添加到我的项目.pro文件中 .
我遇到了同样的问题 . 而不是创建指针我在标头中创建一个空变量:
我通过明确提到它的父对象来分配值(感谢提醒我):
您也可以在cpp文件中执行此操作 . 在我的意见不太结构化的解决方案:
包括
... QSound m_startsound(“mysounds / hallo.wav”,this);
但是,这些解决方案可以解决多次播放静音的加载时间问题 . 它不会使声音在某个触发器上发生变化 . 这需要你的指针结构:在 Headers QPointer p_startsound;
在init或构造函数中设置默认值:
在更改触发器时务必删除旧对象以防止内存泄漏
如果计算时间不重要,则可以使用简单的播放功能
BTW不知何故,我不需要为这些中的任何一个明确地包含多媒体包 . 也许是因为我使用了不同版本的QT,或者因为它是在平台的其他地方继承的 .
编辑使用受保护的QPointer,否则会崩溃...