简体   繁体   中英

How to connect signal and slot with another object in qt --solved

I am doing a qt project, my goal is to draw something based on my input. The signal is in the main, and the drawing is in another object, I am not able to connect them.

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    Frequency fre; //this is the class that does the drawing
    QWidget *window = new QWidget;
    QGridLayout *grid = new QGridLayout;
    
    QGroupBox *groupBox = new QGroupBox(QObject::tr("Volume"));
    QSpinBox *spinBox = new QSpinBox;
    spinBox->setRange(0, 5);
    QObject::connect(spinBox, SIGNAL(valueChanged(int)),&fre, SLOT(setVolume(int))); 
    QVBoxLayout *Vbox = new QVBoxLayout;
    Vbox->addWidget(spinBox);
    groupBox->setLayout(Vbox);
    grid->addWidget(groupBox, 4,7,1,1);

    window->setLayout(grid);
    window->show();
    return app.exec();
 }

This is how I set up the Frequency class:

in h file

class Frequency : public QGLWidget
{
    Q_OBJECT

public slots:
        void setVolume(int value);

in cpp file

void Frequency :: setVolume(int val) {
    vol = val;
    updateGL(); 
}

void Frequency :: paintGL() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    draw();
}

Since I can't put all this in the comment section, I'm going to put it here:

I copied your code and I'm not able to reproduce the error with Qt version 5.14.0 , because when I change the value in any way, the method, void MyObject::setVolume(int value) is being called.

This code should work for you. Possibly, you are running it in release mode, and used a breakpoint to check if it was called (which doesn't always work on release mode).

// #include "QtWidgetsApplication4.h"
#include <QtWidgets/QApplication>
#include "MyObject.h"

#include <QGridLayout>
#include <QGroupBox>
#include <QSpinBox>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    // QtWidgetsApplication4 w;
    // w.show();
    // return a.exec();

    MyObject object;
    QWidget* window = new QWidget;
    QGridLayout* grid = new QGridLayout;

    QGroupBox* groupBox = new QGroupBox(QObject::tr("Volume"));
    QSpinBox* spinBox = new QSpinBox;
    spinBox->setRange(0, 5);
    QObject::connect(spinBox, SIGNAL(valueChanged(int)), &object, SLOT(setVolume(int)));
    QVBoxLayout* Vbox = new QVBoxLayout;
    Vbox->addWidget(spinBox);
    groupBox->setLayout(Vbox);
    grid->addWidget(groupBox, 4, 7, 1, 1);

    window->setLayout(grid);
    window->show();
    return a.exec();
}
#pragma once
#include <QObject>

class MyObject : public QObject
{
    Q_OBJECT

public slots:
    void setVolume(int value);

private:
    int m_volume;
};
#include "MyObject.h"

void MyObject::setVolume(int value)
{
    m_volume = value;
}

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