I have been struggling for a while with an issue on Qt.
我已经在Qt上挣扎了一段时间。
Here is my code:
这是我的代码:
hexbutton.h:
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:
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:
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:
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;
}
And finally, Main.cpp:
最后,Main.cpp:
#include <QApplication>
#include "MyWindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWindow fenetre;
fenetre.show();
return app.exec();
}
Just in case, here is the Test.pro
为了以防万一,这里是Test.pro。
SOURCES += \
Main.cpp \
MyWindow.cpp \
hexbutton.cpp
HEADERS += \
MyWindow.h \
hexbutton.h
And I get the 2 errors:
我得到了两个错误:
1) symbol(s) not found for architecture x86_64
1)没有发现用于架构x86_64的符号。
2) collect2: ld returned 1 exit status
2)collect2: ld返回1退出状态。
It also says 121 times (11*11):
它还说121次(11*11):
Object::connect: No such slot QPushButton::changeIcon() in ../Test/MyWindow.cpp:19
对象:连接:没有这样的插槽QPushButton::changeIcon() in ./Test/MyWindow.cpp:19。
and on the compile output it says:
在编译输出中,它说
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'
Apparently the error comes from the Q_OBJECT (needed for the slots definition), but there is something wrong with my code, not with my compiler (because I have when slots are in MainWindow, it works fine).
显然,错误来自Q_OBJECT(槽定义的需要),但是我的代码有问题,而不是我的编译器(因为当插槽在主窗口时,它可以正常工作)。
Thank you for your help!
谢谢你的帮助!
9 个解决方案
#1
14
Faced same problem
面临同样的问题
- undefined refrence error for my signal.
- 我的信号没有定义的折射误差。
- After putting Q_OBJECT macro... vtable errors.
- 在把Q_OBJECT宏……vtable错误。
I did this and worked for me
我这样做了,为我工作。
- Added Q_OBJECT to file
- Q_OBJECT添加到文件
- Cleaned project
- 清洁项目
- Ran qmake
- 跑qmake
- Rebuild
- 重建
it compiles fine.
它运行良好。
#2
12
Got the same error. I had a private slot declared in a .h file, but I didn't have it defined in my cpp file. I removed my unused slot in both places, and it compiled.
有同样的错误。我有一个在.h文件中声明的私有插槽,但是我没有在cpp文件中定义它。我删除了两个地方的未使用的插槽,并编译。
#3
4
This happened to me when merging Qt code from one platform to another. I just resolved it, after much trial and error, so I'm posting it here since my issue was different. If you are merging new files, you can't just merge the files and the .Pro file and expect that they'll show up in Qt Creator for compilation. Notice your new files aren't actually being displayed in the IDE under your folders. I overlooked this, and was just looking at the folder structure and .Pro file. Once I manually added all of the files in Qt, it compiled fine. Here is how I figured this out:
当将Qt代码从一个平台合并到另一个平台时,我遇到了这种情况。我刚刚解决了这个问题,经过反复试验和错误,所以我把它贴在这里,因为我的问题不一样。如果您正在合并新文件,您不能只合并文件和. pro文件,并期望它们会出现在Qt创建者的编译中。注意,您的新文件实际上并没有显示在文件夹下的IDE中。我忽略了这个,只是看看文件夹结构和。pro文件。一旦我手动添加了Qt中的所有文件,它就会被编译。我是这么想的:
- Right click on the vague "symbol(s) not found for architecture x86_64" error, and select Compiled Output
- 右键单击“未找到用于架构x86_64”错误的“符号(s)”,并选择已编译的输出。
- At the bottom it will show what method(s) it can't find. Find what file(s) those methods are in, and add those headers and class files, by right clicking the folder they should be in the IDE, and selecting "Add Existing File"
- 在底部,它将显示它找不到的方法。找到那些方法所在的文件,并添加这些头和类文件,通过右键单击它们应该在IDE中的文件夹,并选择“添加现有文件”
- Build, or Clean then Build
- 构建,或清理然后构建。
#4
2
CONFIG -= x86_64
fixed this problem for me
CONFIG -= x86_64为我解决了这个问题。
#5
1
I found the answer finally! (for whoever will be interested)
我终于找到了答案!(对任何感兴趣的人)
The mistake is that I shouldn't have extended the class QPushButton, to customize my button, but rather, create my own class HexButton extending/inheriting the QWidget class! Doing so we must add to the button, a QPushButton instance. Well it then makes:
错误是我不应该扩展类QPushButton,来定制我的按钮,而是创建自己的类HexButton扩展/继承QWidget类!这样做我们必须添加到按钮,一个QPushButton实例。然后它就会自动将:
class HexButton : public QWidget
{
Q_OBJECT
public:
HexButton(QWidget *parent = 0);
public slots:
void changeIcon();
private:
QPushButton *button;
};
#6
1
This can happen, when you forgot the Class-Operator, when defining your function.
这可能发生,当您在定义函数时忘记了类操作符。
H File:
H文件:
class Globals
{
public:
unsigned int myFunction();
};
CPP File:
CPP文件:
unsigned int Globals::myFunction(){return 3;}
#7
0
Move the slot changeIcon() into HexButton class from MyWindow class.
从MyWindow类将slot changeIcon()移动到HexButton类。
#8
0
Normally this happens with Qt because of your build system - it indicates that you're not compiling/linking in the code generated by moc
通常情况下,这是由于您的构建系统而发生的Qt——它表明您没有在moc生成的代码中编译/链接。
I'm pretty sure that there's nothing wrong with your code - so the error must be in some setting in qt creator, which I can't really help with as I've not used it much.
我很确定你的代码没有任何问题——所以错误必须在qt创建者的某些设置中,因为我没有使用太多,所以我无法真正帮助它。
If you haven't already, you could try adding an explicit empty destructor declaration to hexbutton.hpp and definition to hexbutton.cpp. It shouldn't be necessary, but I've heard of this making a difference on some quirky compilers/linkers.
如果您还没有这样做,您可以尝试向hexbutton添加一个显式的空析构声明。hpp和hexbutton.cpp的定义。这不应该是必要的,但我听说过这对一些古怪的编译器/连接器产生了影响。
#9
0
try to remove pro.user file and reimport project with any qt 32bit kit.
试着删除pro。使用任何qt 32位组件的用户文件和再导入项目。
#1
14
Faced same problem
面临同样的问题
- undefined refrence error for my signal.
- 我的信号没有定义的折射误差。
- After putting Q_OBJECT macro... vtable errors.
- 在把Q_OBJECT宏……vtable错误。
I did this and worked for me
我这样做了,为我工作。
- Added Q_OBJECT to file
- Q_OBJECT添加到文件
- Cleaned project
- 清洁项目
- Ran qmake
- 跑qmake
- Rebuild
- 重建
it compiles fine.
它运行良好。
#2
12
Got the same error. I had a private slot declared in a .h file, but I didn't have it defined in my cpp file. I removed my unused slot in both places, and it compiled.
有同样的错误。我有一个在.h文件中声明的私有插槽,但是我没有在cpp文件中定义它。我删除了两个地方的未使用的插槽,并编译。
#3
4
This happened to me when merging Qt code from one platform to another. I just resolved it, after much trial and error, so I'm posting it here since my issue was different. If you are merging new files, you can't just merge the files and the .Pro file and expect that they'll show up in Qt Creator for compilation. Notice your new files aren't actually being displayed in the IDE under your folders. I overlooked this, and was just looking at the folder structure and .Pro file. Once I manually added all of the files in Qt, it compiled fine. Here is how I figured this out:
当将Qt代码从一个平台合并到另一个平台时,我遇到了这种情况。我刚刚解决了这个问题,经过反复试验和错误,所以我把它贴在这里,因为我的问题不一样。如果您正在合并新文件,您不能只合并文件和. pro文件,并期望它们会出现在Qt创建者的编译中。注意,您的新文件实际上并没有显示在文件夹下的IDE中。我忽略了这个,只是看看文件夹结构和。pro文件。一旦我手动添加了Qt中的所有文件,它就会被编译。我是这么想的:
- Right click on the vague "symbol(s) not found for architecture x86_64" error, and select Compiled Output
- 右键单击“未找到用于架构x86_64”错误的“符号(s)”,并选择已编译的输出。
- At the bottom it will show what method(s) it can't find. Find what file(s) those methods are in, and add those headers and class files, by right clicking the folder they should be in the IDE, and selecting "Add Existing File"
- 在底部,它将显示它找不到的方法。找到那些方法所在的文件,并添加这些头和类文件,通过右键单击它们应该在IDE中的文件夹,并选择“添加现有文件”
- Build, or Clean then Build
- 构建,或清理然后构建。
#4
2
CONFIG -= x86_64
fixed this problem for me
CONFIG -= x86_64为我解决了这个问题。
#5
1
I found the answer finally! (for whoever will be interested)
我终于找到了答案!(对任何感兴趣的人)
The mistake is that I shouldn't have extended the class QPushButton, to customize my button, but rather, create my own class HexButton extending/inheriting the QWidget class! Doing so we must add to the button, a QPushButton instance. Well it then makes:
错误是我不应该扩展类QPushButton,来定制我的按钮,而是创建自己的类HexButton扩展/继承QWidget类!这样做我们必须添加到按钮,一个QPushButton实例。然后它就会自动将:
class HexButton : public QWidget
{
Q_OBJECT
public:
HexButton(QWidget *parent = 0);
public slots:
void changeIcon();
private:
QPushButton *button;
};
#6
1
This can happen, when you forgot the Class-Operator, when defining your function.
这可能发生,当您在定义函数时忘记了类操作符。
H File:
H文件:
class Globals
{
public:
unsigned int myFunction();
};
CPP File:
CPP文件:
unsigned int Globals::myFunction(){return 3;}
#7
0
Move the slot changeIcon() into HexButton class from MyWindow class.
从MyWindow类将slot changeIcon()移动到HexButton类。
#8
0
Normally this happens with Qt because of your build system - it indicates that you're not compiling/linking in the code generated by moc
通常情况下,这是由于您的构建系统而发生的Qt——它表明您没有在moc生成的代码中编译/链接。
I'm pretty sure that there's nothing wrong with your code - so the error must be in some setting in qt creator, which I can't really help with as I've not used it much.
我很确定你的代码没有任何问题——所以错误必须在qt创建者的某些设置中,因为我没有使用太多,所以我无法真正帮助它。
If you haven't already, you could try adding an explicit empty destructor declaration to hexbutton.hpp and definition to hexbutton.cpp. It shouldn't be necessary, but I've heard of this making a difference on some quirky compilers/linkers.
如果您还没有这样做,您可以尝试向hexbutton添加一个显式的空析构声明。hpp和hexbutton.cpp的定义。这不应该是必要的,但我听说过这对一些古怪的编译器/连接器产生了影响。
#9
0
try to remove pro.user file and reimport project with any qt 32bit kit.
试着删除pro。使用任何qt 32位组件的用户文件和再导入项目。