Q声音错误-参考

QSound error - reference?

本文关键字:参考 错误 声音      更新时间:2023-10-16

我正在尝试播放来自QSound对象的声音。我不明白为什么它不起作用,看看前面的例子

这是标题

#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(类QStringconst&,类QObject*("(_imp??0Q声音@@QAE@ABVQString@@PAVQObject@@@Z(函数"public:__thiscall accessPanel::accessPanel(类QWidget*)"(??0访问面板@@QAE@PAVQWidget@@@Z(

accesspanel.obj:-1:错误:LNK2001:未解析的外部符号"public:虚拟结构QMetaObject const*__thiscallQSound::metaObject(void(const"(?metaObject@QSound@@UBEPBUQMetaObject@@XZ(

还有3个类似的。

我试过以下

QSound::play("filename");-相同问题

我搞砸了一个静态引用,但得到了类似的东西

问题出在我的.pro文件上。我需要将QT+=多媒体添加到我的项目.pro文件中。

我遇到了同样的问题。我没有创建指针,而是在头中创建了一个空变量:

private:
      QSound m_startsound; 

我通过明确提到它的父对象来分配值(感谢您提醒我(:

BloxProcessor::BloxProcessor() : 
     m_showPopup(true),
     ....
     m_startsound("mysounds/hallo.wav",this)
{
....
}

您也可以在cpp文件中执行此操作。在我的机会较少的结构化解决方案中:

包括

QSound m_startsound("mysounds/hallo.wav",此(;

然而,这些解决方案可以解决多次播放静态声音时的加载时间问题。它不会使特定触发器的声音发生变化。这需要您的指针结构:在页眉中QPointer p_startsound;

在init或构造函数中设置默认值:

p_startsound = new QSound("mysounds/he.wav",this);

在更改触发器上,确保删除旧对象以防止内存泄漏

BloxProcessor::triggerToChangeSoundActivated()
{
    p_startsound->deleteLater();
    p_startsound = new QSound("mysounds/hallo.wav",this);
}

如果计算时间不重要,可以使用的简单播放功能

QSound::play("mysounds/hallo.wav");

BTW不知何故,我不需要为这些明确地包括多媒体包。也许是因为我使用了不同版本的QT,或者是因为它在平台中的其他地方被继承了。

EDIT请使用受保护的QPointer,否则它将崩溃。。。

如果您使用的是QT5请在
的附加依赖项中添加Qt5Multimedia.lib项目->属性->配置属性->链接器->输入->附加依赖项