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. QDialog Error: mingw32-make: *** [Makefile:45: debug] Error 2
Forum Updated to NodeBB v4.3 + New Features

QDialog Error: mingw32-make: *** [Makefile:45: debug] Error 2

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 739 Views
  • 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.
  • E Offline
    E Offline
    ElfMoon
    wrote on last edited by
    #1

    I'm new in QT, this is code I copy from another source (which I forget).
    Please help me to explain what is Error 2

    /* Dialog Example */
    #include "mainwindow.h"
    #include <QAction>
    #include <QApplication>
    #include <QComboBox>
    #include <QDialog>
    #include <QDialogButtonBox>
    #include <QFormLayout>
    #include <QGroupBox>
    #include <QLabel>
    #include <QLineEdit>
    #include <QMainWindow>
    #include <QMenu>
    #include <QMenuBar>
    #include <QPushButton>
    #include <QSpinBox>
    #include <QTextEdit>
    #include <QVBoxLayout>
    #include <QWidget>
    
    class Dialog : public QDialog {
        Q_OBJECT
    
        public:
        Dialog();
    
        private:
        void createMenu();
        void createHorizontalGroupBox();
        void createGridGroupBox();
        void createFormGroupBox();
    
        enum { NumGridRows = 3, NumButtons = 4 };
    
        QMenuBar *menuBar;
        QGroupBox *horizontalGroupBox;
        QGroupBox *gridGroupBox;
        QGroupBox *formGroupBox;
        QTextEdit *smallEditor;
        QTextEdit *bigEditor;
        QLabel *labels[NumGridRows];
        QLineEdit *lineEdits[NumGridRows];
        QPushButton *buttons[NumButtons];
        QDialogButtonBox *buttonBox;
    
        QMenu *fileMenu;
        QAction *exitAction;
    };
    
    Dialog::Dialog() {
        createMenu();
        createHorizontalGroupBox();
        createGridGroupBox();
        createFormGroupBox();
    
        bigEditor = new QTextEdit;
        bigEditor->setPlainText(
          tr( "This widget takes up all the remaining space "
             "in the top-level layout." ) );
    
        buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok |
                             QDialogButtonBox::Cancel );
    
        connect( buttonBox, &QDialogButtonBox::accepted, this,
            &QDialog::accept );
        connect( buttonBox, &QDialogButtonBox::rejected, this,
            &QDialog::reject );
    
        QVBoxLayout *mainLayout = new QVBoxLayout;
        mainLayout->setMenuBar( menuBar );
        mainLayout->addWidget( horizontalGroupBox );
        mainLayout->addWidget( gridGroupBox );
        mainLayout->addWidget( formGroupBox );
        mainLayout->addWidget( bigEditor );
        mainLayout->addWidget( buttonBox );
        setLayout( mainLayout );
    
        setWindowTitle( tr( "Basic Layouts" ) );
    }
    
    void Dialog::createMenu() {
        menuBar = new QMenuBar;
    
        fileMenu = new QMenu( tr( "&File" ), this );
        exitAction = fileMenu->addAction( tr( "E&xit" ) );
        menuBar->addMenu( fileMenu );
    
        connect( exitAction, &QAction::triggered, this, &QDialog::accept );
    }
    void Dialog::createHorizontalGroupBox() {
        horizontalGroupBox = new QGroupBox( tr( "Horizontal layout" ) );
        QHBoxLayout *layout = new QHBoxLayout;
    
        for ( int i = 0; i < NumButtons; ++i ) {
            buttons[i] = new QPushButton( tr( "Button %1" ).arg( i + 1 ) );
            layout->addWidget( buttons[i] );
        }
        horizontalGroupBox->setLayout( layout );
    }
    void Dialog::createGridGroupBox() {
        gridGroupBox = new QGroupBox( tr( "Grid layout" ) );
        QGridLayout *layout = new QGridLayout;
    
        for ( int i = 0; i < NumGridRows; ++i ) {
            labels[i] = new QLabel( tr( "Line %1:" ).arg( i + 1 ) );
            lineEdits[i] = new QLineEdit;
            layout->addWidget( labels[i], i + 1, 0 );
            layout->addWidget( lineEdits[i], i + 1, 1 );
        }
    
        smallEditor = new QTextEdit;
        smallEditor->setPlainText(
          tr( "This widget takes up about two thirds of the "
             "grid layout." ) );
        layout->addWidget( smallEditor, 0, 2, 4, 1 );
        layout->setColumnStretch( 1, 10 );
        layout->setColumnStretch( 2, 20 );
        gridGroupBox->setLayout( layout );
    }
    void Dialog::createFormGroupBox() {
        formGroupBox = new QGroupBox( tr( "Form layout" ) );
        QFormLayout *layout = new QFormLayout;
        layout->addRow( new QLabel( tr( "Line 1:" ) ), new QLineEdit );
        layout->addRow( new QLabel( tr( "Line 2, long text:" ) ),
                new QComboBox );
        layout->addRow( new QLabel( tr( "Line 3:" ) ), new QSpinBox );
        formGroupBox->setLayout( layout );
    }
    
    int main( int argc, char *argv[] ) {
        QApplication app( argc, argv );
        Dialog dialog;
    
    #ifdef Q_OS_ANDROID
        dialog.showMaximized();
    #else
        dialog.show();
    #endif
        return app.exec();
    }
    

    This is error output:
    09:16:22: Running steps for project QTProject03...
    09:16:22: Configuration unchanged, skipping qmake step.
    09:16:22: Starting: "H:\QTCreator\Tools\mingw810_64\bin\mingw32-make.exe" -j4
    H:/QTCreator/Tools/mingw810_64/bin/mingw32-make -f Makefile.Debug
    mingw32-make[1]: Entering directory 'D:/1A1 o Disk D/Program Files 01/Cpp Project/build-QTProject03-Desktop_QT-Debug'
    g++ -c -fno-keep-inline-dllexport -g -std=gnu++11 -Wall -Wextra -Wextra -fexceptions -mthreads -DUNICODE -D_UNICODE -DWIN32 -DMINGW_HAS_SECURE_API=1 -DQT_QML_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_NEEDS_QMAIN -I..\QTProject03 -I. -IH:\QTCreator\5.15.2\mingw81_64\include -IH:\QTCreator\5.15.2\mingw81_64\include\QtWidgets -IH:\QTCreator\5.15.2\mingw81_64\include\QtGui -IH:\QTCreator\5.15.2\mingw81_64\include\QtANGLE -IH:\QTCreator\5.15.2\mingw81_64\include\QtCore -Idebug -I. -I/include -IH:\QTCreator\5.15.2\mingw81_64\mkspecs\win32-g++ -o debug\main.o ..\QTProject03\main.cpp
    g++ -Wl,-subsystem,windows -mthreads -o debug\QTProject03.exe debug/main.o debug/mainwindow.o debug/moc_mainwindow.o H:\QTCreator\5.15.2\mingw81_64\lib\libQt5Widgets.a H:\QTCreator\5.15.2\mingw81_64\lib\libQt5Gui.a H:\QTCreator\5.15.2\mingw81_64\lib\libQt5Core.a -lmingw32 H:\QTCreator\5.15.2\mingw81_64\lib\libqtmain.a -LC:\openssl\lib -LC:\Utils\my_sql\mysql-5.7.25-winx64\lib -LC:\Utils\postgresql\pgsql\lib -lshell32
    debug/main.o:main.cpp:(.rdata$.refptr._ZTV6Dialog[.refptr._ZTV6Dialog]+0x0): undefined reference to vtable for Dialog' debug/main.o:main.cpp:(.rdata$.refptr._ZN6Dialog16staticMetaObjectE[.refptr._ZN6Dialog16staticMetaObjectE]+0x0): undefined reference to Dialog::staticMetaObject'
    collect2.exe: error: ld returned 1 exit status
    mingw32-make[1]: *** [Makefile.Debug:72: debug/QTProject03.exe] Error 1
    mingw32-make: *** [Makefile:45: debug] Error 2
    mingw32-make[1]: Leaving directory 'D:/1A1 o Disk D/Program Files 01/Cpp Project/build-QTProject03-Desktop_QT-Debug'
    09:16:28: The process "H:\QTCreator\Tools\mingw810_64\bin\mingw32-make.exe" exited with code 2.
    Error while building/deploying project QTProject03 (kit: Desktop QT)
    When executing step "Make"
    09:16:28: Elapsed time: 00:06.

    I'm using QT 5.15.2 because the latest version of QT Creator cannot run on Windows 8.1
    Please keep support for old Windows 7 and Windows 8.1 until poor student can save enough money to buy new Laptop to run Windows 11.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #4

      Since you're using qmake as build system you have to run qmake afterwards so it sees that a main.moc is needed.
      Or even better - do what should be done with a separate class - move it out into an own source and header file and you won't need to include main.moc.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Since you did not put your class in a separate header file, moc is not run automatically to create the needed stuff for the Q_OBJECT macro and you have to add #include "main.moc" at the end of your main.cpp
        See also https://doc.qt.io/qt-6/moc.html

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        E 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          Since you did not put your class in a separate header file, moc is not run automatically to create the needed stuff for the Q_OBJECT macro and you have to add #include "main.moc" at the end of your main.cpp
          See also https://doc.qt.io/qt-6/moc.html

          E Offline
          E Offline
          ElfMoon
          wrote on last edited by ElfMoon
          #3

          @Christian-Ehrlicher said in QDialog Error: mingw32-make: *** [Makefile:45: debug] Error 2:

          Since you did not put your class in a separate header file, moc is not run automatically to create the needed stuff for the Q_OBJECT macro and you have to add #include "main.moc" at the end of your main.cpp
          See also https://doc.qt.io/qt-6/moc.html

          If I paste this code #include "main.moc" at the end of your main.cpp, QT Creator will give error file not found. I dont have file main.moc and I also dont know how to create it.
          Could you please post content of file main.moc that need to be created.

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Since you're using qmake as build system you have to run qmake afterwards so it sees that a main.moc is needed.
            Or even better - do what should be done with a separate class - move it out into an own source and header file and you won't need to include main.moc.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            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