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 and slot working without Q_OBJECT Macro

Signal and slot working without Q_OBJECT Macro

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 5 Posters 957 Views 2 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.
  • V Offline
    V Offline
    Vinoth Rajendran4
    wrote on last edited by
    #1

    Hi All,

    class MainWindow : public QMainWindow
    {
       // Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private:
        Ui::MainWindow *ui;
    
    };
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        connect(ui->pushButton,SIGNAL(clicked(bool)),this,SLOT(close()));
    
    }
    

    Q_OBJECT macro is commented, but still signal and slot is working fine.. Can someone please help me understand how this is possible ??

    aha_1980A 1 Reply Last reply
    0
    • V Vinoth Rajendran4

      Hi All,

      class MainWindow : public QMainWindow
      {
         // Q_OBJECT
      
      public:
          explicit MainWindow(QWidget *parent = 0);
          ~MainWindow();
      
      private:
          Ui::MainWindow *ui;
      
      };
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          connect(ui->pushButton,SIGNAL(clicked(bool)),this,SLOT(close()));
      
      }
      

      Q_OBJECT macro is commented, but still signal and slot is working fine.. Can someone please help me understand how this is possible ??

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Vinoth-Rajendran4

      Probably some prior generated files are still existing? It should stop working after you wiped the build folder and rebuild your program.

      Regards

      Qt has to stay free or it will die.

      V 1 Reply Last reply
      0
      • aha_1980A aha_1980

        @Vinoth-Rajendran4

        Probably some prior generated files are still existing? It should stop working after you wiped the build folder and rebuild your program.

        Regards

        V Offline
        V Offline
        Vinoth Rajendran4
        wrote on last edited by
        #3

        @aha_1980 , Thanks for the confirmation

        aha_1980A 1 Reply Last reply
        1
        • V Vinoth Rajendran4

          @aha_1980 , Thanks for the confirmation

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Vinoth-Rajendran4

          So is you problem solved? Then please mark this topic as SOLVED too. Thanks!

          Qt has to stay free or it will die.

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

            Hi,

            You are not adding any new signal or slot to your class but using what is already available hence there's no need for the Q_OBJECT macro in your case. However as soon as you want to implement signal/slot/property into your class you will have to add that macro.

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

            aha_1980A JKSHJ 2 Replies Last reply
            6
            • SGaistS SGaist

              Hi,

              You are not adding any new signal or slot to your class but using what is already available hence there's no need for the Q_OBJECT macro in your case. However as soon as you want to implement signal/slot/property into your class you will have to add that macro.

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @SGaist Good catch!

              Qt has to stay free or it will die.

              1 Reply Last reply
              0
              • SGaistS SGaist

                Hi,

                You are not adding any new signal or slot to your class but using what is already available hence there's no need for the Q_OBJECT macro in your case. However as soon as you want to implement signal/slot/property into your class you will have to add that macro.

                JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #7

                @SGaist said in Signal and slot working without Q_OBJECT Macro:

                You are not adding any new signal or slot to your class but using what is already available hence there's no need for the Q_OBJECT macro in your case. However as soon as you want to implement signal/slot/property into your class you will have to add that macro.

                Also, without the Q_OBJECT macro, this->metaObject()->className() returns the wrong class.

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                5
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by VRonin
                  #8

                  Based on experience, not on knowledge. Treat with caution.

                  Q_OBJECT is necessary if:

                  • you declare a new signal or slot
                  • use qobject_cast on that class
                  • use the meta-object system on that class

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  3

                  • Login

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