0

I get compile error that explain

undefined reference to `vtable for Test'

If i remove/comment Q_OBJECT macro, compiler is fine. I think theres problem with class Test.

like compiler don't know what to do with Q_OBJECT macro.

my environtment is mingw64 with qmake tool.

what i need to do to make compiler knows what Q_OBJECT macro is?

#include <QApplication>
#include <QWidget>
#include <QObject>
#include <QPushButton>
#include <QGridLayout>
#include <QMessageBox>

class Test: public QObject {
  Q_OBJECT

  public slots:
    void message() {
      QMessageBox message;
      message.setText("Click event was triggered.");
      message.exec();
    };
};

int main(int argc, char *argv[]) {
  QApplication app(argc, argv);
  Test test;

  QWidget *window = new QWidget;
  
  QGridLayout *layout = new QGridLayout(window);

  QPushButton *login = new QPushButton("Info");
  QObject::connect(login, SIGNAL(clicked()), &test, SLOT(message()));

  layout->addWidget(login, 0, 0);

  window->setLayout(layout);
  window->show();

  return app.exec();
}
yui amanda
  • 17
  • 2
  • Try moving your Test class into its own header file (like test.h), and in your .pro file add `INCLUDES += test.h`. – JarMan Sep 25 '21 at 13:46
  • @JarMan ok, it works. hmm, why i should split it into different file to make it works? – yui amanda Sep 25 '21 at 13:55
  • @yuiamanda add `"main.moc"` after `}` – eyllanesc Sep 25 '21 at 16:16
  • @eyllanesc after or before? in bottom class test i add main.moc? is it not compile error? – yui amanda Sep 25 '21 at 16:20
  • @yuiamanda 1. I said "after" `return app.exec();` `}`, 2. If you are going to use Q_OBJECT then the MOC needs to implement its magic, and in your case it does it in main.moc so you must include it in main.cpp – eyllanesc Sep 25 '21 at 16:22

1 Answers1

-2

The QObject class should be in its own header file (like test.h), and your .pro file should contain:

INCLUDES += test.h

That allows the moc tool to properly find it and process it.

JarMan
  • 7,589
  • 1
  • 10
  • 25