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. Cann't write connect in main.cpp
QtWS25 Last Chance

Cann't write connect in main.cpp

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 4 Posters 3.3k Views
  • 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.
  • L.GogsL Offline
    L.GogsL Offline
    L.Gogs
    wrote on last edited by
    #1
    #include <QCoreApplication>
    #include <QTimer>
    #include <QObject>
    #include <QDebug>
    
    class Taz742 : public QObject
    {
    public:
        Taz742()
        {
            tm = new QTimer();
        }
    
        QTimer *tm;
        ~Taz742()
        {
            delete tm;
        }
    
    public slots:
        void Print()
        {
            qDebug() << "Taz742";
        }
    };
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        Taz742 *L = new Taz742();
        L->tm->start(1000);
        L->connect(L->tm, SIGNAL(timeout()), L, SLOT(Print()));
    
        return a.exec();
    }
    
    

    on the black screen appears --> QObject::connect No such slot QObject::Print()
    what is wrong in my code?

    L.GogsL 1 Reply Last reply
    1
    • L.GogsL L.Gogs
      #include <QCoreApplication>
      #include <QTimer>
      #include <QObject>
      #include <QDebug>
      
      class Taz742 : public QObject
      {
      public:
          Taz742()
          {
              tm = new QTimer();
          }
      
          QTimer *tm;
          ~Taz742()
          {
              delete tm;
          }
      
      public slots:
          void Print()
          {
              qDebug() << "Taz742";
          }
      };
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
          Taz742 *L = new Taz742();
          L->tm->start(1000);
          L->connect(L->tm, SIGNAL(timeout()), L, SLOT(Print()));
      
          return a.exec();
      }
      
      

      on the black screen appears --> QObject::connect No such slot QObject::Print()
      what is wrong in my code?

      L.GogsL Offline
      L.GogsL Offline
      L.Gogs
      wrote on last edited by
      #2
      This post is deleted!
      m.sueM 1 Reply Last reply
      0
      • L.GogsL L.Gogs

        This post is deleted!

        m.sueM Offline
        m.sueM Offline
        m.sue
        wrote on last edited by
        #3

        Hi @L.Gogs

        Your class does not have a Q_OBJECT macro.

        -Michael.

        L.GogsL 1 Reply Last reply
        2
        • m.sueM m.sue

          Hi @L.Gogs

          Your class does not have a Q_OBJECT macro.

          -Michael.

          L.GogsL Offline
          L.GogsL Offline
          L.Gogs
          wrote on last edited by
          #4

          @m.sue
          yes I know but when I wrote it, I had errors

          Pradeep KumarP 1 Reply Last reply
          0
          • Pradeep KumarP Offline
            Pradeep KumarP Offline
            Pradeep Kumar
            wrote on last edited by
            #5
            class Taz742 : public QObject
            {
            Q_OBJECT 
            public:
                Taz742()
                {
                    tm = new QTimer();
                }
            
                QTimer *tm;
                ~Taz742()
                {
                    delete tm;
                }
            
            public slots:
                void Print()
                {
                    qDebug() << "Taz742";
                }
            };
            

            Thanks,

            Pradeep Kumar
            Qt,QML Developer

            L.GogsL 1 Reply Last reply
            0
            • L.GogsL L.Gogs

              @m.sue
              yes I know but when I wrote it, I had errors

              Pradeep KumarP Offline
              Pradeep KumarP Offline
              Pradeep Kumar
              wrote on last edited by
              #6

              @L.Gogs

              can u post what errors occurred.

              Thanks,

              Pradeep Kumar
              Qt,QML Developer

              L.GogsL 1 Reply Last reply
              1
              • Pradeep KumarP Pradeep Kumar

                @L.Gogs

                can u post what errors occurred.

                Thanks,

                L.GogsL Offline
                L.GogsL Offline
                L.Gogs
                wrote on last edited by
                #7

                @Pradeep-Kumar
                of course :))

                undefined reference to 'vtable for Taz742'

                m.sueM 1 Reply Last reply
                0
                • Pradeep KumarP Pradeep Kumar
                  class Taz742 : public QObject
                  {
                  Q_OBJECT 
                  public:
                      Taz742()
                      {
                          tm = new QTimer();
                      }
                  
                      QTimer *tm;
                      ~Taz742()
                      {
                          delete tm;
                      }
                  
                  public slots:
                      void Print()
                      {
                          qDebug() << "Taz742";
                      }
                  };
                  

                  Thanks,

                  L.GogsL Offline
                  L.GogsL Offline
                  L.Gogs
                  wrote on last edited by
                  #8
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • L.GogsL L.Gogs

                    @Pradeep-Kumar
                    of course :))

                    undefined reference to 'vtable for Taz742'

                    m.sueM Offline
                    m.sueM Offline
                    m.sue
                    wrote on last edited by
                    #9

                    Hi @L.Gogs

                    Your Taz742 destructor is virtual, by inheritance from QObject. A class with virtual functions should be defined in its own file: Taz742.cpp and Taz742.h

                    -Michael.

                    L.GogsL 1 Reply Last reply
                    1
                    • m.sueM m.sue

                      Hi @L.Gogs

                      Your Taz742 destructor is virtual, by inheritance from QObject. A class with virtual functions should be defined in its own file: Taz742.cpp and Taz742.h

                      -Michael.

                      L.GogsL Offline
                      L.GogsL Offline
                      L.Gogs
                      wrote on last edited by
                      #10

                      @m.sue
                      Thanks...
                      but how can I write connect in main.cpp

                      m.sueM 1 Reply Last reply
                      1
                      • L.GogsL L.Gogs

                        @m.sue
                        Thanks...
                        but how can I write connect in main.cpp

                        m.sueM Offline
                        m.sueM Offline
                        m.sue
                        wrote on last edited by
                        #11

                        Hi @L.Gogs

                        Just #include the header file. Then you can use your class there

                        -Michael.

                        L.GogsL 1 Reply Last reply
                        1
                        • m.sueM m.sue

                          Hi @L.Gogs

                          Just #include the header file. Then you can use your class there

                          -Michael.

                          L.GogsL Offline
                          L.GogsL Offline
                          L.Gogs
                          wrote on last edited by
                          #12

                          @m.sue
                          no no, I want to write connect into main.cpp without any other header or cpp file.

                          m.sueM mrjjM 2 Replies Last reply
                          0
                          • L.GogsL L.Gogs

                            @m.sue
                            no no, I want to write connect into main.cpp without any other header or cpp file.

                            m.sueM Offline
                            m.sueM Offline
                            m.sue
                            wrote on last edited by m.sue
                            #13

                            Hi @L.Gogs

                            To be blunt: You have to comply with the rules of the computer language (C++ in this case) or not use it. You cannot discuss such issues with the compiler/linker.

                            -Michael.

                            1 Reply Last reply
                            1
                            • L.GogsL L.Gogs

                              @m.sue
                              no no, I want to write connect into main.cpp without any other header or cpp file.

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by mrjj
                              #14

                              @L.Gogs said in Cann't write connect in main.cpp:

                              no no, I want to write connect into main.cpp without any other header or cpp file.

                              https://stackoverflow.com/questions/34928933/why-is-important-to-include-moc-file-at-end-of-a-qt-source-code-file

                              You can hack it but using a .h file is the right way and besides this approach might have issues.
                              I never used it as its non common to only have .cpp file outside school projects.

                              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