views:

96

answers:

3

sys info : win xp SP3 , Microsoft Visual Studio 2008 Version 9.0.21022.8 RTM

Microsoft .NET Framework Version 3.5 SP1

Qt Add-in 1.1.5

I installed Qt 4.6.3 from the site http://qt.nokia.com/downloads/windows-cpp-vs2008. Then I added the Add-in Qt 1.1.5 and configured the PATH variable.

When I open a new QT project , default example works just fine.

On Nokia (qt) site I found some examples but it seems that things are not working properly.

Here is one of many examples that do not work :

#include <QtGui>
#include <QWidget>

 class QLabel;
 class QLineEdit; 
 class QTextEdit;


class AddressBook : public QWidget
 {
     Q_OBJECT

 public:
     AddressBook(QWidget *parent = 0);

 private:
     QLineEdit *nameLine;
     QTextEdit *addressText;
 };

AddressBook::AddressBook(QWidget *parent)
     : QWidget(parent)
 {
     QLabel *nameLabel = new QLabel(tr("Name:"));
     nameLine = new QLineEdit;

     QLabel *addressLabel = new QLabel(tr("Address:"));
     addressText = new QTextEdit;

     QGridLayout *mainLayout = new QGridLayout;
     mainLayout->addWidget(nameLabel, 0, 0);
     mainLayout->addWidget(nameLine, 0, 1);
     mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop);
     mainLayout->addWidget(addressText, 1, 1);

     setLayout(mainLayout);
     setWindowTitle(tr("Simple Address Book"));
 }

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

     AddressBook addressBook;
     addressBook.show();

     return app.exec();
}

Compiler says this ::

Output Window

Linking...

main.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall AddressBook::metaObject(void)const " (?metaObject@AddressBook@@UBEPBUQMetaObject@@XZ)

main.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall AddressBook::qt_metacast(char const *)" (?qt_metacast@AddressBook@@UAEPAXPBD@Z)

main.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall AddressBook::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@AddressBook@@UAEHW4Call@QMetaObject@@HPAPAX@Z)

main.obj : error LNK2001: unresolved external symbol "public: static struct QMetaObject const AddressBook::staticMetaObject" (?staticMetaObject@AddressBook@@2UQMetaObject@@B)

C:\Documents and Settings\nik\My Documents\Visual Studio 2008\Projects\vs_03\Debug\vs_03.exe : fatal error LNK1120: 4 unresolved externals

Results

Build log was saved at "file://c:\Documents and Settings\nik\My Documents\Visual Studio 2008\Projects\vs_03\vs_03\Debug\BuildLog.htm" vs_03 - 5 error(s), 0 warning(s)

It seems to me that the thing has to do with the use of macro Q_OBJECT but just dont know what to do that thing starts to work properly.

Maybe wrong installation or ... NO IDEA

Any help is appreciated.

+1  A: 

I don't think it's a wrong installation - I assume you're using Visual Studio to build the project and in that case, you also need to tell it to build the _moc.cpp files that should have been generated for your class AddressBook.

If they haven't been generated you also need to run moc on the header files for AddressBook.

Timo Geusch
From site : http://dcsoft.wordpress.com/?aspxerrorpath=/community_server/blogs/dcsoft/archive/2009/03/06/how-to-setup-qt-4-5-visual-studio-integration.aspx1. Download and install the Qt Visual Studio Add-in to install the Qt plug-in into Visual Studio (both 2005 and 2008 are supported by the add-in).2. Start Visual Studio.3. Select menu Qt | Configure Qt Versions. Add c:\qt\4.6.3 .4. Now Qt is fully functional, and you can use VS2005/2008 to build Qt apps.It seems to me that it is done by VS automatically by this as it says. NOT SURE !
nik_02
@nik_02 - the errors you are seeing are the typical ones that you get when the _moc.cpp files aren't generated or compiled. I use VS2008 to build QT apps but I'm not using the plugin so things might be slightly different. Nevertheless you need to check that these files are generated and compiled to get rid of the link error.
Timo Geusch
OK , thanks for help.
nik_02
A: 

I find solution.

Read all the details about the installation on this page >>

http://dcsoft.wordpress.com/?aspxerrorpath=/community_server/blogs/dcsoft/archive/2009/03/06/how-to-setup-qt-4-5-visual-studio-integration.aspx.

After a whole day of studying and configuration, I finally managed to enable QT 4.6.3. on the VS 2008. Follow the detailed instructions and there should not be a problem.

My problem was that I used the following options:

Starting with Qt 4.6, the LPGL version now comes pre-built for Visual Studio 2008 RTM. If this fits your need, you can simply install it and skip to INSTALL VISUAL STUDIO ADD-IN.

This was wrong in my case so I go to next chapter :

DOWNLOAD QT SOURCE CODE

As the option to download only the Qt source code is a bit obfuscated on the current Nokia website, please follow these directions: ................................................................................................................................................................................................................................. etc. READ ON THE SITE.

For now all works great.There are no errors in linking. MOC works fine ...

nik_02
+1  A: 

Any time you need moc to run against your files and you don't have your class in a separate header and implementation file you need to have a #include "FILENAME.moc" at the end of your file. If you were to add that after your main function, everything should work.

You should be able to test this by going into your project directory and doing the following:

  • Delete any makefiles that may be present
  • Run the Visual Studio Command Prompt
    • Run qmake -project to generate a project file
    • Run qmake to generate the makefiles
    • Run nmake to build the project

The nmake command should completely successfully and without linking errors on a simple project like the above. For more complex projects, you might need to modify the .pro file to include Qt`s webkit or otherwise make accessible options which are not available by default.

The alternative is to move the class definition for AddressBook into a header file with an appropriate implementation (cpp/cxx) file.

Kaleb Pederson
Thanks.In the meantime I made the installation of Qt from (build your own) version and everything works as expected. It was my fault because I was originally use a pre-built version of Qt.
nik_02
I've always used the pre-built (commercial) version of Qt without problems. As long as the pre-built version matches up with your compiler (i.e. MSVC vs. GCC) you shouldn't have any problems.
Kaleb Pederson