Update:
If I delete Q_OBJECT, and I do not use SLOT SINGAL, just use connect() like this:
connect(this, &QWidget::destroyed, this, &QWidget::myslot),
my code will run well without any warnings and errors.
I want to write a little code to instruct some classes, so I try to simplify my code. But I encountered some strange things. I cannot write a simple widget in my main.cpp. If I write the widget in mywidget.cpp and mywidget.h, the program runs well. If I want to write the widget in the main.cpp, what should I do?
This is my code.
#include <QApplication> #include <QWidget> class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = 0); ~Widget(); }; Widget::Widget(QWidget *parent) : QWidget(parent) { } Widget::~Widget() { } int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); } 
Q_OBJECT, the code runs well. But if I remove it, I cannot use theconnect(), which is a useful function I need.hppor.hfile, not in the.cpp.