首页 > > > Using Qt with VS.pdf

Using Qt with VS.pdf

Using Qt with VS.pdf

上传者: hpp0hpp 2011-05-07 评分1 评论1 下载26 收藏10 阅读量878 暂无简介 简介 举报

简介:本文档为《Using Qt with VSpdf》,可适用于IT书籍领域,主题内容包含IanDavisUsingQtwithVisualStudioPageofUsingQtwithVisualStudioVersionIanDavi符等。

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 Ian Davis Using Qt 4 with Visual Studio 2005 Page 2 of 34 Table of Contents NOTES....................................................................................................................................................................................................3 CONFIGURING THE ENVIRONMENT.........................................................................................................................................................4 CREATING A PROJECT ............................................................................................................................................................................7 USING THE QT DESIGNER.....................................................................................................................................................................22 Table of Figures FIGURE 1: SELECTING THE OPTIONS MENU ................................................................................................................................................ 5 FIGURE 2: VISUAL STUDIO OPTIONS...........................................................................................................................................................6 FIGURE 3: CREATING A NEW 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: DEBUG NMAKE PROPERTIES....................................................................................................................................................13 FIGURE 10: DEBUG REBUILD ALL COMMAND LINE..................................................................................................................................14 FIGURE 11: RELEASE NMAKE PROPERTIES............................................................................................................................................... 15 FIGURE 12: RELEASE REBUILD ALL COMMAND LINE............................................................................................................................... 16 FIGURE 13: ADDING NEW 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: ADDING NEW 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 HEADER GENERATION ........................................................................................................................ 30 FIGURE 29: RELEASE CONFIGURATION.....................................................................................................................................................31 FIGURE 30: RELEASE REBUILD ALL HEADER GENERATION...................................................................................................................... 31 FIGURE 31: GENERATE HEADERS FROM .UI FILE ...................................................................................................................................... 32 FIGURE 32: ADDING GENERATED HEADER............................................................................................................................................... 33 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.1 I am not using NMake. I am using the mingw32-make that is installed as part of the Qt installation. 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 Install a. Download the Qt 4.0.1 installer. In my case it is qt-win-opensource-4.0.1-mingw.exe b. 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 the chase 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 type “make” 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 (StartRun”cmd”) 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. Build a. 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. Ian Davis Using Qt 4 with Visual Studio 2005 Page 5 of 34 In order for Visual Studio to be able to use code completion, thus making it a worthwhile editor, the Qt headers must be added to it header lookup path. To do this, go to the file menu and select ToolsOptions… Figure 1: Selecting the Options Menu Ian Davis Using Qt 4 with Visual Studio 2005 Page 6 of 34 In the treeview select “Projects and Solutions””VC++ 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 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 the build 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” ”New””Project…” Figure 3: Creating a New Project Ian Davis Using Qt 4 with Visual Studio 2005 Page 8 of 34 In the treeview select “Other Languages””Visual 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 Ian Davis Using Qt 4 with Visual Studio 2005 Page 9 of 34 Click Next. Figure 5: Makefile Application Wizard Screen 1 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:” blank if you wish. When done click on the “Release Configuration Settings” link. Figure 6: Makefile Application Wizard Screen 2 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:” blank if 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 Ian Davis Using Qt 4 with Visual Studio 2005 Page 12 of 34 We must now finish customizing our build properties to that Visual Studio’s build options worked as we would expect. Select “Project” ”Properties”. Figure 8: Selecting Project Properties 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 All Command Line” “…” button. Figure 9: Debug NMake Properties 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 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, notice the Output: Release\<exe_name> - 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 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 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 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 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(); } 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 fun toys. Figure 16: Code Completion in Action 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 if successful. 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 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 not that 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 Visual Studio and select “Project” ”Add Existing Item…” Figure 19: Adding a .ui File 1 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 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 .ui file in the Solution Explorer and right-click on it. Select “Open With…”. Figure 21: Adding New Default Application Ian Davis Using Qt 4 with Visual Studio 2005 Page 25 of 34 Click “Add…” Figure 22: Open With Window 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 the lower box and click OK. Figure 23: Add Program Window 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 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 Qt Designer. 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 Ian Davis Using Qt 4 with Visual Studio 2005 Page 29 of 34 Select “Project” ”Properties” Figure 26: Changing Project Properties 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. For your project it will be : uic <file_name>.ui –o <filename>.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 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 Ian Davis Using Qt 4 with Visual Studio 2005 Page 32 of 34 Select “Build” ”Rebuild <ProjectName>” and your header will be generated. Be care with the uic command as you can destroy the .ui file. Figure 31: Generate Headers from .ui File 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 Ian Davis Using Qt 4 with Visual Studio 2005 Page 34 of 34 Select the header you specified and you can now view it in your project. Notes Configuring the Environment Creating a Project Using the Qt Designer

该用户的其他资料

  • 名称/格式
  • 评分
  • 下载次数
  • 资料大小
  • 上传时间

用户评论(1)

0/200
上传我的资料

相关资料

资料评价:

/ 34
所需积分:1 立即下载
返回
顶部
举报
资料
关闭

温馨提示

感谢您对爱问共享资料的支持,精彩活动将尽快为您呈现,敬请期待!