How to pass QSlider object pointer to another function



  • I have 2 source files and 1 header file:

    • main.cpp
    • slider.cpp
    • slider.h

    I have created my own Slider class and placed it inside the slider.cpp.
    A QSlider object will be created whenever my own Slider object is created.
    A QLCDNumber object is created in main.cpp.

    Now, I would like to connect those 2 widgets with QObject::connect(); (signals and slots)
    However the LCD doesn't refresh but it builds and runs successfully.

    Is it because I've put my own Slider object (slider) inside QObject::connect(...) ?
    Inside main.cpp:

    QObject::connect(slider, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)));
    

    If yes, how can I get the QSlider object (newSlider) from slider.cpp to main.cpp ?

    main.cpp:

    #include <QtWidgets>
    #include "slider.h"
    
    class QSlider;
    
    int main(int argc, char *argv[]) {
        QApplication app(argc, argv);
    
        // Create a new Slider object
        Slider *slider = new Slider(Qt::Horizontal);
        slider->setRange(-100, 100);
    
        // Create new QLCD Object
        QLCDNumber *lcd = new QLCDNumber;
        lcd->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
    
        // Sync slider and lcd
        QObject::connect(slider, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)));
    
        // Set layout
        QWidget *top = new QWidget;
        QVBoxLayout *layout = new QVBoxLayout(top);
        layout->addWidget(slider);
        layout->addWidget(lcd);
    
        top->show();
        return app.exec();
    }
    

    slider.cpp:

    #include <QtWidgets>
    
    class QSlider;
    
    class Slider : public QWidget {
    public:
        Slider(Qt::Orientation orient = Qt::Horizontal, QWidget *parent = 0);
        void setRange(int min, int max);
    private:
        QSlider *newSlider;
    };
    

    slider.h:

    #include "slider.h"
    
    // Constructor
    Slider::Slider(Qt::Orientation orient, QWidget *parent) : QWidget(parent) {
      newSlider = new QSlider(orient);
      QVBoxLayout *layout = new QVBoxLayout;
      layout->addWidget(newSlider);
      setLayout(layout);
    }
    
    void Slider::setRange(int min, int max) {
      slider->setRange(min, max);
    }
    

  • Moderators

    Your Slider class does not have valueChanged() signal. In fact, it does not have any signals or slots.

    If you want to reuse QSlider functions, then inherit from QSlider instead of QWidget:

    class Slider : public QSlider {
    Q_OBJECT
    [...]
    

    Don't forget about that Q_OBJECT macro, it is important.

    Also, do not include whole QtWidgets module - it will slow down your compilation time a lot. Only #include what you really need: QSlider, QLCDNumber and QApplication.



  • Hi,

    As @sierdzio said, your slider class does not have valueChanged(int) signal, if you want to communicate with each other so then declare one signal valueChanged(int) and emit that signal whenever Qslider valueChanged(int) signal emits.

    Try this,

    // in slider.h

    //add   Q_OBJECT macro 
    Signals:
    void  valueChanged(int);
    

    //change slider.cpp constructor

    Slider::Slider(Qt::Orientation orient, QWidget *parent) : QWidget(parent) {
      newSlider = new QSlider(orient);
      QVBoxLayout *layout = new QVBoxLayout;
      layout->addWidget(newSlider);
      setLayout(layout);
    connect(newSlider,SIGNAL( valueChanged(int)),this,SIGNAL( valueChanged(int)));
    }
    

  • Moderators

    BTW. I also recommend using the new syntax for connections: it would have given you a compilation error - so you would know immediately that your signal is missing.

    connect(slider, &Slider::valueChanged, lcd, &QLCDNumber::display);
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.