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. Confusion in connecting SIGNALS and SLOTS between two separate classes
Qt 6.11 is out! See what's new in the release blog

Confusion in connecting SIGNALS and SLOTS between two separate classes

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.5k 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.
  • T Offline
    T Offline
    thescouse
    wrote on last edited by
    #1

    Hello everyone.
    I am trying to connect two classes as in making a signal and a slot in one class, where the signal would be emitted by the slot function and recieved by the slot in another class where the real action would be performed.
    I have done the following:

    1. Declared the signal and slot in first class header file:

    signals:
    @ void saveclicked();

    public slots:
    void savepic();@

    1. Declared the slot in the second class:

    @public slots:
    void savejpg();@

    1. Emitting the signal in the function definition:

    @void videocapture::savepic()
    {
    emit saveclicked();
    }@

    1. Finally the last class definition:

    @void videowidget::paintEvent(QPaintEvent *event) {
    try{
    connect(savepic(),SIGNAL(saveclicked()),this, SLOT(savejpg()));
    QPainter painter(this);

        if(!img.isNull()){
            painter.drawImage(QPoint(0,0), img);
            }
    }catch(...){}
    

    }

    void videowidget::savejpg()
    {
    QPainter painter(this);

        if(!img.isNull()){
            painter.drawImage(QPoint(0,0), img);
            img.save("test","jpg");
            }
    

    }
    @

    where the function in the paintevent is for video feed purpose and the savejpg is for capturing image when save button is clicked.
    It results in the following error:
    savepic() was not declared in this scope.

    Is my methosd correct? Or the error shown is the only error, Which i doubt..
    So how to really correct it? Thanks in advance.

    1 Reply Last reply
    0
    • L Offline
      L Offline
      legarcon
      wrote on last edited by
      #2

      Look at the definition of the connect method.
      First parameter is the sender but not the
      sender as the signal method but the sending class.

      "http://qt-project.org/doc/qt-4.8/qobject.html#connect":http://qt-project.org/doc/qt-4.8/qobject.html#connect

      1 Reply Last reply
      0
      • T Offline
        T Offline
        thescouse
        wrote on last edited by
        #3

        thanks I'll check it out.

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

          Hi,

          Don't do the connection in the paintEvent, this function should only do paiting related stuff. Also, each time paintEvent is called, you will add a new connection so your slot will be called several times.

          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
          • T Offline
            T Offline
            thescouse
            wrote on last edited by
            #5

            I had tried doing the same thing in a single class where the save capture button was made..but it somehow didn't work though when done in the paint event class it worked..any reason why it didn't work..the program didn't throw any error on compilation..I had declared a QImage in video capture too.

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

              With only parts of your classes it's impossible to tell.

              This connection syntax won't throw any error a build time, but if something's wrong, you'll have a message at run time

              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

              • Login

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