Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Skills of using Qt Creator

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Qt Creator is Qt's official IDE, and this IDE provides a complete development environment for Qt programmers. Of course, this IDE is written in Qt and is free. The real compilation part of this IDE uses MinGW gcc compiler. In other words, the main purpose of this IDE is to help developers write code and use MinGW in a more friendly graphical interface (rather than the command line). Below I would like to take MS Visual Studio as a reference to talk about some experience and tips on the use of Qt Creator on the Windows platform.

1. Engineering documents and project organization structure

VC6 used to use dsw files (followed by sln) to manage software projects (the new version of VC provides dsw conversion tools), and there can be many dsp, or project files, in each dsw. Each dsp can contain as many .h.cpp.rc files as you want, and each dsp can be compiled separately. Can be compiled to exe or dll or ocx and so on. All dsp in a dsw can also be compiled together, and dependencies can be set between each dsp. For example, one dsp is called baseui and the other is called myapp. Where baseui will be compiled into baseui.dll and myapp will be compiled into myapp.exe. If you need to use a class or a resource or a function in baseui in myapp, then baseui is needed in the dependency of myapp. After the dependency is set, baseui compiles ahead of myapp before each overall compilation, thus ensuring that the lib required by myapp is already available before compilation.

In Qt Creator, the organization file of the project is of type pro, which does not have a level 2 structure like dsw and dsp. Like dsw or dsp files, it is also a machine-generated text file. However, in Qt Creator, you may often need to edit this file manually. Here is a simple pro file:

TARGET = Test112

TEMPLATE = app

SOURCES + = main.cpp/

Mainwindow.cpp

HEADERS + = mainwindow.h

FORMS + = mainwindow.ui

Line 1 "TARGET" defines the name of the target file

Line 2 "TEMPLATE" defines the type of the target file, and the app executable file. Lib is a library file (either dll, statically linked libraries or plugin)

All cpp files are defined in "SOURCES"

All the h files are defined in "HEADERS"

All ui files are defined in "FORMS" (interface classes that can be visually edited in Qt Creator)

Here, after qmake, the ui file will generate a .h file at the beginning of ui_, in which the program will automatically generate a new UI class, in which the interface elements of visual editing will be written in code. In the class corresponding to the ui file, the automatically generated ui class is used by a new instance of new to assist the developer.

