Signal emitted but Slot won't work
-
Hello, I am relatively a new to be using Qt and studying Signals and Slot recently.
My first try is that when QString variable is set via a certain function outside of UI, append it to the QPlainTextEdit on UI.
So I write the code like
customconnectobject.h
class CustomConnectObject : public QObject
{
Q_OBJECTsignals:
void mySignal(QString);public:
void setString(QString msg)
{
emit mySignal(msg);
}mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
ui(new Ui::MainWindow)
{
ui->setupUi(this);
CustomConnectObject * cco =new CustomConnectObject();
connect(cco,&CustomConnectObject::mySignal,ui->plainTextOutput,&QPlainTextEdit::appendPlainText)otherfunction.cpp
CustomConnectObject * co =new CustomConnectObject();
co->setString("test");I expected whenever I call co->setString, I could get the text updated in the QPlainTextEdit object but nothing happens.
I checked if connect is made well and it was ok.
Please help me out
-
Signal shud be emitted from cco object not co object. That is the issue here.
-
@dheerendra yes, that too :-)
-
@Eeli-K
Thank you for a reply. So apparently the object 'co' in otherfunction.cpp and 'cco' in mainwindow.cpp are different.Then should I call 'cco' in mainwindow.cpp?
Sorry I know I am asking basic things but I wanted to understand Signals and Slot better.
-
Two different instances of the same type of object.
coo is a pointer so you have to use it where you can "see" it. You connected that objects signal to the slot so yes you must use the same pointer to the object to make it emit the signal. as @Eeli-K and @dheerendra have said. -
-
@checkers This is quite basic thing in C++, you clearly need practice in it and unfortunately there's no other way to learn it than practice, fail first and then succeed :) Eventually you will understand how to govern the lifetime of objects, where and when to create and destroy them and how objects can have access to or reach other objects. In C++ you can for example pass pointers and references as arguments to functions and store objects as member data. Kenchan already gave tips for this situation. It's difficult to say more without seeing the whole code.
-
@kenchan
I call some functions in otherfunction.cpp from mainwindow.cpp by including otherfunction.h
I wanted to call cco from those functions in otherfunction.cpp likeotherfunction.cpp
void testfunction()
{
cco->setString("test");
}
mainwindow.cpp
void MainWindow::on_pushButton_clicked()
{
testfunction();
}I will try passing cco directly to my function in otherfunction.cpp
thank you very much
-
@checkers Why does otherfunction.cpp need access to cco if cco is part of main window? Doing it like this is bad design. This testfunction could just return the string and you call cco then in mainwindow:
QString testfunction() { return "test"; } void MainWindow::on_pushButton_clicked() { cco->setString(testfunction()); }
-
@checkers Well, slots are usually defined in other classes than signals. That is the whole idea behind signals/slots: that an object can notify other objects via signals. The object which emits a signal usually does not know who is connected to this signal nor does it care - it just emits the signal, who ever wants receives it.