Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Q_OBJECT compile error... T^T



  • If I comment out the line that contains Q_OBJECT, it compiles fine. (but, SLOT does not work.)

    I have found a case that has experienced similar problems, but I don't know how to apply it to this source.

    please help me

    .h file

    #ifndef ROOMBUTTON_H
    #define ROOMBUTTON_H
    #include <QPushButton>
    
    class RoomButton : public QPushButton
    {
        Q_OBJECT // This line is error..
    
    public:
        RoomButton();
    
    
    public slots:
        void roomClicked();
    
    signals:
        void textChanged(QString &str);
    
    private:
        QString temp;
    
    
    };
    
    #endif // ROOMBUTTON_H
    

    .cpp file

    #include "roombutton.h"
    #include <QPushButton>
    
    RoomButton::RoomButton()
    {
        QSizePolicy sizePolicy2(QSizePolicy::Minimum, QSizePolicy::Preferred);
        this->setSizePolicy(sizePolicy2);
    
        QFont font = this->font();
        font.setPointSize(24);
        this->setFont(font);
    
        QObject::connect(this,SIGNAL(clicked()),
                this,SLOT(roomClicked()));
    }
    
    void RoomButton::roomClicked()
    {
        if (temp != this->text()) {
        temp = this->text();
        emit textChanged(temp);
        }
    }
    
    void RoomButton::textChanged(QString &str){
    }
    

    compile error code

    roombutton.cpp:4: error: undefined reference to `vtable for RoomButton'
    

    Thanks in advance for your help.


  • Moderators

    @Kycho said in Q_OBJECT compile error... T^T:

    compile error code

    roombutton.cpp:4: error: undefined reference to `vtable for RoomButton'
    

    You have some stale files in the build directory. There are a few different ways to fix it:

    • Click Build > Clean Project _____, OR
    • Manually delete your build folder, OR
    • Click Build > Run qmake (assuming you're using qmake)


  • @JKSH
    Thank you so much for solving the problem.


  • Moderators

    @Kycho
    There is more to fix (the constructor) or you'll end up with strange errors later down the road.

    public:
        RoomButton();
    
    //to
       explicit RoomButton(QWidget *parent = nullptr);
    
    RoomButton::RoomButton()
    {
        QSizePolicy sizePolicy2(QSizePolicy::Minimum, QSizePolicy::Preferred);
        this->setSizePolicy(sizePolicy2);
    
        QFont font = this->font();
        font.setPointSize(24);
        this->setFont(font);
    
        QObject::connect(this,SIGNAL(clicked()),
                this,SLOT(roomClicked()));
    }
    
    // to
    RoomButton::RoomButton(QWidget *parent)  : QPushButton(parent);
    {
        QSizePolicy sizePolicy2(QSizePolicy::Minimum, QSizePolicy::Preferred);
        this->setSizePolicy(sizePolicy2);
    
        QFont font = this->font();
        font.setPointSize(24);
        this->setFont(font);
    
        QObject::connect(this,SIGNAL(clicked()),
                this,SLOT(roomClicked()));
    }
    


  • @J.Hilk

    I modified the source according to your advice.
    Thank you so much!!


Log in to reply