I don't know if I've made myself clear, but the ui file will help developers generate some interface-related code, but this code is used for the original interface class in the form of member variables and code of another new class. (sweat, I'm tired.) I never understood why Qt Creator didn't directly modify the code of the original class like VC did. Is this difficult to achieve? I opened the .ui file and saw that it was actually a xml file description of the interface. Forget to see where this implementation has an advantage is that if the program interface is flexible enough, if the program upgrade only involves the interface, then you can just download this ui file to upgrade. This feature may make saas software developers ecstatic. But for most desktop app developers, it's still a little confusing. Fortunately, there is signal/slot in Qt that can be used to pass messages between widget and interface classes (such as dialog), otherwise the implementation of adding a layer of "fake parent class" in the middle is unacceptable.

What the Pro file says is enough to show that Qt Creator is not particularly friendly in project management and setup. There is a big gap compared with VC (or even VC6). Editing the pro file reminds me of writing turbo C programs on the command line N years ago. Hehe, maybe open source software is just like this?

There is still a lot for developers to learn about pro files. I will write a special tip.

2. IDE interface

VC's interface is classic and hasn't changed fundamentally for years (but I'm sure many older VCers still resent the cancellation of Class Wizard in later versions of VC6).

The Qt Creator interface is relatively general. The hard wound is that there is no Class Tree, which is very inconvenient for OOP. Because there is no class tree, the developer cannot code the class, which means that adding a member function requires manually entering the function name twice in the .h and .cpp files of the class. At the same time, it takes more time to overload a function than it does in VC. Although the time to add a function may only increase by 5-10 seconds, it may be frustrating for programmers who have just had a flash of inspiration. I hope the later version can be added.

The code input interface Qt Creator has done a good job, and syntax coloring (not only the coloring of Standard C++, but also the coloring of Qt-specific keywords, such as SLOT, etc.) and intellisense features like VC (code completion) feel very comfortable. I remember watching VC Team's BLOG some time ago and someone left a message saying that intellisense had a lot of problems. I haven't found bug in the two months I've been using Qt Creator. But I have always thought that the function of Visual Assist is the most professional. Please raise your hand if you are using VA.

3. Compile

In terms of compilation speed, VC wins completely. More than a little faster. Of course, qmake itself will take up a little more time, but you can still clearly feel that VC is much faster.

In terms of the quality of compiled code, I am a layman and dare not jump to conclusions. But I feel that the exe file generated by Qt will be larger, and I don't know if it's a sacrifice for performance. So when writing code in TX, don't try to save trouble and include the entire Qt module (such as QtGUI).

4. Debug

I have to say, VC won again. Qt Creator is not only slow, but also often has some inexplicable problems. You may be asked to re-build Debugging Helper. Gdb crash may also appear.

5. Help

MSDN won't say much. Qt has a special Qt Assist program to provide all the help. There is also a help module in Qt Creator itself. On the network, Qt central provides forum and wiki. However, if you encounter problems with programming, you will find that far fewer people use Qt than VC. Maybe some of the minor problems in VC have long been encountered by others and published on the Internet. On the other hand, it may be difficult to find answers to the problems you encounter in Qt. TX users of Qt should also strive to enrich the open source community.

6. How to choose

If you only use Qt to develop on Windows, you can choose VC2008. The Express version is also free.

If you want to develop Cross-platform programs, you should choose Qt Creator. Because it runs on Windows, Linux and Mac (MinGW is also Cross-platform). Therefore, in terms of platform migration and maintenance, Qt Creator may be a better choice. In addition, my personal experience is that from VC6 to 2008, I often encounter an inexplicable exit from VC, and sometimes the code has not been saved. During the period of using Qt Creator, this has never happened. I believe this also proves from the side that Qt itself is very robust.

Some tip using Qt Creator

1. Compilation does not pass after using modules such as network or opengl or sql

Write the following statement in the pro file: QT + = network or QT + = opengl or QT + = sql

two。 Code completion is case sensitive

In the Tools- > Options menu, in Text Editor- > Completion, check "Case-sensitive completion" so that the entire completion will not disappear because you have the wrong case.

3. There is no debug helper when Debug.

As shown in the figure below, click Rebuild in the location of the red circle in the Options menu.

4. Pictures have been added to Qrc, but they can not be used in the program.

Resource files used by Qt programs can be added to the Qrc file. These files are compiled into exe files. When adding resources, you need to increase the prefix prefix first. See if your prefix is correct. If the prefix is "/", then a typical image file path might be ": / Resources/Images/aaa.png". In addition, in qrc, file names and pathnames are case-sensitive. I have been stuck here for several hours, and finally I wonder if Qt Creator is a parallel import.

5. Qt Creator how to link the dynamic link library generated by VC in Windows system

This problem has been bothering me all day. What I want is to follow the method in VC, add the include file, add the lib file, and then compile. Who knows, there is always a problem with the link. The prompt is undefined reference to XXXXX. After checking the manual and the Internet, it turns out that this is a common problem encountered by mingw users on windows, and there seems to be no perfect solution so far. The cause of this error is that the name of the _ stdcall function generated by VC is not consistent with that generated by mingw. One solution is to use tools such as libdll to generate a new lib for mingw, and the other is to recompile (sweat) all the code.

In the end, I don't know why I tried a new method, which has not yet been technically found. It hasn't been extensively tested. If you are interested, TX can try it.

If you want to link to an abc.lib file, the header file is abc.h and the dynamic link library is abc.dll. Then add the following line to the pro file first

INCLUDEPATH + = D:/Qt/include

INCLUDEPATH is a compilation variable that stores all the paths that contain the include file, and the abc.h file is saved in this directory. Then add the following line:

LIBS + = D:/Qt/bin/abc.dll

LIBS is also a compilation variable that stores all the library files that need to be linked. Look, it's not "abc.lib", it's "abc.dll". Amazing, so that the link can be successful (lib files can be deleted). TX, who likes to study, can study exactly how mingw does it.

6. The Qt project failed to compile after changing the file directory

Chinese pathnames are not supported when Qt Creator compiles. For paths with spaces, if you want to add to the pro file, you need to use $$quote to specify, such as: $$quote (C:/mylibs/extra libs/extra.lib). In addition, if you are used to'/ 'under Windows, you'd better change your habit and use' /'in Qt.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report