Results 1 to 7 of 7

Thread: Set up the Qt4.3.2 with Visual Studio 2005

  1. #1
    Join Date
    Nov 2007
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Set up the Qt4.3.2 with Visual Studio 2005

    I cost about a week to set up Qt4 integrated with Visual 2005, and I find that English Documents about this topic are out of time. There is a piece of German Document ("QT 4.x unter Windows und Visual Studio 2005") is very good. so I tanslate part of it into English. So, I am glad to share it with you.
    QT 4.3.2 with Visual Studio 2005
    1. Create a new Project
    “File -> New -> Project ...”, then choose “Other Project Types -> Visual Studio Solutions -> Blank Solution” and set the solution name and location.
    After creating the solution, open the Solution Explorer , choose and click right mouse button the Solution, click Add -> New Project ... in the pop-menu. In the opening dialog choose “Visual C++ -> Win32 -> Win32 Console Application”, and then set the project name. In the opening dialog, click “Next” or “Application Settings”, and choose “Console application” and “Empty project”, the click “Finish” after that you create an empty project.
    2. Add a “cpp” file to the project
    Click the right mouse button on the sub-folder Source Files and click “Add -> New Item”. In the following dialog, choose “Visual C++ -> Code -> C++ File (.cpp)” and set the file name. In this example, we set it “main.cpp”. In the “Solution Explorer -> Source Files”, you can will find a new file named “main.cpp”
    3. Use the QT Designer
    Open the “QT Designer”, and choose “File -> New Form”. In the opening dialog , choose “Main Window”. In the new window in the edit area, double-click on the existing record “Type Here”. When it become editable, type “&File” and then enter key. Using the same method add a new entry “&Exit” to the “File” menu. Now turn to the “Properties Editor” and rename the item we added. Set the QMenuBar, “File” menu (QMenu), “Exit” menu (QAction) as “m_menubar”, “m_menu_file”, “m_action_quit”, respectively.

    Click the “plus icon” in the “Signal/Slot Editor”. Then choose the “Sender” as QAction object “m_action_quit”, the “Signal” as “triggered()”, “Slot” as “MainWindow” and “Slot” as “close()”.
    You can change the title of the window in the property “windowTitle” of the window. When you storing, remember that the file name should be the same as the name of the window.
    4. Use the UI file in Visual Studio 2005
    You should add the ui file to the project before you use it. Click right mouse button on the project, this time, choose “Add -> Existing Item”. In the opening dialog, the “Files of Type” must be set as “All Files (*.*)”. Then choose the UI file previously saved and add it to the project.

    When such a UI file is added for the first time, you get a message appears that “A custom build rule to build files with extension ‘*.ui’ could not be found.” Click the “Yes” button and set the dialog. In this example, we set it as:

    Display Name:
    “QT Build Rule”
    File Name
    “qt_rules”
    Directory
    “C:\Qt\4.3.2\VS2005” (the location the build rule file saved)

    Then insert a new build rule by clicking the “Add Build Rule…” and set the property as following:
    • Command Line:
    $(QTDIR)/bin/uic.exe "$(InputPath)" -o ui_$(InputName).h
    • Display Name
    Compile QT UserInterface File (*.ui)
    • Execution Description:
    Creating UI File ui\_\$(InputName).h
    • File Extensions:
    *.ui
    • Name:
    UI
    • Outputs:
    ui_$(InputName).h

    Click the “Ok” button to save the settings. Now, click the right mouse button on the UI File (this example is MainWindow.ui) and click the “Compile” entry of the pop-menu. So, the UI Compiler will produce a new header file of the source code implementing the window class of the UI file created by the QT Designer.
    5. Create the main class
    Add two new files, “MainWindow.cpp” and “MainWindow.h” to the project. So the project has the following files:

    1. Main.cpp
    2. MainWindow.ui
    3. MainWindow.h
    4. MainWindow.cpp

    The following is the source code in the MainWindow.h file:
    ------------------------------------------------------------------------------------------------------
    /***********************MainWindows.h************** **********/
    // to avoid multiple class definitions by including this file more than once
    // we have to surround the class definition by this compiler flag
    #ifndef MAINWINDOW_H_
    #define MAINWINDOW_H_
    #include "Ui_MainWindow.h"
    /**
    * Sample MainWindow Class
    * The class is a simple implementation of a QDesigner created form file
    * defining a simple QMainWindow application
    * The class inherits from QMainWindow and Ui_MainWindow. The Ui_MainWindow
    * provides the QDesigner part of the implementation, while the QMainWindow
    * provides the main functions of a QT Application
    */
    class MainWindow : public QMainWindow, protected Ui_MainWindow
    {
    Q_OBJECT
    public:
    /**
    * Constructor of the MainWindow class
    * @param parent this optional parameter defines a parent widget the
    created instance will be child of
    * @param flags optional parameter defining extra widget options
    (see also the QT documentation)
    */
    MainWindow(QWidget* parent = 0, Qt::WindowFlags flags = 0);
    /**
    Destructor of the MainWindow class, defined virtual to guarantee that
    the destructor will be called even if the instance of this class is
    saved in a variable of a parent class type
    */
    virtual ~MainWindow();
    };
    #endif // end of #ifndef MAINWINDOW_H_
    ------------------------------------------------------------------------------------------------------
    The source code for the MainWindow.cpp file is:
    ------------------------------------------------------------------------------------------------------
    /***********************MainWindows.cpp************ ************/
    #include "MainWindow.h"
    MainWindow::MainWindow(QWidget* parent /* = 0 */, Qt::WindowFlags flags /* = 0 */)
    : QMainWindow(parent, flags)
    {
    // create gui elements defined in the Ui_MainWindow class
    setupUi(this);
    }
    MainWindow::~MainWindow()
    {
    // no need to delete child widgets, QT does it all for us
    }
    ----------------------------------------------------------------------------------------------------------------
    The Source code for the main.cpp:
    -------------------------------------------------------------------------------------------------------
    /***********************main.cpp******************* *****/
    // Header file to get a new instance of QApplication
    #include <Qt/qapplication.h>
    // Header file for MainWindow class
    #include "MainWindow.h"
    int main(int argc, char* argv[])
    {
    // create a new instance of QApplication with params argc and argv
    QApplication app( argc, argv );
    // create a new instance of MainWindow with no parent widget
    MainWindow mainWindow(0, Qt::Window);
    // Shows the widget and its child widgets.
    mainWindow.show();
    // Enters the main event loop and waits until exit() is called
    // or the main widget is destroyed or by default the last window closed,
    // and returns the value that was set via to exit()
    // (which is 0 if exit() is called via quit()).
    return app.exec();
    }
    -------------------------------------------------------------------------------------------------------


    6. Set environment variables and Linker
    Click the right button on project and choose “Properties”.
    First we need t o set the environment. Then choose the
    “Configuration Properties -> All Configurations -> C/C++ -> General” and set the property “Additional Include Directories” as “$(QTDIR)/include”.
    Now, we should set the Linker. Choose the “Linker -> Input” and then set “Additional Dependencies”.
    In the case of “Debug” Configuration, it’s set as the following:
    “qtmaind.lib QtGuid4.lib QtCored4.lib”
    And in the case of “Release” Configuration, it’s set as the following:
    “qtmain.lib QtGui4.lib QtCore4.lib”
    At last set the “Linker -> General -> Additional Library Directories” in the case of “All Configurations” as “$(QTDIR)/lib”.

    7. Set The Meta-Object-compiler of QT
    Custom the build step of the header file (in this example is MainWindow.h). Right click on the mouse button, and click the menu entry “Properties”. Then choose “Configuration Properties -> Custom Build Step” and set the “Configuration” as “All Configurations”. Now, set the fields as following:
    • Command Line:

    $(QTDIR)\bin\moc.exe "$(InputPath)" -o "$(InputDir)moc_$(InputName).cpp"

    • Description:

    Performing moc on $(InputName).h

    • Outputs:

    $(InputDir)moc_$(InputName).cpp

    Now, right-clicking on the header file MainWindow.h can now select “Compile” then Meta Object Compiler does his work. Then add the “moc_MainWindow.cpp” file produced by MOC to the project (right click on the project and select “Add -> Existing Item…”, choose the file to add to the project)
    Well the following files could be found in the project:
    1. main.cpp
    2. MainWindow.ui
    3. MainWindow.h
    4. MainWindow.cpp
    5. moc_MainWindow.cpp

    Compile and execute the project. Enjoy it.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Set up the Qt4.3.2 with Visual Studio 2005

    Very good!
    It would be a good idea if you'd post it in the wiki.

  3. #3
    Join Date
    Nov 2006
    Location
    Shrewsbury, UK
    Posts
    97
    Thanks
    3
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Set up the Qt4.3.2 with Visual Studio 2005

    ..or just write the code (.cpp, .h, .ui) in a folder and run
    qmake -project
    qmake -tp vc

    and open the resulting .vcproj file with VS2005

    Pete

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Set up the Qt4.3.2 with Visual Studio 2005

    Well, I just installed MSVC 2005 express, and compiled Qt 4.3.2 OS, all went smoothly (followed advice on this forum, wiki).
    However, all though I manage to create a .vcproj project with:
    qmake -project
    qmake -tp vc
    it doesn't moc, it only compiles "normally".

    Am I right to assume that moc *should* be supported if all is correctly setup, or do open source users need to do this manually and add the mocs to the project?
    (not Qt options in "tools->options".)

    If this should be supported (I guess through the makefile), any idea what I might have been doing wrong?(points that I should check)
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Set up the Qt4.3.2 with Visual Studio 2005

    got it.
    I forgot to add Q_OBJECT in my test classes....
    So qmake didn't generate the moc rule.
    Being impatient is not good...
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Oct 2007
    Posts
    27
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Set up the Qt4.3.2 with Visual Studio 2005

    Hello,

    I did all that stuff except moc, but I do this manually with qmake -project and qmake -tp vc. I have a linking problem. Linker could not find included libraries (qtmain.lib QtGui4.lib QtCore4.lib) because I don't have it in my qt\4.2.0\lib folder. I have *.a libraries. How can I make *.lib libraries for my Qt?

    Thanks

  7. #7
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Set up the Qt4.3.2 with Visual Studio 2005

    You have to compile Qt for msvc because there're no binary packages for msvc available. You downloaded the binary package for mingw.

    Search the forum archives - this was discussed very very often!

Similar Threads

  1. Qt configure with msvc.net
    By jivanr in forum Installation and Deployment
    Replies: 1
    Last Post: 11th June 2007, 09:17
  2. Qt Cryptographic Architecture
    By vermarajeev in forum Qt Programming
    Replies: 6
    Last Post: 9th February 2007, 14:15
  3. Qt-4.2.2 qmake won't compile under visual studio 2005 on vista
    By moowy in forum Installation and Deployment
    Replies: 7
    Last Post: 13th January 2007, 22:06
  4. Qt 4.1.0 on Visual Studio 2005
    By nErnie in forum Installation and Deployment
    Replies: 8
    Last Post: 6th July 2006, 01:56
  5. Qt Designer & Visual Studio 2005
    By pSiCho in forum Qt Tools
    Replies: 6
    Last Post: 9th February 2006, 14:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.