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

Basic signal and slot



  • trying to better understand the signals and slots, got the code from here:-
    http://doc.qt.io/qt-4.8/signalsandslots.html

    for the life of me ..... it just wouldn't build
    error: undefined reference to Counter::valueChanged(int)' error: undefined reference tovtable for Counter'
    error: undefined reference to `vtable for Counter'

    I am using the Dialog application project template
    in pro

    QMAKE_CXXFLAGS += -fpermissive
    QMAKE_CXXFLAGS += -std=c++14
    

    in Dialog.h

    class Counter : public QObject
    {
        Q_OBJECT
    
    public:
        Counter() { m_value = 0; }
    
        int value() const { return m_value; }
    
    public slots:
        void setValue(int value);
    
    signals:
        void valueChanged(int newValue);
    
    private:
        int m_value;
    };
    

    in dialog.cpp

    void Counter::setValue(int value)
    {
        if (value != m_value) {
            m_value = value;
            emit valueChanged(value);
        }
    }
    

    Edited: Added code tags - p3c0



  • I got it to work when I created separate header (Counter.h) /CPP (Counter.cpp) file , is there any way to configure the MOC to not do this........


  • Lifetime Qt Champion

    Hi

    Moc is run on the headers
    so class defs should be in the header.

    Besides its recommended structure to use .h files for class
    definitions as not to make huge mess.

    However if you dream of not using .h files , it is possible
    http://stackoverflow.com/questions/3001615/qt-moc-with-implementations-inside-of-header-files

    For example: (file main.cpp)

    struct SubObject : QObject
    {
    Q_OBJECT
    };

    //...

    #include "main.moc"

    --

    The Q_OBJECT does all the magic and its ultra important to run qmake if adding this macro.


Log in to reply