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. [SOLVED] Issues with SIGNALS and SLOTS
Qt 6.11 is out! See what's new in the release blog

[SOLVED] Issues with SIGNALS and SLOTS

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 4.2k Views 1 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hi All,

    I am trying something with SIGNALS and SLOTS:

    Two classes defined:

    1. WeightTracker

    2. Display

    Header files given below

    WeightTracket.h

    @
    #include <QObject>
    #include <QDate>

    class weightTracket : public QObject
    {
    Q_OBJECT
    public:
    explicit weightTracket(QObject *parent = 0);
    void update (QDate d, float w);

    QDate getDate();
    float getFloat();
    

    signals:

    void  changed();
    

    public slots:

    private:

    QDate date;
    float weight;
    

    @

    display.h

    @
    class display : public QObject
    {
    Q_OBJECT
    public:
    explicit display(QObject *parent = 0);

    signals:

    public slots:

    void show(weightTracket * t);
    

    };

    @

    The "show" method in display class
    @
    void display::show(weightTracket * t){

    qDebug() << t->getDate().toString() << t->getFloat();
    

    }

    @

    Main:

    @
    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    weightTracket * tracker = new weightTracket();
    
    QDate d1(2014,04,14);
    
    tracker->update(d1,123);
    
    display * output = new display();
    
    QObject::connect(tracker,SIGNAL(changed()),output,SLOT(show(tracker)));
    
    return a.exec&#40;&#41;;
    

    }

    @

    When I try to run the program I get the message:

    QObject::connect: No such slot display::show(tracker)

    is the output.

    Where am I going wrong?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      thiberi0
      wrote on last edited by
      #2

      I think its is incompatibility of parameters at the connection. Normally, you have to emit at SIGNAL function the same parameters that you are going to use in SLOT function.

      Since the SIGNAL function changed() has no parameters then the SLOT function shouldn't have parameters.

      thiberi0

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Dont think that is the answer. Signals seldom have parameters. I think.

        Anyway how do I link the classes?

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          I updated the show() method for testing as below:

          @
          void display::show(){

          //qDebug() << t->getDate().toString() << t->getFloat();
          
          qDebug() << "In the slot";
          

          }

          @

          I changed the connection to :

          @
          QObject::connect(tracker,SIGNAL(changed()),output,SLOT(show()));

          @

          The message is gone but I am not getting the qDebug() message from the display() slot.

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

            Hi,

            It's normal, show() is a non virtual public method from QWidget, so yours will not be called.

            Many signals throughout the Qt library have parameters.

            See the "Signal and Slot":http://qt-project.org/doc/qt-5/signalsandslots.html chapter, it's explained nicely with examples

            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
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              I have changed the name to show3(). But still does not work.

              Thanks SGaist for the link. Really helpful.

              My connection is still not working.

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

                Do you emit changed anywhere in your code ? If so and it happens before the connect statement then it won't have any effect.

                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
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  On the dot. The connection was after the signal was emitted. Changed it now it works.

                  Thanks. As usual you are a savior.

                  In one class I have a QDate and float that is updated. In the second class (display class) I need to display it to the console.

                  What is the best way to pass that information using SIGNALS and SLOTs?

                  1 Reply Last reply
                  0
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #9

                    Got it done.

                    I am making the signal emit the QDate and float and the slot is consuming the QDate and float.

                    Thank you once again.

                    1 Reply Last reply
                    0

                    • Login

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