Signal between 2 class



  • Hello,
    I have 2 classes and I have to connect them with a signal

    A::A()
    {
    QListView *view=new QListView(this)
    /*
    ..
    */
    connect(view,&QListView::clicked,this,[ ](){emit newSignal();});
    };
    B::B()
    {
    A *a=new A;
    connect(a,&A::newSignal,this,()[ ]{qDebug("test");});
    }
    

    sorry for not post a complet code, he's long.
    Signal of QListView is works, but newSignal is not emited !
    Thanks for your help :)


  • Moderators

    Please post the actual code. This won't even compile.
    [ ](){emit newSignal();} this lambda does not capture this so it would be a compilation error to try and call newSignal() from it.
    ()[ ]{qDebug("test");} the parenthesis are all backwards.
    How and where is newSignal declared? Do you have a Q_OBJECT macro in you A class declaration?



  • connect(this->view,&QListView::clicked, this,[=]{
    emit this->newSignal();
    });



  • @Yacinoben
    Why using lambda in the first connect statement? You can directly connect signals with signals:

    connect(view, &QListView::clicked, this, &A::newSignal);
    


  • It doesn't work .
    this is my code :

    class History : public QDialog
    {
        Q_OBJECT
    public:
        explicit History(QWidget *parent = 0);
    
    signals:
        void selectHistory();
    };
    
    History::History(QWidget *parent) : QDialog(parent)
    {
    /*
    ...
    */
         connect(view,&QListView::clicked,this,&History::selectHistory);
    }
    
    void MainWindow::showHistory()
    {
        History *history=new History(this);
        history->exec();
        connect(history,&History::selectHistory,this,[ ] () {qDebug("test);});
    }
    

    The QListView contains a history of browser, i would like when i click in a index, i open a new tab in my navigator with a fonction addTab in Mainwindow, but the signal doesn't work




  • Lifetime Qt Champion

    Hi,

    You're doing your connection after exec which means that it will happen after you close your history dialog thus you won't see any message.

    On a side note, you are leaking memory since you don't delete history. The objects will get destroyed when MainWindow is since you give it a parent but the memory will fill up more each time you call showHistory.



  • Yes, you are right, when i call history, the object doesn't deletes, the use of the memory increases.
    but now, its work !!

       History *history=new History(this);
       history->setAttribute(Qt::WA_DeleteOnClose);
       connect(history,&History::selectHistory,this,[ ] () {qDebug("test");});
       history->exec();
    

    Thank's


  • Moderators

    It's easier and shorter to just create it on the stack:

    History history(this);
    connect(&history,&History::selectHistory, this, [ ]{ qDebug("test"); } );
    history.exec();
    


  • Thanks !


Log in to reply
 

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