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. Basic signal and slot

Basic signal and slot

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 793 Views 1 Watching
  • 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.
  • AsifBahrainwalaA Offline
    AsifBahrainwalaA Offline
    AsifBahrainwala
    wrote on last edited by p3c0
    #1

    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 to vtable 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

    1 Reply Last reply
    0
    • AsifBahrainwalaA Offline
      AsifBahrainwalaA Offline
      AsifBahrainwala
      wrote on last edited by
      #2

      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........

      1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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.

        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