Extend a QT QRadioButton Class to attach QSound::Play to a private slot

244 views Asked by At

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! :)

1

There are 1 answers

3
Tyler Jandreau On BEST ANSWER

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.