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_OBJECT

    signals:
    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



  • Just guess, perhaps that signal should have public scope?



  • @checkers You create two different objects of class CustomConnectObject (badly named btw, because it's a class, not an object). When in the otherfunction.cpp you call co->setString that object's signal isn't connected.



  • @kenchan Thank you for a replay, did you mean I should rewrite it

    public:
    void setString(QString msg)

    instead of

    signals:
    void setString(QString msg)

    but it didn't work



  • Signal shud be emitted from cco object not co object. That is the issue here.



  • No, more like
    public signals:
    or
    public
    signals:



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



  • @kenchan
    Thanks a lot.
    It is true that I created cco in mainwindow.cpp and connected it in mainwindow.cpp

    but is there a way to 'see' cco in otherfunction.cpp ?



  • where is other function relative to main window and cco?
    you could:
    pass the pointer to the other function directly
    or
    make cco a member of mainwindow and make mainwidow visible to other function and have a getter function in mainwindow
    ... etc.



  • @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 like

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



  • @Eeli-K
    Thank you. I know I need some practice in here as I am a novice to C++ also.
    I will try to solve current situation.


  • Moderators

    @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());
    }
    


  • @jsulm
    Thank you for advice
    You sound correct

    I may be confused the concept of Signals and Slot that Signals must be called from other place than Slot


  • Moderators

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


Log in to reply