简体   繁体   English

带有cmake的Visual Studio 2013中的Qt-如何使它工作?

[英]Qt in Visual Studio 2013 with cmake - how do I get this to work?

I am currently trying integrate the library Qt in my C++ project. 我目前正在尝试将库Qt集成到我的C ++项目中。 First I thought I could use it like any other library. 首先,我认为我可以像使用其他任何库一样使用它。 I downloaded it, added the include path and libs to my VS project and tried a small sample of code that just creates a simple window with a text edit and a button. 我下载了它,在我的VS项目中添加了include路径和库,并尝试了一小段代码示例,该示例仅创建了一个带有文本编辑和按钮的简单窗口。 This worked and the small user interface was displayed. 这可以正常工作,并且显示了小的用户界面。

Now I started to try implementing a class derived from a QWidget and realised, that it might not be that simple. 现在,我开始尝试实现一个从QWidget派生并实现的类,它可能不是那么简单。 The class I tried creating looked like this: 我尝试创建的类如下所示:

#include <QtWidgets\qstylepainter.h>
#include <QtWidgets\qwidget.h>

class MapRenderer : public QWidget{
    Q_OBJECT
public:
    MapRenderer(QWidget *parent = 0);
protected:
    void paintEvent(QPaintEvent* event);
};

And the corresponding cpp file: 和相应的cpp文件:

MapRenderer::MapRenderer(QWidget *parent) : QWidget(parent){
}

void MapRenderer::paintEvent(QPaintEvent *event){
    QPainter p(this);
    p.setPen(QPen(Qt::black, 3));
    p.drawPoint(QPointF(10, 20));
}

Now as I tried compiling this I started getting linker three linker errors that looked like this: 现在,当我尝试编译此链接时,我开始出现以下三个链接器错误:

error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl MapRenderer::metaObject(void)const " (?metaObject@MapRenderer@@UEBAPEBUQMetaObject@@XZ)   G:\Documents\C++ Projects\HistoricalBorders\MapRenderer.obj
error LNK2001: unresolved external symbol "public: virtual void * __cdecl MapRenderer::qt_metacast(char const *)" (?qt_metacast@MapRenderer@@UEAAPEAXPEBD@Z)
error LNK2001: unresolved external symbol "public: virtual int __cdecl MapRenderer::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@MapRenderer@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z)

So I had a look around on the internet and found out that Qt apparently requires a far more complicated build process than a 'normal' c++ project with 'normal' libraries. 因此,我在互联网上浏览了一下,发现Qt显然比带有“正常”库的“正常” c ++项目所需的构建过程复杂得多。 I read of qmake and a Visual Studio Qt Add-In that can be used to create Qt-Projects in Visual Studio. 我阅读了qmake和可用于在Visual Studio中创建Qt项目的Visual Studio Qt加载项。

So I gave it a try and installed the Visual Studio Add-In and specified my Qt-version's root directory in its settings. 因此,我尝试了一下并安装了Visual Studio加载项,并在其设置中指定了Qt版本的根目录。 Afterwards my project still wouldn't compile, but the only error message then was: 之后,我的项目仍然无法编译,但是唯一的错误消息是:

error LNK1112: module machine type 'x64' conflicts with target machine type 'X86' 错误LNK1112:模块机器类型“ x64”与目标机器类型“ X86”冲突

Now I don't really get this, because my Qt library is 64bit and my project configuration also is 64bit as well as all the other libraries I am using. 现在我还不太了解,因为我的Qt库是64位的,而我的项目配置也是64位的,以及我正在使用的所有其他库。 Furthermore, before installing the add-in I didn't have this problem. 此外,在安装外接程序之前,我没有这个问题。 I have to say, my project is a 'normal' Visual Studio C++ console application. 我不得不说,我的项目是一个“普通” Visual Studio C ++控制台应用程序。 I saw that since I installed the VS Add-in it is also possible to choose 'Qt-console application' when creating a new project in Visual Studio. 我看到由于安装了VS加载项,因此在Visual Studio中创建新项目时也可以选择“ Qt控制台应用程序”。 I also gave this a try but it didn't even compile after creating it. 我也尝试了一下,但是创建后甚至没有编译。 I also don't really know what the difference of a "Qt-project" to a normal VS-project is, except that the Qt libraries are included by default. 我也真的不知道“ Qt项目”与普通的VS项目有什么区别,除了默认情况下包含Qt库。

The other thing I have to mention is that I am using cmake to create my VS project file because I use version control (mercurial) and multiple people shall work on the project. 我不得不提到的另一件事是我使用cmake创建我的VS项目文件,因为我使用了版本控制(Mercurial),并且多个人将在该项目上工作。

At the moment I am totally confused on how to get this library to work. 目前,我对如何使该库正常工作完全感到困惑。

Now what is my question? 现在我的问题是什么? Honestly, I don't really know. 老实说,我真的不知道。 What I'm asking myself is: 我问自己:

  • Where does the architecture mismatch linker error come from? 体系结构不匹配链接器错误从何而来?
  • Can I create a normal VS console application and get it to work with Qt? 我可以创建一个普通的VS控制台应用程序并将其与Qt一起使用吗?
  • What's the difference of a 'Qt-console application" to a normal VS project? “ Qt控制台应用程序”与普通VS项目有什么区别?
  • Is it possible to generate a Qt-compatible VS project with cmake? 是否可以使用cmake生成与Qt兼容的VS项目?

EDIT: Now I uninstalled the Add-In again, reinstalled it but am now getting the old linker errors again (no longer the architecture mismatch). 编辑:现在我再次卸载了该加载项,重新安装了它,但是现在又又收到了旧的链接器错误(不再是体系结构不匹配)。 I have no idea why. 我不知道为什么。

EDIT: Ok, some clarification. 编辑:好的,一些澄清。 Yes, I want to use cmake. 是的,我要使用cmake。 I also already have a cmakelists.txt and a findQt.cmake that I wrote myself and that asks you for your Qt root directory and then adds the necessary include paths and library paths to the project. 我也已经有一个cmakelists.txt和一个我自己写的findQt.cmake,它要求您提供Qt根目录,然后向项目添加必要的包含路径和库路径。 This also works so far. 到目前为止,这也有效。 Eg the following code compiles without problems and shows an interface: 例如,以下代码可以毫无问题地编译并显示一个接口:

QApplication app(argc, argv);
QTextEdit *textEdit = new QTextEdit;
QPushButton *quitButton = new QPushButton("&Quit");

QObject::connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(textEdit);
layout->addWidget(quitButton);
QWidget window;
window.setLayout(layout);
window.show();
app.exec();

Now if I try to create a class as shown above (the MapRenderer ) I get the linker errors also shown above. 现在,如果我尝试创建一个如上所示的类( MapRenderer ),我也会得到上面显示的链接器错误。 Now I thought I am doing something wrong. 现在我以为我做错了。 My only question is what. 我唯一的问题是。 From your comment, drescherjm, I see that I problably missed the moc-step (sorry but I have no idea what exactly that is). 从您的评论中,德雷舍姆(Drescherjm),我发现我可能错过了Moc步骤(对不起,但我不知道那是什么意思)。

If you have Qt project file ( .pro ) (or can get it somehow), you can run 如果您有Qt项目文件( .pro )(或可以通过某种方式获取它),则可以运行

cd <pro_folder>
qmake -tp vc 

to (re-)generate Visual Studio project file with Qt build process properly set up, including 通过正确设置的Qt构建过程来(重新)生成Visual Studio项目文件,包括

  • moc (meta object compiler; that's what you are missing now), moc (元对象编译器;这就是您现在所缺少的),
  • uic (processing designer's files) uic (处理设计师的文件)
  • rcc (resource compiler). rcc (资源编译器)。

All generated files will be compiled and linked properly. 所有生成的文件将被正确编译和链接。 You don't even need AddIn for that. 您甚至不需要AddIn。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM