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. why vtable won't work for single file app
Qt 6.11 is out! See what's new in the release blog

why vtable won't work for single file app

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 626 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.
  • Thank YouT Offline
    Thank YouT Offline
    Thank You
    wrote on last edited by Thank You
    #1

    I was just trying to make application without using ui files and only with single file.
    Everything was working and I got undefined 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 that

    You 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 about moc but couldn't find the answer

    this 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();
    }
    
    
    

    Let's make QT free or It will go forever

    TRUE AND FALSE <3

    1 Reply Last reply
    0
    • Thank YouT Offline
      Thank YouT Offline
      Thank You
      wrote on last edited by
      #6

      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.html

      If you got more add here I will edit and make single answer

      Let's make QT free or It will go forever

      TRUE AND FALSE <3

      1 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi

        MOC runs only on .h files

        but try

        #include "main.moc"

        in the bottom of main.cpp

        and then rebuild all

        Thank YouT 1 Reply Last reply
        1
        • mrjjM mrjj

          Hi

          MOC runs only on .h files

          but try

          #include "main.moc"

          in the bottom of main.cpp

          and then rebuild all

          Thank YouT Offline
          Thank YouT Offline
          Thank You
          wrote on last edited by
          #3

          @mrjj Yes It works Can you tell more about it ? How does it work?

          Let's make QT free or It will go forever

          TRUE AND FALSE <3

          eyllanescE jsulmJ 2 Replies Last reply
          0
          • Thank YouT Thank You

            @mrjj Yes It works Can you tell more about it ? How does it work?

            eyllanescE Offline
            eyllanescE Offline
            eyllanesc
            wrote on last edited by
            #4

            @Thank-You See https://stackoverflow.com/questions/38510050/define-signals-and-slots-inside-main-cpp

            If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

            1 Reply Last reply
            0
            • Thank YouT Thank You

              @mrjj Yes It works Can you tell more about it ? How does it work?

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #5

              @Thank-You https://doc.qt.io/qt-5/moc.html

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • Thank YouT Offline
                Thank YouT Offline
                Thank You
                wrote on last edited by
                #6

                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.html

                If you got more add here I will edit and make single answer

                Let's make QT free or It will go forever

                TRUE AND FALSE <3

                1 Reply Last reply
                1

                • Login

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