Solved SIGNAL AND SLOTS
-
Hello, I have a problem and I don't understand why.
Until now I had only one window(main), and when I added new window and I move some components because in the main where too many, signal and slots stop working. Was everything fine in main, same code in other class(new window) which is printed into a stackedWidget from centralwidget, doesn't work.// connect textChange signal with TextChange slot to change height of chat line connect(ui.mChatLine, SIGNAL(textChanged()), this, SLOT(TextChanged())); // connect user selection signal with UserChange slot to chat view connect(ui.mUserList, SIGNAL(clicked(QModelIndex)), this, SLOT(UserChanged(QModelIndex)));
-
If your new class has a
chatView
, achatLine
and the slotsUserChanged()
+TextChanged()
it should work.
Re-runqmake
and see if it works.@AlexandruToma said in SIGNAL AND SLOTS:
Do my new ui have mChatLine and mUserList if I see them here?
btw: Not everything what IntelliSense suggests is a good idea.
How do you design your ui? With QtDesigner? -
@AlexandruToma said in SIGNAL AND SLOTS:
I move some components because in the main where too many, signal and slots stop working
"Moved" in which way? You cant just take code from one class to another, especially when the code references ui elements.
So how exactly did you move your stuff? -
@AlexandruToma
As @Pl45m4 says. And if you used New Signal Slot Syntax you would have a much better chance of spotting mistakes/getting this right, instead of guesswork.... -
I moved (ctrl+x) all components with their implementation from centralwidget(QMainWindow) into other window. I dont have errors, it s all fine, just doesn't call that UserChanged or TextChanged slots anymore
-
@AlexandruToma thats the point, would you use the new signal and slot syntax, that @JonB linked, you would have compile time errors and not runtime silent fails.
Copy pasted, huh,
I'm almost certain your new ui doesn't have amChatLine
ormUserList
component in it. -
Dont try to access ui elements from another class in your new class. It wont work.
If you need to change something, send a signal to the ui-"owner" class and let the same class change its ui.(Send signal from
newWindow
to yourmainWindow
and keep the code, you've copied, there) -
@J-Hilk
Do my new ui have mChatLine and mUserList if I see them here?I dont try to acces ui elements from another class. I try to receive an signal from current class ui into the current class slot.
-
If your new class has a
chatView
, achatLine
and the slotsUserChanged()
+TextChanged()
it should work.
Re-runqmake
and see if it works.@AlexandruToma said in SIGNAL AND SLOTS:
Do my new ui have mChatLine and mUserList if I see them here?
btw: Not everything what IntelliSense suggests is a good idea.
How do you design your ui? With QtDesigner? -
@Pl45m4 Thank you for you time. The problem was that I forgot to write this line of code
public slots:
above slots in my new class header file.
@J-Hilk said in SIGNAL AND SLOTS:
I'm almost certain your new ui doesn't have a mChatLine or mUserList component in it.
If I already have all ui components created and styled in one window, it s like a reuse of code, I didn't copy/paste something from web, was my code and I knew what I am doing, when I move all ui components. I just miss that line because in c++ I didn't use that line.
You were not trying to help me here, just make fun of me as i copy and paste code. I understand there are many people doing this, but it s not fair to treat all of us like that.
Have a good day sir. -
@AlexandruToma
first of, I'm glad that you were able to solve your problem, and also thanks for sharing the answer, not something everyone does.If I already have all ui components created and styled in one window, it s like a reuse of code, I didn't copy/paste something from web, was my code and I knew what I am doing, when I move all ui components. I just miss that line because in c++ I didn't use that line.
You were not trying to help me here, just make fun of me as i copy and paste code. I understand there are many people doing this, but it s not fair to treat all of us like that.to address this: I never said, nor assumed you copied anything from the internet.
you said:
when I added new window ... signal and slots stop working
and on the question
"Moved" in which way?
you answered:
I moved (ctrl+x) all components with their implementation from centralwidget(QMainWindow) into other window.
That plus the actual connect call, is all the information we have to go on with. That is not a lot of information. For example had you posted more code of your class in the opening post, @Pl45m4 or @JonB would have easily spotted the missing
slots
macroWhich by the way is a copy and paste error on your part.
And, had you, like we suggested, used the new Qt5 Signal/Slot syntax the problem would have gone away, as it does not require the slots macro any longer.Btw, the Qt4 connect statement you used, should have provided the following message during runtime:
QObject::connect: No such slot Mainview::TextChanged() in ....
which would have been nice to know as well, as it would have made the error obvious.
If anything, take this with you:
- Don't be discouraged to ask for or provide other in this forum with help.
- If you ask a question, provide as much information as possible, ideally a code example or even better, a minimal compileable example. The more the better!
- treat warnings as errors (
-Werror
) - Change over to the new signal/slot syntax, it makes life easier
- Think positive 😄