VS 2010 Qt加载项中的链接错误

时间:2012-09-26 00:31:16

标签: c++ visual-studio-2010 qt linker

我在Visual Studio 2010中遇到了Qt的一些问题。 我已经从this site下载了qt二进制文件,版本 Qt-4.7.3-dev-msvc2010-rdh 。 这段代码没有任何问题。

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget* parent);

protected:
     void paintEvent(QPaintEvent *e);
private:
};

当我添加这一行时:

void keyPressEvent(QKeyEvent *e);

发生此错误

1>------ Build started: Project: lol, Configuration: Release Win32 ------
1>Build started 25/09/2012 21:04:40.
1>InitializeBuildStatus:
1>  Creating "Release\lol.unsuccessfulbuild" because "AlwaysCreate" was specified.
1>CustomBuild:
1>  Moc'ing lol.h...
1>ClCompile:
1>  All outputs are up-to-date.
1>  moc_lol.cpp
1>  lol.cpp
1>  main.cpp
1>  Generating Code...
1>lol.obj : error LNK2001: unresolved external symbol "protected: virtual void __thiscall lol::mousePressEvent(class QMouseEvent *)" (?mousePressEvent@lol@@MAEXPAVQMouseEvent@@@Z)
1>C:\Users\VTM\Documents\Visual Studio 2010\Projects\TC\Win32\Release\\lol.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:04.68
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

1 个答案:

答案 0 :(得分:0)

似乎链接器无法找到keyPressEvent实现。 你在Widget类中实现了keyPressEvent()吗?您必须这样做,因为您在类定义中声明了这一点。添加以下代码只是为了将事件转发到baseclass。

void Widget::keyPressEvent(QKeyEvent *e){ QWidget::keyPressEvent(e);}