PDA

View Full Version : Singal and slot...!



prakashmaiya
1st May 2015, 07:18
Dear Experts,

Here is code snippet to emit a signal from other class.

source code (a.cpp)

classA::keypressevent(...)
{
sender = new classname(...);
connect(sender , SIGNAL(select(int)), this, SLOT(action(int));
}

source code(a.h)

class classA:QWidget
{
public slot:
void action(int value);
}

source code (b.cpp)

classname:keypressevent()
{
/* do some operation */
...
emit select(0);
}

source code(b.h)

class classname
{
signal:
void select(int);
}

program is been successfully compiled and executed as well... But the problem is signal is not been emitted, or its emitted receiver (slot) is not ready yet to listen or slot (receiver) unable to perform its action.

Really confused where the problem is, and how would i come to know or rectify it to solve the same. Or else how to get the return value of the connect. how would i know whether signal is been emitted or not.

Would really appreciate the quick response. Thanks in advance...

jefftee
1st May 2015, 07:28
Is classA::keypressevent ever executed? If so, is it executed before or after classname::keypressevent? Also, please confirm that your keypressevent method is actually an override of the QWidget::keyPressEvent virtual method and that your actual code matches the virtual method's signature and case. i.e. keypressevent is different than keyPressEvent.

Edit: Please use
tags when posting source code.

prakashmaiya
1st May 2015, 07:31
No classA::keypressevent is executed before classname::keypressevent.

my control reaches the classname::keypressevent method from the classA::keypressevent only!!

jefftee
1st May 2015, 07:35
You lost me, I don't see where classA::keypressevent invokes classname::keypressevent?

jefftee
1st May 2015, 07:39
Why don't you copy/paste your code in the code tags I referenced earlier. Your example is missing the method for your action slot, so I suspect your contrived example above isn't your actual code since it's in the wrong case, etc.

prakashmaiya
1st May 2015, 07:39
Yeah infact it is QWidget::keyPressEvent method only!!! sorry for the wrong case sensitive used previous!! :)

No, i mean its big project am working on, but it reaches through the after execution of classA::keyPressEvent method only :)

prakashmaiya
1st May 2015, 07:41
Dear jeftee,

if u dont mind could u plz guide me to post the code in the same tag, as am really unaware of that, where n how to post in the code tag as u suggested earlier. thanks in advance.

jefftee
1st May 2015, 07:43
Click on the BB Code link near the bottom right of this thread, it will show you syntax for using BB code. It's similar to HTML, where each tag has a starting tag and ending tag you wrap around your code... For the code tag, you would use it as follows when posting:




// your code goes here

d_stranz
1st May 2015, 16:40
// your code goes here


And the word "code" should be in all uppercase with no extra spaces between the word and the enclosing square brackets. (Lowercase will work, but all of the editing keys insert uppercase tokens, so be consistent).

An alternative is to click the "Go Advanced" button when typing your reply. You will see an enhanced interface, with a "#" icon. Click this and it will insert a pair of "code" tags. Paste real code in between them, not miss-typed, misspelled, uncompilable stuff you make up on the fly. It isn't worth our time to try to help you when what you post isn't an actual example of what is causing a problem and isn't even correct code.

