Get undefined reference error but don't know why



  • Hello there,

    I have code a experimental minimal QApp to test how to open new Windows after pushing a button. But i get the error
    "undefined reference to 'vtable for MainWindow'".

    Here my SourceCode:
    mainwindow.h

    #pragma once
    #include <QWidget>
    #include <QBoxLayout>
    #include <QPushButton>
    #include "newwindow.h"
    
    class MainWindow : public QWidget
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = 0);
    
    public slots:
        void openNewWindow();
    };
    

    newwindow.h

    #pragma once
    
    #include <QWidget>
    #include <QBoxLayout>
    #include <QLabel>
    
    class NewWindow : public QWidget
    {
    public:
        NewWindow(QWidget *parent = 0);
    };
    

    mainwindow.cpp

    #include "mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
    {
        QVBoxLayout *vbox = new QVBoxLayout;
            vbox->setMargin(20);
        QPushButton *btn= new QPushButton("New window", this);
            btn->setMinimumSize(QSize(200,50));
    
        vbox->addWidget(btn, 2, Qt::AlignVCenter);
        setLayout(vbox);
    
        connect(btn, SIGNAL(clicked()), this, SLOT(openNewWindow()));
    }
    void MainWindow::openNewWindow()
    {
        NewWindow *newwindow = new NewWindow();
        newwindow->show();
    }
    

    newwindow.cpp

    #include "newwindow.h"
    
    NewWindow::NewWindow(QWidget *parent) : QWidget(parent)
    {
        QVBoxLayout *vbox = new QVBoxLayout;
            vbox->setMargin(20);
        QLabel *label = new QLabel("New Window", this);
    
        vbox->addWidget(label, 2, Qt::AlignVCenter);
        setLayout(vbox);
    }
    

    main.cpp

    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow window;
        window.show();
        return a.exec();
    }
    

    Thanks for all they want help
    -Casisto

    Edited: Put code after 3 backticks(```) and end with the same - p3c0



  • ok,
    i had can solve it by myself.

    For all they have they problem too:

    The error is sourced from the use of "Q_OBJECT".
    To solve the error, right-cklick on the Project and choose "Run qmake" and after this: "Rebuild".

    Then the error should be disappeared ;-)

    -casisto


  • Moderators

    Hi @casisto, I'm glad to hear that you've solved your problem. Thanks for sharing your solution!



  • I've also had the same problem that I solved the same way.
    I happened on Windows and ever on Linux


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.