Solved why vtable won't work for single file app
-
I was just trying to make application without using ui files and only with single file.
Everything was working and I gotundefined reference "vtable for mainwindow"
when I added Q_OBJECT which I had seen before too. What I knew about it was we should run qmake to make it work. And It doesn't work in this case. I tried fully removing build folders too.https://forum.qt.io/topic/33004/vtable-issue-and-moc/2
Here @SGaist had mentioned thatYou need to declare your MainWindow in it's own header in order for moc to make it's job.
Why it doesn't work for single page?
Are there reasons for doing it so?
https://doc.qt.io/archives/qt-4.8/moc.html
I tried reading aboutmoc
but couldn't find the answerthis is simplepage.pro
QT += core gui greaterThan(QT_MAJOR_VERSION,4):QT +=widgets TEMPLATE=app TARGET=simple_page CONFIG +=c++11 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 SOURCES += main.cpp
this is main.cpp
#include<QApplication> #include<QVBoxLayout> #include<QPushButton> #include<QtWidgets> #include<QObject> #include<QMainWindow> #include<QLineEdit> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); virtual ~MainWindow(){} private: QWidget *mainWidget; QVBoxLayout *mainLayout; QPushButton *btn,*closeButton; QLineEdit *edit; }; MainWindow::MainWindow(QWidget *parent):QMainWindow(parent){ mainWidget = new QWidget(parent); mainLayout = new QVBoxLayout(); btn = new QPushButton("Button which "); closeButton = new QPushButton("Close Window"); edit = new QLineEdit(mainWidget); edit->setText("starting Text"); mainLayout->addWidget(edit); mainLayout->addWidget(btn); mainLayout->addWidget(closeButton); mainWidget->setLayout(mainLayout); setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint); setCentralWidget(mainWidget); } int main(int argc, char *argv[]) { QApplication app(argc,argv); MainWindow *mainWin = new MainWindow(); mainWin->show(); return app.exec(); }
-
Compiled answer from stack overflow with other links too.
Standard C++ doesn't provide support for the dynamic meta-information needed by Qt's meta-object system. Qt solves this problem by providing a separate tool, moc, that parses Q_OBJECT class definitions and makes the information available through C++ functions. Since moc implements all its functionality using pure C++, Qt's meta-object system works with any C++ compiler.
Signals and slots in Qt are managed through the moc: meta object compiler. Basically, the moc generates additional C++ code for each class containing the Q_OBJECT macro in order to implement effectively the signals and slots mechanisms. The additional code is then linked to the original class declaration.
29
It's necessary if you define QObject subclasses with the Q_OBJECT macro in a .cpp file. When you do so:
qmake must generate rules inside your Makefile to invoke moc on that .cpp file.That special (hackish?) inclusion triggers qmake to do so, and tells it which would be moc's output file (teststring.moc) when invoked on your .cpp.
In order to compile moc's output (which is still a bunch of C++ code) the compiler must see your class definition. Otherwise, it will complain that there's no such thing as YourClass::staticMetaObject and similar, because it has no idea that YourClass exists.
Typically one defines classes featuring Q_OBJECT in a header file. moc then adds a #include "header.h" into its generated output, and this means moc's output can be happily compiled.
But what if your class definition is inside a .cpp? You can't #include a .cpp file in moc's output, as that would give you tons of redefinition errors.
Instead, you #include moc's output in your .cpp, so that it gets compiled together and everyone is happy. (This means qmake will only emit one rule saying to run moc, but not another rule telling the compiler to compile moc's output.)
From 2. you can also also desume that defining classes with Q_OBJECT in a .h does not require any special inclusion.
When Q_OBJECT in xxx.h, after qmake, the system will generate a moc_xxx.cpp with xxx.h in it.
When Q_OBJECT in xxx.cpp, after qmake, the system will generate a xxx.moc, and you need to add the .moc file into the .cpp file.
LINKS
http://doc.qt.io/qt-5/why-moc.html
https://doc.qt.io/qt-5/moc.htmlIf you got more add here I will edit and make single answer
-
Hi
MOC runs only on .h files
but try
#include "main.moc"
in the bottom of main.cpp
and then rebuild all
-
@mrjj Yes It works Can you tell more about it ? How does it work?
-
-
-
Compiled answer from stack overflow with other links too.
Standard C++ doesn't provide support for the dynamic meta-information needed by Qt's meta-object system. Qt solves this problem by providing a separate tool, moc, that parses Q_OBJECT class definitions and makes the information available through C++ functions. Since moc implements all its functionality using pure C++, Qt's meta-object system works with any C++ compiler.
Signals and slots in Qt are managed through the moc: meta object compiler. Basically, the moc generates additional C++ code for each class containing the Q_OBJECT macro in order to implement effectively the signals and slots mechanisms. The additional code is then linked to the original class declaration.
29
It's necessary if you define QObject subclasses with the Q_OBJECT macro in a .cpp file. When you do so:
qmake must generate rules inside your Makefile to invoke moc on that .cpp file.That special (hackish?) inclusion triggers qmake to do so, and tells it which would be moc's output file (teststring.moc) when invoked on your .cpp.
In order to compile moc's output (which is still a bunch of C++ code) the compiler must see your class definition. Otherwise, it will complain that there's no such thing as YourClass::staticMetaObject and similar, because it has no idea that YourClass exists.
Typically one defines classes featuring Q_OBJECT in a header file. moc then adds a #include "header.h" into its generated output, and this means moc's output can be happily compiled.
But what if your class definition is inside a .cpp? You can't #include a .cpp file in moc's output, as that would give you tons of redefinition errors.
Instead, you #include moc's output in your .cpp, so that it gets compiled together and everyone is happy. (This means qmake will only emit one rule saying to run moc, but not another rule telling the compiler to compile moc's output.)
From 2. you can also also desume that defining classes with Q_OBJECT in a .h does not require any special inclusion.
When Q_OBJECT in xxx.h, after qmake, the system will generate a moc_xxx.cpp with xxx.h in it.
When Q_OBJECT in xxx.cpp, after qmake, the system will generate a xxx.moc, and you need to add the .moc file into the .cpp file.
LINKS
http://doc.qt.io/qt-5/why-moc.html
https://doc.qt.io/qt-5/moc.htmlIf you got more add here I will edit and make single answer