【发布时间】:2012-02-21 00:00:14
【问题描述】:
我一直在为 Qt 上的一个问题苦苦挣扎。
这是我的代码:
hexbutton.h:
#ifndef HEXBUTTON_H
#define HEXBUTTON_H
#include <QPushButton>
#include <QWidget>
#include <QIcon>
class HexButton : public QPushButton
{
Q_OBJECT
public:
HexButton(QWidget *parent, QIcon &icon, int i, int j);
public slots:
void changeIcon();
};
#endif // HEXBUTTON_H
Hexbutton.cpp:
#include "hexbutton.h"
HexButton::HexButton(QWidget *parent, QIcon &icon, int i , int j) : QPushButton(parent){
//setFlat(true);
setIcon(icon);
setGeometry((i*40)+((j*40)/2), j*40, 40, 40);
}
void HexButton::changeIcon(){
setIcon(QIcon("/Users/jonathanbibas/Documents/Workspace Qt/Test/hexagon.gif"));
}
MyWindow.h:
#ifndef MYWINDOW_H
#define MYWINDOW_H
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QLCDNumber>
#include <QSlider>
#include <QProgressBar>
#include "hexbutton.h"
class MyWindow : public QWidget
{
public:
MyWindow();
~MyWindow();
private:
HexButton * myButtons[11][11];
};
#endif // MYWINDOW_H
MyWindow.cpp:
#include "MyWindow.h"
#include <QColor>
#include <QIcon>
MyWindow::MyWindow() : QWidget() {
setFixedSize(740, 440);
QIcon icon = QIcon("/Users/jonathanbibas/Documents/Workspace Qt/Test/whitehexagon.png");
for(int i =0 ; i < 11 ; i ++){
for(int j =0 ; j < 11 ; j ++){
myButtons[i][j] = new HexButton(this, icon, i, j);
QObject::connect(myButtons[i][j], SIGNAL(clicked()), myButtons[i][j], SLOT(changeIcon()));
}
}
}
MyWindow::~MyWindow()
{
delete myButtons;
}
最后是 Main.cpp:
#include <QApplication>
#include "MyWindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWindow fenetre;
fenetre.show();
return app.exec();
}
以防万一,这里是 Test.pro
SOURCES += \
Main.cpp \
MyWindow.cpp \
hexbutton.cpp
HEADERS += \
MyWindow.h \
hexbutton.h
我得到了 2 个错误:
1) 未找到架构 x86_64 的符号
2) collect2: ld 返回 1 个退出状态
它还说 121 次 (11*11):
Object::connect: ../Test/MyWindow.cpp:19 中没有这样的插槽 QPushButton::changeIcon()
并在编译输出中显示:
18:22:15: Running build steps for project Test...
18:22:15: Configuration unchanged, skipping qmake step.
18:22:15: Starting: "/usr/bin/make" -w
make: Entering directory `/Users/jonathanbibas/Documents/Workspace Qt/Test-build-desktop-Desktop_Qt_4_8_0_for_GCC__Qt_SDK__Debug'
g++ -c -pipe -g -gdwarf-2 -arch x86_64 -Xarch_x86_64 -mmacosx-version-min=10.5 -Wall -W -DQT_GUI_LIB -DQT_CORE_LIB -I../../../QtSDK/Desktop/Qt/4.8.0/gcc/mkspecs/macx-g++ -I../Test -I../../../QtSDK/Desktop/Qt/4.8.0/gcc/lib/QtCore.framework/Versions/4/Headers -I../../../QtSDK/Desktop/Qt/4.8.0/gcc/include/QtCore -I../../../QtSDK/Desktop/Qt/4.8.0/gcc/lib/QtGui.framework/Versions/4/Headers -I../../../QtSDK/Desktop/Qt/4.8.0/gcc/include/QtGui -I../../../QtSDK/Desktop/Qt/4.8.0/gcc/include -I. -I../Test -I. -F/Users/jonathanbibas/QtSDK/Desktop/Qt/4.8.0/gcc/lib -o hexbutton.o ../Test/hexbutton.cpp
g++ -headerpad_max_install_names -arch x86_64 -Xarch_x86_64 -mmacosx-version-min=10.5 -o Test.app/Contents/MacOS/Test Main.o MyWindow.o hexbutton.o moc_MyWindow.o -F/Users/jonathanbibas/QtSDK/Desktop/Qt/4.8.0/gcc/lib -L/Users/jonathanbibas/QtSDK/Desktop/Qt/4.8.0/gcc/lib -framework QtGui -framework QtCore
Undefined symbols for architecture x86_64:
"vtable for HexButton", referenced from:
HexButton::HexButton(QWidget*, QIcon&, int, int)in hexbutton.o
HexButton::HexButton(QWidget*, QIcon&, int, int)in hexbutton.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [Test.app/Contents/MacOS/Test] Error 1
make: Leaving directory `/Users/jonathanbibas/Documents/Workspace Qt/Test-build-desktop-Desktop_Qt_4_8_0_for_GCC__Qt_SDK__Debug'
18:22:20: The process "/usr/bin/make" exited with code 2.
Error while building project Test (target: Desktop)
When executing build step 'Make'
显然错误来自 Q_OBJECT(插槽定义需要),但我的代码有问题,而不是我的编译器(因为当插槽位于 MainWindow 时,它工作正常)。
感谢您的帮助!
【问题讨论】:
-
您能否包含完整的错误,即未找到哪些符号?
-
你知道moc吗?如果类没有正确地移动,您可能会遇到链接问题。尝试从命令行使用 qmake 构建。
-
我添加了有关该错误的更多详细信息。但我不知道moc。如果你说错误来自链接器,我会使用它。
-
我在弄乱 Qt+OpenCV 时也遇到了这个问题,一个未定义但声明的插槽也是我的问题,就像乔纳森的一样。很容易错过,因为插槽代码是通过设计器中的鼠标单击生成的。谢谢,乔纳森的解决方案。 (我还不能投票。)
-
@JonathanBibas,我刚刚在使用 Qt Creator 时遇到了同样的问题,在 MyWindow.cpp 末尾添加 #include "MyWindow.moc" 解决了它,这对你有用吗?