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. Signal & Slots problems
Forum Updated to NodeBB v4.3 + New Features

Signal & Slots problems

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 2.4k Views 2 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.
  • Boris1B Offline
    Boris1B Offline
    Boris1
    wrote on last edited by kshegunov
    #1

    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]

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MSimpson
      wrote on last edited by
      #2

      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.

      kshegunovK 2 Replies Last reply
      0
      • M MSimpson

        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.

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3

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

        Kind regards.

        Read and abide by the Qt Code of Conduct

        Boris1B 1 Reply Last reply
        2
        • M MSimpson

          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.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @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!

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • kshegunovK kshegunov

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

            Kind regards.

            Boris1B Offline
            Boris1B Offline
            Boris1
            wrote on last edited by
            #5

            @kshegunov
            What does mean phrase "rerun qmake"?

            mrjjM 1 Reply Last reply
            0
            • Boris1B Boris1

              @kshegunov
              What does mean phrase "rerun qmake"?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

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

              1 Reply Last reply
              2
              • Boris1B Offline
                Boris1B Offline
                Boris1
                wrote on last edited by Boris1
                #7

                Yeah, it works! Thank you all!

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved