Qt World Summit: Submit your Presentation

Basic signal and slot

  • trying to better understand the signals and slots, got the code from here:-

    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
        Counter() { m_value = 0; }
        int value() const { return m_value; }
    public slots:
        void setValue(int value);
        void valueChanged(int newValue);
        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


    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

    For example: (file main.cpp)

    struct SubObject : QObject


    #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