using qt 4 with visual studio 2005
TRANSCRIPT
-
7/31/2019 Using Qt 4 With Visual Studio 2005
1/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 1 of 34
Using Qt 4.0 with Visual
Studio 2005
Version 1.5
Ian Davis
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
2/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 2 of 34
Table of ContentsNOTES....................................................................................................................................................................................................3
CONFIGURING THE ENVIRONMENT.........................................................................................................................................................4
CREATING A PROJECT ............................................................................................................................................................................7
USING THE QT DESIGNER.....................................................................................................................................................................22
Table of FiguresFIGURE 1: SELECTING THE OPTIONS MENU ................................................................................................................................................5
FIGURE 2: VISUAL STUDIO OPTIONS...........................................................................................................................................................6
FIGURE 3: CREATING ANEW PROJECT........................................................................................................................................................7
FIGURE 4: SELECTING A C++ MAKEFILE PROJECT......................................................................................................................................8
FIGURE 5: MAKEFILE APPLICATION WIZARD SCREEN 1 .............................................................................................................................9
FIGURE 6: MAKEFILE APPLICATION WIZARD SCREEN 2 ...........................................................................................................................10
FIGURE 7: MAKEFILE APPLICATION WIZARD SCREEN 3 ...........................................................................................................................11
FIGURE 8: SELECTING PROJECT PROPERTIES ............................................................................................................................................12
FIGURE 9: DEBUGNMAKE PROPERTIES....................................................................................................................................................13
FIGURE 10: DEBUG REBUILD ALL COMMAND LINE..................................................................................................................................14
FIGURE 11: RELEASENMAKE PROPERTIES...............................................................................................................................................15
FIGURE 12: RELEASE REBUILD ALL COMMAND LINE...............................................................................................................................16FIGURE 13: ADDINGNEW SOURCE FILE ...................................................................................................................................................17
FIGURE 14: SELECTING C++ SOURCE .......................................................................................................................................................18
FIGURE 15: EDITING THE CODE ................................................................................................................................................................19
FIGURE 16: CODE COMPLETION IN ACTION ..............................................................................................................................................20
FIGURE 17: BUILDING AND RUNNING THE APPLICATION ..........................................................................................................................21
FIGURE 18: RUNNING APPLICATION .........................................................................................................................................................21
FIGURE 19: ADDING A .UI FILE 1...............................................................................................................................................................22
FIGURE 20: ADDING A .UI FILE 2...............................................................................................................................................................23
FIGURE 21: ADDINGNEW DEFAULT APPLICATION ...................................................................................................................................24
FIGURE 22: OPEN WITH WINDOW.............................................................................................................................................................25
FIGURE 23: ADD PROGRAM WINDOW.......................................................................................................................................................26
FIGURE 24: SETTING DEFAULT APPLICATION ...........................................................................................................................................27
FIGURE 25: FINALIZING DEFAULT PROGRAM ...........................................................................................................................................28
FIGURE 26: CHANGING PROJECT PROPERTIES..........................................................................................................................................29
FIGURE 27: DEBUG CONFIGURATION........................................................................................................................................................30
FIGURE 28: DEBUG REBUILD ALL HEADERGENERATION ........................................................................................................................ 30
FIGURE 29: RELEASE CONFIGURATION.....................................................................................................................................................31
FIGURE 30: RELEASE REBUILD ALL HEADERGENERATION......................................................................................................................31
FIGURE 31: GENERATE HEADERS FROM .UI FILE ......................................................................................................................................32
FIGURE 32: ADDING GENERATED HEADER...............................................................................................................................................33
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
3/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 3 of 34
Notes
Important areas of a screen shot are highlighted in a red square.
I am using the Professional version of Visual Studio 2005 and the Open Source version of Qt 4.0.1I am not using NMake. I am using the mingw32-make that is installed as part of the Qt installation.
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
4/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 4 of 34
Configuring the Environment
In order to have a usable environment for Qt for, a couple of steps must be taken:1. Configure Qt
2. Configure Visual Studio 2005
Configuring Qt
1. Download and Installa. Download the Qt 4.0.1 installer. In my case it is qt-win-opensource-4.0.1-mingw.exeb. Run the installer and make sure to download and install mingw.
c. Once the installers are finished, reboot your computer.
d. In this step you can also refer to C:\Qt\4.0.1\doc\html\install-win.html as I am cutting to thechase and you may need to follow a step in their document (i.e. for licensing as I am using the
Open Source version). I am assuming at this point that you are going to use mingw32-make
instead of NMake. Qt creates a batch file that wraps mingw32-make and will run if you typemake in the command prompt. The Qt installer added it to your PATH so you should not need
to worry about it.
e. Now that your system is back up, open a command prompt (StartRuncmd) and browse to
your Qt install dir (C:\Qt\4.0.1).2. Configure
a. Run C:\Qt\4.0.1>configure.exe (This can take a long time, find something else to do)
3. Builda. Run C:\Qt\4.0.1>make (This will take a very long time. Go out to dinner or something).
4. At this point Qt should be usable. If you tried to write a program before following these steps you were
probably very frustrated. Reboot your computer and continue on to the next section.
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
5/34
-
7/31/2019 Using Qt 4 With Visual Studio 2005
6/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 6 of 34
In the treeview select Projects and SolutionsVC++ Directories. In the Show directories for: dropdown,select Include files and then click on the new folder button. Click on the button and browse to your Qt
install directory. This path should have no spaces. Once done, click the OK button. The environment for Visual
Studio should now be set up completely.
Figure 2: Visual Studio Options
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
7/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 7 of 34
Creating a Project
In order to use Qt in Visual Studio 2005 we must use VC++ projects, but more than that we must control thebuild system with makefiles. For a beginner I have set up a few item in the guide to suite their needs. I will note
them as a step is encountered.
After opening Visual Studio 2005, select File NewProject
Figure 3: Creating a New Project
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
8/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 8 of 34
In the treeview select Other LanguagesVisual C++ and then select Makefile Project in the listview to
the right. Enter your project name and location and click OK.
Figure 4: Selecting a C++ Makefile Project
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
9/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 9 of 34
Click Next.
Figure 5: Makefile Application Wizard Screen 1
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
10/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 10 of 34
Change all fields so that they look like the ones presented below. You can leave Rebuild command line: blankif you wish. When done click on the Release Configuration Settings link.
Figure 6: Makefile Application Wizard Screen 2
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
11/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 11 of 34
Change all fields so that they look like the ones presented below. You can leave Rebuild command line: blankif you wish. You will have to uncheck the Same as debug configuration check box to change these settings.
When done, click Finish.
Figure 7: Makefile Application Wizard Screen 3
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
12/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 12 of 34
We must now finish customizing our build properties to that Visual Studios build options worked as we wouldexpect. Select Project Properties.
Figure 8: Selecting Project Properties
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
13/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 13 of 34
Select the Debug Configuration and choose NMake from the treeview on the left. Click on the Rebuild AllCommand Line button.
Figure 9: Debug NMake Properties
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
14/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 14 of 34
Enter the following lines:qmake project
qmake
make clean
make debug
The first two lines are for beginners who do not want to create their own makefiles. These lines will create the
project file, add all needed sources to the build list, and create platform specific makefiles. In the last line we
tell make to create the debug build which is output to Debug\ which follows the settings we entered before.
Figure 10: Debug Rebuild All Command Line
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
15/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 15 of 34
Now that the Debug Configuration is complete we must repeat it for the Release Configuration. Again, noticethe Output: Release\ - this tells Visual Studio what to execute on a successful build. The mingw32-
make automatically puts the built files in the \Debug or Release\ folders based on your make parameters. We
are just telling Visual Studio about this convention.
Figure 11: Release NMake Properties
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
16/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 16 of 34
Again we add the four lines, but we add release instead of debug to the make command.
Figure 12: Release Rebuild All Command Line
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
17/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 17 of 34
Now we can add a source file. Select Project Add New Item
Figure 13: Adding New Source File
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
18/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 18 of 34
Select Visual C++Code in the treeview and then C++ file (.cpp) from the list. Enter your file name and
click Add.
Figure 14: Selecting C++ Source
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
19/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 19 of 34
Enter your source code. I have provided a very basic example below.
Figure 15: Editing the Code
int main(int argc, char *argv[])
{QApplication app(argc, argv);QPushButton hello("Hello World!");hello.resize(100, 30);QObject::connect(&hello, SIGNAL(clicked()),
&app, SLOT(quit()));hello.show();
return app.exec();}
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
20/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 20 of 34
Remember when we configured the include path? Because of that we have code completion and a few other funtoys.
Figure 16: Code Completion in Action
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
21/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 21 of 34
After entering your code you can click the Start Debugging arrow and your application will build and run ifsuccessful. All errors will be available in the error tab at the bottom of the IDE just as with any other project
that is native to Visual Studio. You will likely get a warning about the debug target. Select to ignore this
warning from now on.
Figure 17: Building and Running the Application
And here is the sample application running.
Figure 18: Running Application
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
22/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 22 of 34
Using the Qt Designer
One key feature that is needed is to be able to use and Qt Designer and Visual Studio. But how? It is really notthat difficult and I will take you through step-by-step.
Create a .ui file in the Qt Designer and then save it in your Visual Studio project folder. Now switch to VisualStudio and select Project Add Existing Item
Figure 19: Adding a .ui File 1
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
23/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 23 of 34
Change the Files of type: to display All Files (*.*) and then choose your .ui file and click Add.
Figure 20: Adding a .ui File 2
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
24/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 24 of 34
You must follow the next five steps for the first time you have added a .ui file to your project. Locate your .uifile in the Solution Explorer and right-click on it. Select Open With.
Figure 21: Adding New Default Application
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
25/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 25 of 34
Click Add
Figure 22: Open With Window
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
26/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 26 of 34
Browse to your Qt installation bin directory and paste its location into the top box. Enter Qt Designer in thelower box and click OK.
Figure 23: Add Program Window
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
27/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 27 of 34
Select Qt Designer and click Set as Default.
Figure 24: Setting Default Application
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
28/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 28 of 34
Click OK. Now, every time you double-click on a .ui file in the Solution Explorer it will open the file in the QtDesigner. We are however, left with a problem. There is a .ui file, but no .h file that our application needs. Also,
what happens if we update the .ui file? These issues are taken care in the next steps.
Figure 25: Finalizing Default Program
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
29/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 29 of 34
Select Project Properties
Figure 26: Changing Project Properties
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
30/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 30 of 34
Select the Debug Configuration and open the Rebuild Command Line item.
Figure 27: Debug Configuration
We now add a new line to the top of the batch list. uic Qt4VS2005.ui o qt4vs2005.h is added so that every
time we change our .ui file, all we have to do is perform a Rebuild All and the headers will be regenerated. Foryour project it will be :
uic .ui o .h
You can do this for every .ui file you have. We must also do this for the Release Configuration.
Figure 28: Debug Rebuild All Header Generation
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
31/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 31 of 34
Select the Release Configuration and open the Rebuild Command Line item. And mirror the previous step
Figure 29: Release Configuration
Figure 30: Release Rebuild All Header Generation
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
32/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 32 of 34
Select Build Rebuild and your header will be generated. Be care with the uic commandas you can destroy the .ui file.
Figure 31: Generate Headers from .ui File
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
33/34
Ian Davis Using Qt 4 with Visual Studio 2005 Page 33 of 34
Now we can add the new header to our solution. Select Project Add Existing Item
Figure 32: Adding Generated Header
http://www.bcltechnologies.com/easypdf/http://www.bcltechnologies.com/easypdf/ -
7/31/2019 Using Qt 4 With Visual Studio 2005
34/34
Select the header you specified and you can now view it in your project.