0

I've been playing around with QT5 for Android, I've been struggling a bit with extending an existing class so I can play a sound anytime I click on a radio button that has been promoted.

I'm using the standard QT APP template to get started,

This is what I have so far: radiowclick.h:

#ifndef RADIOWCLICK_H
#define RADIOWCLICK_H
#include <QObject>
#include <QWidget>
#include <qradiobutton.h>
class RadioWClick : public QRadioButton
{
    Q_OBJECT
Public:
    RadioWClick(QWidget *parent = 0);
signals:
    void clicked();
private slots:
    void PrivateClicked();
};
#endif // RADIOWCLICK_H

radiowclick.cpp:

#include "radiowclick.h"
#include <QtMultimedia/qsound.h>
RadioWClick::RadioWClick(QWidget *parent) :
    QRadioButton(parent)
{
    connect(this, SIGNAL(clicked()), this, SLOT(PrivateClicked()));
}

void RadioWClick::PrivateClicked()
{
    QSound::play(":/sounds/ButtonClick.wav");
}

main.cpp: #include "radiowclick.h"...

Everything compiles fine, when I use vanilla sockets and slots I get the clicking sound, but with the promoted radiobuttons and the private slot i have no joy.

Thanks for your help! :)

  • No joy means the slot is called, but the sound is not playing, or something else? Are you putting that sound file into resource? – László Papp Nov 25 '14 at 16:16
  • Whoever voted me down a little feedback would be appreciated. I'd been researching and tried different approaches for hours, I only asked the question as a last resort and this seems to be a question that would be useful to others. –  Nov 26 '14 at 04:03
  • The sound file is in the resource file, as I mentioned in my original post when I use the normal QRadioButton class and include the QSound::play... for each signal i have no problems, only when i try creating a subclass am i having issues. –  Nov 26 '14 at 04:08
  • Did you change the code here after the answer was given? That's not good practice. We can't see what the mistake was. – dev_nut Feb 21 '16 at 17:38

1 Answers1

0

I think what's happening is you're overwriting the connect signal defined in QAbstractButton which is a base of QPushButton.

Try removing your definition of clicked() and connecting your signal PrivateClicked() to the base class clicked() signal.

Some code:

class RadioWClick : public QRadioButton
{
    Q_OBJECT
Public:
    RadioWClick(QWidget *parent = 0);    
private slots:
    void PrivateClicked();
};

And the implementation

RadioWClick::RadioWClick(QWidget *parent) :
    QRadioButton(parent)
{
    connect(this, SIGNAL(clicked()), this, SLOT(PrivateClicked()));
}

void RadioWClick::PrivateClicked()
{
    QSound::play(":/sounds/ButtonClick.wav");
}

NOTE: I have not tested this code. Use at your own risk.

Tyler Jandreau
  • 4,245
  • 1
  • 22
  • 47
  • I would vote you up but i don't have enough reputation. I haven't had a chance to test your solution but it makes a lot of sense. Thanks for taking the time to write a reply and I will let you know how it goes when I get home. If it works you'll be my favorite person of the day! –  Nov 26 '14 at 04:06
  • **Thankyou!! I can't believe i was so close to it all along! thanks again!** –  Nov 26 '14 at 11:17
  • @user2342062 You're welcome. Glad the solution worked for you. – Tyler Jandreau Nov 26 '14 at 15:37