Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Signal between 2 class
Qt 6.11 is out! See what's new in the release blog

Signal between 2 class

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 6 Posters 3.8k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Yacinoben
    wrote on last edited by Yacinoben
    #1

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

    miclandM 1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      2
      • P Offline
        P Offline
        praveen0991kr
        wrote on last edited by praveen0991kr
        #3

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

        1 Reply Last reply
        0
        • Y Yacinoben

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

          miclandM Offline
          miclandM Offline
          micland
          wrote on last edited by
          #4

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

          connect(view, &QListView::clicked, this, &A::newSignal);
          
          1 Reply Last reply
          3
          • Y Offline
            Y Offline
            Yacinoben
            wrote on last edited by Yacinoben
            #5

            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

            1 Reply Last reply
            0
            • mihoM Offline
              mihoM Offline
              miho
              wrote on last edited by
              #6

              http://easy-qt.blogspot.ru/2012/10/1.html

              p.s. russian site

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                3
                • Y Offline
                  Y Offline
                  Yacinoben
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • Chris KawaC Online
                    Chris KawaC Online
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

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

                    History history(this);
                    connect(&history,&History::selectHistory, this, [ ]{ qDebug("test"); } );
                    history.exec();
                    
                    1 Reply Last reply
                    2
                    • Y Offline
                      Y Offline
                      Yacinoben
                      wrote on last edited by
                      #10

                      Thanks !

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved