0

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();
}
Ashley
  • 441
  • 1
  • 5
  • 8
  • This looks correct but you did not show your full header. Did you forget the Q_OBJECT macro? – drescherjm Jun 28 '20 at 01:27
  • Yeah, I did, and I modified my question to show that. – Ashley Jun 28 '20 at 02:08
  • 1
    What does "I am not able to connect them" mean? Did you see an error message printed to stdout or stderr when you ran your program? If so, what does it say? – Jeremy Friesner Jun 28 '20 at 02:10
  • You are using the old-style connections: have you tried `QObject::connect(spinBox, QSpinBox::valueChanged,&fre, &Frequency::setVolume);`? –  Jun 28 '20 at 09:21
  • @Jeremy Friesner there is no error message, and after I connect, the graph should change accordingly to different input numbers, which it does not. – Ashley Jun 28 '20 at 17:39
  • @Blayer Bond I can't do it, I am using qt 4.8, which is a fairly old version, and the compiler does not support this function call. – Ashley Jun 28 '20 at 17:40
  • 2
    @Ashley if there is no error message emitted by the `connect()` call, then most likely the connection is being set up correctly, and either the `valueChanged(int)` signal is not being emitted, or (more likely) the `setVolume(int)` method isn't having the expected effect when it is called. The next thing I would do is add a temporary `printf()` call into the `setVolume(int)` method to see if it is actually getting called, or not. – Jeremy Friesner Jun 28 '20 at 18:07
  • @Ashley, I suggest you go through the list of 11 possible errors, given as answer to [this question](https://stackoverflow.com/questions/26422154/my-signal-slot-connection-does-not-work), and let us know if you found the error (and what it is, if yes) or not. –  Jun 28 '20 at 18:58

1 Answers1

1

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;
}
  • I am using an old version of qt, but anyway, solved the problem. thanks. – Ashley Jun 29 '20 at 22:42
  • I used the old syntax, so you could run it ;) Also, what exactly went wrong? My answer isn't really a solution, so it shouldn't be marked, unless the solution is in there. If it's not in there, you could provide a detailed answer to your own question and mark it as a solution. You're welcome! –  Jun 30 '20 at 10:06