简体   繁体   中英

Using QSlider in window to change background volume

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM