0
votes

I have a MainWindow and then a SecondWindow class that opens when a button is clicked in MainWindow. There is a slider in this SecondWindow class I want to control the music in both the MainWindow and SecondWindow classes.

my main function:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    MainWindow w;
    w.show();

    SecondWindow s;

    //this connects MainWindow to SecondWindow to open
    QObject::connect(...);

    //plays background music
    Music m;
    QObject::connect(&s, SIGNAL(Volume(int)), &m, SLOT(setVol(int)));


    return a.exec();
}

I have a music object that plays a function defined as such:

class Music
{
public:
    Music();

public slots:
    void setVol(int value);
private:
    QMediaPlayer* music;
};

my SecondWindow is defined as such:

class SecondWindow: public QMainWindow
{
    Q_OBJECT
public:
    explicit SecondWindo(QWidget *parent = nullptr);
    ~SecondWindow();
signals:
    void VolumeChange(int value);
    void Volume(int value);

public slots:
    void ShowSettingsWindow();
    void changeVolume(int value);

private:
    void Connections();

    int volume; //holds the volume value based on the music_slider
    QSlider* music_slider;


};

At the bottom of my SecondWindow's default constructor I have the following connect statement, with the SLOT definition:

QObject::connect(music_slider, SIGNAL(valueChanged(int)), this, SLOT(VolumeChange(int)));

void SecondWindow::VolumeChange(int value){
    emit Volume(value);
}

then music has the SLOT defined as such:

void Music::setVol(int value){
    music->setVolume(value);
}

I am currently trying to make it so that everytime the slider changes value, VolumeChange is called for the value that the slider currently has. Then, the signal Volume is called causing music to call the function setVol and thus set the volume. But I recieve an error on the second connect statement in main saying it could not convert all argument types. How an I fix these connect statements to work or is there a better way to do this?

1
add Q_OBJECT before public: in Music headereyllanesc
this did not work and I recieved the same errors, I tried deriving it from QObject by writing class Music : public QObject but then recieved errors that my Settings::VolumeChange(int) already defined in Settings.objnutella100
Please edit your code to use the new signal/slot syntax and include any error messages verbatim.G.M.

1 Answers

0
votes

According to your code, this connect QObject::connect(music_slider, SIGNAL(valueChanged(int)), this, SLOT(VolumeChange(int))); in the second window is invalid because it doesn't have a SLOT called VolumeChange. In fact, it's a SIGNAL. However, it has a SLOT called changeVolume, which I think you really mean.