Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to pass QSlider object pointer to another function
Forum Updated to NodeBB v4.3 + New Features

How to pass QSlider object pointer to another function

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JimmyMarsJ Offline
    JimmyMarsJ Offline
    JimmyMars
    wrote on last edited by
    #1

    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);
    }
    
    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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.

      (Z(:^

      1 Reply Last reply
      5
      • Venkatesh VV Offline
        Venkatesh VV Offline
        Venkatesh V
        wrote on last edited by p3c0
        #3

        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)));
        }
        
        1 Reply Last reply
        2
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          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);
          

          (Z(:^

          1 Reply Last reply
          3

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved