bweber
bweber

Reputation: 4082

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. 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. 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. 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:

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. I read of qmake and a Visual Studio Qt Add-In that can be used to create Qt-Projects in Visual Studio.

So I gave it a try and installed the Visual Studio Add-In and specified my Qt-version's root directory in its settings. 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'

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. 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. 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. 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.

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.

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:

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. 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. This also works so far. E.g. 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. 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).

Upvotes: 1

Views: 969

Answers (1)

Drop
Drop

Reputation: 13005

If you have Qt project file (.pro) (or can get it somehow), you can run

cd <pro_folder>
qmake -tp vc 

to (re-)generate Visual Studio project file with Qt build process properly set up, including

  • moc (meta object compiler; that's what you are missing now),
  • uic (processing designer's files)
  • rcc (resource compiler).

All generated files will be compiled and linked properly. You don't even need AddIn for that.

Upvotes: 1

Related Questions