Confusion in connecting SIGNALS and SLOTS between two separate classes



  • 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.



  • 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



  • thanks I'll check it out.


  • Lifetime Qt Champion

    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.



  • 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.


  • Lifetime Qt Champion

    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


Log in to reply
 

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