prakashmaiya
2nd May 2015, 09:51
a.cpp
void ChannelInfo::keyPressEvent(QKeyEvent * event)
{
m_filelist = new filelist(MAX_COMBO_LIST_BUTTON_5, 1, QSize(362, 25), FLIST_TYPE_FAVORITE_SELECT, 1, this);
connect(m_filelist, SIGNAL(sigFavClickOk(int)), this, SLOT(sltFavClickOk(int)));

a.h
class ChInfoPlateAnimation: public QWidget
{
Q_OBJECT
private:
friend class ChannelInfo;
class ChannelInfo : public ChInfoPlateAnimation
{
Q_OBJECT
public slots:
void sltFavClickOk(int value);
private:
filelist *m_filelist;

b.cpp
void filelist::keyPressEvent(QKeyEvent *event)
{
emit sigFavClickOk(0);
}

b.h
class filelist : public QWidget
{
Q_OBJECT
signals:
void sigFavClickOk(int value);


This is what have written, could let me know any problem with this, if not why it is not emitting or slot not listen yet and finally nothing is working out.

Thanks in advance for your suggestion.
:)



a.cpp
void ChannelInfo::keyPressEvent(QKeyEvent * event)
{
m_filelist = new filelist(MAX_COMBO_LIST_BUTTON_5, 1, QSize(362, 25), FLIST_TYPE_FAVORITE_SELECT, 1, this);
connect(m_filelist, SIGNAL(sigFavClickOk(int)), this, SLOT(sltFavClickOk(int)));
}
a.h
class ChInfoPlateAnimation: public QWidget
{
Q_OBJECT
private:
friend class ChannelInfo;
}
class ChannelInfo : public ChInfoPlateAnimation
{
Q_OBJECT
public slots:
void sltFavClickOk(int value);
private:
filelist *m_filelist;
}
b.cpp
void filelist::keyPressEvent(QKeyEvent *event)
{
emit sigFavClickOk(0);
}

b.h
class filelist : public QWidget
{
Q_OBJECT
signals:
void sigFavClickOk(int value);
}


compiled successfully but signal not emitted or dont know that whether slot is not ready yet to listen!!!! kindly do any one help me out to solve this, thanks in advance.

Added after 26 minutes:

Dear jefftee,

any idea abt this?, thanks in advance. :)

anda_skoa
2nd May 2015, 11:41
Have you verified that filelist::keyPressEvent() is being called?
E.g. use qDebug() to log something or by using a debugger break point?

Cheers,
_

prakashmaiya
2nd May 2015, 12:03
yes it is being called.

Added after 10 minutes:

I think by this code, it should work!!!!!!!!, really dont understand where it got struck or not working!!!!!!!!!

Added after 8 minutes:



void filelist::keyPressEvent(QKeyEvent *event)
{
case AKEY_EXIT:
emit sigFavClickOk(0);
break;
}


So ill emit this sigFavClickOk when i pressed exit button while am in filelist focusproxy. by doing this it sould call the slot called sltFavClickOk() method of channelinfo class. But where as here it no happening. how do i check whether it is been successful emitted or not, i mean is there any test like getting return type of that. i think control not reached in side sltFavClickOk() method as i have printed some valued test but it is not reached there. but just emit is being called!!! may be slot is not yet listened? not able to get where the problem is

immediate solution would be highly appreciate!!!!.

Regards,
Maiya

anda_skoa
2nd May 2015, 12:36
Have you checked the return value of connect()?

What is the number of received for your signal at the time of emit? (see QObject::receivers())

Cheers,
_

prakashmaiya
2nd May 2015, 13:33
int result;
result = connect(m_filelist, SIGNAL(sigFavClickOk(int)), this, SLOT(sltFavClickOk(int)));
qDebug("************************************result:%d\n", result);


yep, connect returns value '1'

************************************result:1

anda_skoa
2nd May 2015, 15:05
And the number of receivers at the point of emit?
And have you verfied that the filelist instance used in the connect is the one who's event handler is called?

Cheers,
_

jefftee
2nd May 2015, 20:51
You still didn't show the code for sltFavClickOk.

prakashmaiya
2nd May 2015, 20:59
I could not able to get u!!

And the number of receivers at the point of emit? -- how do i check the same ?

And have you verfied that the filelist instance used in the connect is the one who's event handler is called? -- really not able to understand this!!



a.cpp file
void ChannelInfo::sltFavClickOk(int value)
{
qDebug("ChannelInfo::%s()", __FUNCTION__);
this->setFocus();

m_timer->start();
{
/* do some thing here */
}
}


sltFavClickOk() is just a method of class ChannelInfo where i do some operations.

jefftee
2nd May 2015, 21:00
As part of their destructor, QObject, and classes that inherit from QObject, will disconnect any connected objects. If your m_filelist instance has been deleted for example, it would remove the connection and could be why when you finally emit the signal, nothing is connected to the slot any longer.

prakashmaiya
2nd May 2015, 21:05
okay...

So what shall i do now, to accomplish this, have any better idea to invoke this now?

jefftee
2nd May 2015, 21:12
Here's what I would recommend. Move the instantiation of your m_filelist and connect statement out of the keypress event somewhere else in your code, perhaps the constructor for ChannelInfo.

Set a breakpoint on your connect statement and write down values for m_filelist and the "this" pointer. Set a breakpoint on your emit statement, then verify the values for the m_filelist and "this" pointer.

If that does not lead you to the error, write out qDebug messages at the top of every method to make sure you understand the order and sequence that things are being invoked.

Your problem seems to me that you are either emitting the signal before the connect has been performed or your ChannelInfo instance or filelist instance has gone out of scope before you emit the signal.

prakashmaiya
2nd May 2015, 21:26
Dear jefftee,

But i don't think so my m_filelist instance has been deleted!!!, or else could you please let me know how to check the same, whether my m_filelist instance has been deleted or not?, as am very much new to this whole subject!!! Thanks in advance.

Added after 6 minutes:

may be i too suspect the same...!

Your problem seems to me that you are either emitting the signal before the connect has been performed or your ChannelInfo instance or filelist instance has gone out of scope before you emit the signal.

But how do i check whether my these instances has gone out of the scope!!!!

Added after 7 minutes:

Is there any QTimer concept will play the role here to make the instance gone out of scope or call the destructor or some thing else?

jefftee
2nd May 2015, 22:06
But how do i check whether my these instances has gone out of the scope!!!!
By using the debugger and setting breakpoints as I requested in my previous post. I also have switched to the new connect syntax available in Qt5.x, which lets the compile verify signal/slot signatures. Change your connect statement from:



connect(m_filelist, SIGNAL(sigFavClickOk(int)), this, SLOT(sltFavClickOk(int)));

to



connect(m_filelist, &filelist::sigFavClickOk, this, &ChannelInfo::sltFavClickOk);


Then recompile your program. If the signal/slot signatures don't match, the compiler will complain. I don't see where yours are mismatched, but sometimes it's the obvious overlooked things that are hard to find... :)

One more thing you might try, is to connect your signal to some instance/slot, perhaps something like:



connect(m_filelist, &filelist::sigFavClickOk, qApp, &QApplication::aboutQt);


Then emit your signal and see if you get the about Qt dialog, etc.

prakashmaiya
2nd May 2015, 23:41
Thank you very much jefftee, definitely i'll try it out now, as you suggested. :)

Added after 40 minutes:



connect(m_filelist, &filelist::sigFavClickOk, this, &ChannelInfo::sltFavClickOk);


getting an error like...!


error: within this context
error: no matching function for call to 'ChannelInfo::connect :*)(int), QApplication*, void (*)())'


even for the


connect(m_filelist, &filelist::sigFavClickOk, qApp, &QApplication::aboutQt);


same

anda_skoa
3rd May 2015, 09:31
And the number of receivers at the point of emit? -- how do i check the same ?

http://doc.qt.io/qt-4.8/qobject.html#receivers



And have you verfied that the filelist instance used in the connect is the one who's event handler is called? -- really not able to understand this!!

Check that the filelist object you are connecting with is the one that handles the event.
E.g. by logging the object pointer's value in both places.

Cheers,
_

d_stranz
3rd May 2015, 21:56
If the code as originally posted is the actual code being executed, then it seems that for each key press event, a new m_filelist instance is being created. Why? If this is a member variable of the ChannelInfo class, then for every key press event, you'll be creating another instance and connecting that instance to the slot. At the same time, the value currently in the m_filelist instance goes into limbo because the variable is re-assigned.

The net result of this code is that the m_filelist instance that was originally set up to fire the sigFavClickOK signal has probably long since been replaced by a different instance because of key press events.

But I'm just guessing, because the only code that shows anything of significance in this whole thread is the keyPressEvent that creates the multiple m_filelist instances. There is absolutely nothing in the code shown for the filelist class that gives any indication where the signal is emitted, and unfortunately I left my magic code demystifier somewhere and I haven't been able to find it for a while.

prakashmaiya
4th May 2015, 06:22
or else kindly could any suggest, how to implement the below logic...,


Now I'm in one screen called chninfo. when I press any key (for example: 'select' key) which goes to another screen called list screen.

here I do some operations, and finally when press 'EXIT' key here (in this screen) it should go back to the original screen (called chninfo) and where it should do some operations (by invoking some method - which actually do the necessary operations).

this is actually trying to do it by SIGNAL/SLOT concept, but actually it doesn't working out for me!!!!!!!


Kindly could any one do suggest on this, I would really appreciate the response. Thanks in advance... :)

jefftee
4th May 2015, 07:18
Kindly could any one do suggest on this, I would really appreciate the response. Thanks in advance... :)
Did you move your instantiation of the filelist and connect statements as d_stranz suggested?

For each keypress, you are creating a new filelist and connect statement. Those should be done once outside of keyPressEvent.

d_stranz
4th May 2015, 20:33
Now I'm in one screen called chninfo. when I press any key (for example: 'select' key) which goes to another screen called list screen.

here I do some operations, and finally when press 'EXIT' key here (in this screen) it should go back to the original screen (called chninfo) and where it should do some operations (by invoking some method - which actually do the necessary operations).

You must have a special keyboard, because mine doesn't have keys labeled "Select" or "EXIT".

I'm sorry, but I can't help you. You are giving us vague descriptions of a user interface, no code to back it up, and you expect us to give you a solution. The Qt documentation, examples, and tutorials have dozens of examples of using signals and slot to navigate among different parts of a user interface. Why don't you take the time to study them and learn what might apply to your application?