Signal & Slots problems



  • When I run my program, which include one class declared in header-file and initialized in source-file, my compiler send me these messages:

    1)main.obj:-1: ошибка: LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl Counter::metaObject(void)const " (?metaObject@Counter@@UEBAPEBUQMetaObject@@XZ)
    2)main.obj:-1: ошибка: LNK2001: unresolved external symbol "public: virtual void * __cdecl Counter::qt_metacast(char const *)" (?qt_metacast@Counter@@UEAAPEAXPEBD@Z)
    3)main.obj:-1: ошибка: LNK2001: unresolved external symbol "public: virtual int __cdecl Counter::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@Counter@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z)
    

    my program(n=main-file is not finished):
    main:

    #include<QtWidgets>
    #include<QObject>
    #include<counter.h>
    
    int main(int argc, char **argv)
    {
        QApplication app(argc, argv);
        QLabel lbl("0");
        QPushButton cmd("ADD");
        Counter counter;
        lbl.show();
        cmd.show();
        app.exec();
    }
    

    counter.h:

    #pragma once
    #include<QObject>
    class Counter : public QObject
    {
        Q_OBJECT
    private:
        int value;
    public:
        Counter();
    public slots:
        void slotInc();
    signals:
        void goodbye();
        void counterchange(int);
    };
    

    counter.cpp:

    #include "counter.h"
    
    
    Counter::Counter() : QObject(), value(0)
    {}
    void Counter::slotInc()
    {
        emit counterchange(++value);
        if(value==5)
        {
            emit goodbye();
        }
    }
    

    What's wrong?

    [Added code formatting tags ~kshegunov]



  • QWidget derived objects must be heap based. Start by changing your QLabel and QPushButton to pointers. You might start with the sample hello world example.


  • Qt Champions 2016

    @Boris1 ,
    Hello,
    Rerun qmake, the linker errors you see one usually gets when the Q_OBJECT macro hasn't been processed.

    Kind regards.


  • Qt Champions 2016

    @MSimpson
    Hello,

    QWidget derived objects must be heap based.

    No, they don't have to. I was tempted to write "mustn't", but well that would be just as wrong as saying they "must" be allocated on the heap.

    Cheers!



  • @kshegunov
    What does mean phrase "rerun qmake"?


  • Qt Champions 2016

    @Boris1
    It means to go to the Build menu and select "Run qmake"
    so it runs that tool.



  • Yeah, it works! Thank you all!


Log in to reply
 

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