3

I'm trying to play a sound from a QSound object. I can't see why it's not working, looking at previous example

This is the header

#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

This is the main

#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();
}

I'm getting the following errors.

accesspanel.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: void _thiscall QSound::play(void)" (_imp_?play@QSound@@QAEXXZ) referenced in function "private: void __thiscall accessPanel::on_one_clicked(void)" (?on_one_clicked@accessPanel@@AAEXXZ)

accesspanel.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QSound::QSound(class QString const &,class QObject *)" (_imp??0QSound@@QAE@ABVQString@@PAVQObject@@@Z) referenced in function "public: __thiscall accessPanel::accessPanel(class QWidget *)" (??0accessPanel@@QAE@PAVQWidget@@@Z)

accesspanel.obj:-1: error: LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall QSound::metaObject(void)const " (?metaObject@QSound@@UBEPBUQMetaObject@@XZ)

And 3 others similar to it.

I've tried the following

QSound::play("filename"); - same issues

I messed around with a static reference but got something like

Barney
  • 2,355
  • 3
  • 22
  • 37
Chris O'Brien
  • 372
  • 6
  • 25
  • possible duplicate of [Q\_OBJECT linker error!](http://stackoverflow.com/questions/3264259/q-object-linker-error) – BЈовић Feb 25 '13 at 19:04
  • Tried most of the stuff in that link prior to post (QMake, clean, restart QT, restart PC etc), but I'll go through it again. – Chris O'Brien Feb 25 '13 at 19:22
  • I've deleted the build folder, recompiled, run qmake, rebuilt and most combinations. Error persists. – Chris O'Brien Feb 25 '13 at 19:51
  • Can you post an example contained in one file? You should be able to narrow it down to just a QSound example. – Mitch Feb 25 '13 at 20:57
  • Sure. If I remove all QSound references in the header/source and replace the method with the below, I still get errors. void accessPanel::on_one_clicked() { // keypad->play(); QSound::play("../Island/Sounds/keypad.wav"); input = input + "1"; ui->codeDisplay->setText(input); } – Chris O'Brien Feb 25 '13 at 21:25
  • Ugh, that looks messy. Is there no way to format code in a reply? – Chris O'Brien Feb 25 '13 at 21:25
  • Error added here. accesspanel.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: static void __cdecl QSound::play(class QString const &)" (__imp_?play@QSound@@SAXABVQString@@@Z) referenced in function "private: void __thiscall accessPanel::on_one_clicked(void)" (?on_one_clicked@accessPanel@@AAEXXZ) // Second Error // release\Island.exe:-1: error: LNK1120: 1 unresolved externals – Chris O'Brien Feb 25 '13 at 21:26

3 Answers3

3

The problem was my .pro file. I needed to add QT += multimedia to my project .pro file.

Chris O'Brien
  • 372
  • 6
  • 25
0

I ran into the same problem. Instead of creating a pointer I create an empty variable in the header:

private:
      QSound m_startsound; 

I assign the value with explicitly mentioning it parent object (thanks for reminding me of that):

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

You could also do this in the cpp file. In my oppinion less structured solution:

include

... QSound m_startsound("mysounds/hallo.wav",this);

However, these solutions could solve loading time in playing a static sound several times. It does not make the sounds changeable on a certain trigger. This requires your pointer structure: in header QPointer p_startsound;

on init or in the constructor set the default value:

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

on change trigger be sure to remove the old object to prevent memory leaks

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

If computing time is of no essence the simple play function can be used

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

BTW Somehow I don't need to include the multimedia package explicitly for any of these. Perhaps because I use a diferent version of QT or because it is inherited somewhere else in the platform.

EDIT Use a protected QPointer instead otherwise it will crash...

Roberdus
  • 1
  • 1
0

If you are using QT5 Please Add Qt5Multimedia.lib in the Additional dependencies at
Project -> Properties->Configuration Propeties -> Linker -> Input -> Additional Dependencies

Saman
  • 71
  • 1
  • 3