0

I've just started with Qt. This is an example from a book. I've tried to compare the code with that in the book. It seems identical.

I am getting errors. How can I fix them?

The NetBeans shows as follows:

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE=/C/Qt/4.8.4/bin/qmake.exe SUBPROJECTS= .build-conf
make[1]: Entering directory `/c/Documents and Settings/Deloitte/��� ���������/NetBeansProjects/HelloWorld1'
/C/Qt/4.8.4/bin/qmake.exe VPATH=. -spec win32-g++ -o qttmp-Debug.mk nbproject/qt-Debug.pro
mv -f qttmp-Debug.mk nbproject/qt-Debug.mk
"/usr/bin/make" -f nbproject/qt-Debug.mk dist/Debug/MinGW_Qt-Windows/HelloWorld1.exe
make[2]: Entering directory `/c/Documents and Settings/Deloitte/��� ���������/NetBeansProjects/HelloWorld1'
g++ -mthreads -Wl,-subsystem,windows -o dist/Debug/MinGW_Qt-Windows/HelloWorld1.exe build/Debug/MinGW_Qt-Windows/main.o build/Debug/MinGW_Qt-Windows/moc_Counter.o  -L'c:/Qt/4.8.4/lib' -lmingw32 -lqtmaind build/Debug/MinGW_Qt-Windows/HelloWorld1_resource_res.o -lQtGuid4 -lQtCored4 
build/Debug/MinGW_Qt-Windows/main.o: In function `Z5qMainiPPc':
C:\Documents and Settings\Deloitte\��� ���������\NetBeansProjects\HelloWorld1/main.cpp:16: undefined reference to `Counter::Counter()'
build/Debug/MinGW_Qt-Windows/moc_Counter.o: In function `ZN7Counter18qt_static_metacallEP7QObjectN11QMetaObject4CallEiPPv':
C:\Documents and Settings\Deloitte\��� ���������\NetBeansProjects\HelloWorld1/moc_Counter.cpp:56: undefined reference to `Counter::slotInc()'
collect2: ���������� ld ����������� � ����� �������� 1
make[2]: *** [dist/Debug/MinGW_Qt-Windows/HelloWorld1.exe] Error 1
make[2]: Leaving directory `/c/Documents and Settings/Deloitte/��� ���������/NetBeansProjects/HelloWorld1'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/c/Documents and Settings/Deloitte/��� ���������/NetBeansProjects/HelloWorld1'
make: *** [.build-impl] Error 2


BUILD FAILED (exit value 2, total time: 1s)

counter.h

#ifndef _Counter_h_
#define _Counter_h_

#include <QObject>

class Counter:public QObject{
    Q_OBJECT
    private:
        int m_nValue;
        public:
            Counter();
        public slots:
            void slotInc();
        signals:
            void goodbye();
            void counterChanged(int);
};

main.cpp

#include <QtGui/QApplication>
#include <QtGui>
#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();
    
    QObject::connect(&cmd, SIGNAL(clicked()), 
            &counter, SLOT (slotInc()));
    
    QObject::connect(&counter, SIGNAL(counterChanged(int)), 
            &lbl, SLOT(setNum(int)));
    
    QObject::connect(&counter, SIGNAL (goodbye()), &app, SLOT(quit()));
    
    
    return app.exec();
}
4

1 回答 1

0

您的代码缺少 Counter 类成员函数的实际定义,尤其Counter::Counter是构造函数和Counter::slotInc. 您必须为它们提供实现。

Counter::goodbye并且Counter::counterChanged是由 Qt MOC 提供定义的信号。

于 2013-05-01T10:50:19.197 回答