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. Connecting slider from one class to method in another

Connecting slider from one class to method in another

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 286 Views
  • 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.
  • J Offline
    J Offline
    John Green
    wrote on last edited by
    #1

    I am trying to connect a slider I have in my Window class (A) which contains a widget of my other class (B). I am struggling to understand how I can make it so if the slider is changed in A, I can call a function in class B that does something like this:

    A::A(){
    //constructor
    //create window
    ...
    Slider = new QSlider(Qt::Horizontal);
    window->addWidget(slider);
    //connect slider to slot from B sending slider value
    }

    B::doSomething(int value){
    //do something with value
    }

    How can I do this, which class would I need to include the slot in, would I need to call it on an instance etc

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      B::soSomething shall be the slot.
      The connection happens between two instances not two classes.

      See the Signals and Slot chapter of Qt's documentation.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • J Offline
        J Offline
        John Green
        wrote on last edited by
        #3

        @John-Green said in Connecting slider from one class to method in another:

        Okay so I added the slot to B but it doesn't work, I added the Q_OBJECT macro but it can't find ClassB::doSomething because it tries to find ClassA::ClassB::doSomething. What do I need to change?
        Inside A's constructor
        QObject::connect(Slider, SIGNAL(valueChanged(int)), this, SLOT(ClassB::doSomething(int)));

        Inside Bs header file:
        Public Slots:
        void doSomething(int)

        Inside Bs cpp file:
        void B::doSomething(int number){
        //do something with number
        }

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          The SLOT macro expects only the name of the slot. No class before.

          You should move to the new connection syntax. That will make the compilation fail if you don't create proper connection.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1

          • Login

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