2
votes

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.

3
Appears to be MOC related. Make sure the MOC is producing a matching moc_.cpp file for this sample. If it is, make sure that moc_.cpp file is added to the project.stinky472
Do you have experience with VS2008-Qt? Do you have any explanation in a few steps?nik_02
Apologies, unfortunately not. I do most of that stuff from the command line so my suggestion would be analogous to Timo's.stinky472

3 Answers

1
votes

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.

1
votes

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.

0
votes

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