It should be said that I carefully googled and tried many ways, but the problem did not disappear. Here is the actual code, all in one main.cpp file, as it is conceived, when you press a button, it is separated into a new window.

#include <QApplication> #include <QtWidgets> class custombtn: public QPushButton{ Q_OBJECT public: custombtn(QWidget * parent = nullptr):QPushButton(parent){} public slots: void slotChangeParent(){ this->setParent(nullptr); this->show(); } }; int main(int argc, char **argv){ QApplication app(argc,argv); QWidget wgt; custombtn btn(&wgt); QLabel lbl("LABEL",&wgt); btn.resize(300,300); lbl.resize(300,100); btn.show(); lbl.show(); wgt.show(); QObject::connect(&btn, SIGNAL(clicked(bool)), &btn, SLOT(slotChangeParent())); return app.exec(); } 

Here is the pro file:

 QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = untitled TEMPLATE = app SOURCES += main.cpp 

I tried to run qmake, create a new project and build in it, delete the Debug folder and re-compile, the path to the project consists only of English letters, tried to copy the project even to the root of the disk (to make sure that it’s not the paths), but errors from the series LNK2001: неразрешенный внешний символ "public: virtual struct QMetaObject const* __cdecl custombrn::metaObject(void)const"[...] remained. What can be wrong?

  • 3
    Make a declaration in * .h ... Perhaps moc cannot generate the necessary sorts for such a file. Or are you doing something wrong ... - Vladimir Martyanov
  • @ Vladimir Martiyanov collect everything as usual - qmake - run in qt creator - Nik
  • one
    Add the generated MOC file to the end of your file: #include "main.moc" - Pavel Parshin
  • @PavelParshin there is no such file. - Nik
  • @ Vladimir Martiyanov Thank you, you blew everything into different files and it works. - Nik

1 answer 1

In order to use the Q_OBJECT macro, Q_OBJECT necessary that the class using it be in separate files, in this case in the custombtn.h and custombtn.cpp . File custombtn.h :

 #include <QtWidgets> class custombtn: public QPushButton{ Q_OBJECT public: custombtn(QWidget * parent = nullptr); public slots: void slotChangeParent(); }; 

File custombtn.cpp:

 #include "mainwindow.h" custombtn::custombtn(QWidget * parent):QPushButton(parent){} void custombtn::slotChangeParent(){ this->setParent(nullptr); this->show(); } 
  • And proofs will be? (except empiricism) - free_ze
  • @free_ze and what you need proofs? - Nik
  • References to the documentation. I did not read about any such agreements, but I also encountered this problem. - free_ze
  • @free_ze; I did not read this in the documentation either. Now I have purposefully searched, but specifically that classes should be moved to separate files is not written there. But the fact is that it works that way. - Nik
  • 2
    @free_ze Regarding documentation, here’s a quote ( doc.qt.io/qt-5/moc.html ): "For those files that contain a Q_OBJECT macro. " The moc compiler analyzes header files only. - Pavel Parshin