Qt Learning Route 2 (7): MainWindow Introduction

Source: Internet
Author: User

Blog reprinted from: https://www.devbean.net/2012/08/qt-study-road-2-mainwindow/

The previous article roughly introduces the content of the QT modules, with the aim of a strategically advantageous position-like understanding of the QT framework. From now on, we'll start experimenting with Qt to start a new journey. Since we have introduced the relevant contents of the signal slots in more detail, we can start further learning with a new program and have a more in-depth understanding of the signal slots.

QMainWindowis a pre-defined main window class that is brought by the QT framework. The so-called main window, is a common sense of the application (not the game or the like) the topmost window. such as the browser you are using now, the main window is the browser window. Try to recall the classic main window, usually by a title bar, a menu bar, several toolbars, and a taskbar. Between these subcomponents is our workspace. In fact, it QMainWindow is such a layout.

Here we create a new project. Remember when you were building a new project, Qt Creator would usually help us create one? MainWindow We have modified the function in order to introduce the signal slot main() . Now we use the Qt Creator generated code directly to compile and run it:

#include <QApplication>"mainwindow.h"int main (int Char* argv[]) {    qapplication app (argc, argv);    MainWindow win;    Win.show ();     return app.exec ();}

Running results in Windows are as follows

Let's take a closer look at this window. Although not obvious, it is actually divided into several parts:

The top of the main window is the window title, the title bar, which is typically used to display headings and control buttons such as Maximize, minimize, and close. Typically, each graphical interface framework uses the operating system's native code to generate a window. So, you'll see that in KDE, the title bar of the main window is KDE style, and the title bar is Windows-style on the Windows platform. If you do not like the local style, such as QQ, it is actually the title bar to draw out, this technology is called Directui, that is, no handle drawing, this is not in the scope of this article. Window Title Below is the menu bar, which is the menus bar, which is used to display menus. At the bottom of the window is the status bar, which is referred to as state bars. When we mouse over certain components, you can display some information in the status bar, such as the browser, the mouse over the text with the link, you will see the actual URL of the link at the bottom.

Except for the three horizontal bars above, the middle is represented by a rectangular area. As we can see, the outermost layer, called the tool bar area, is used to display the toolbar areas. The reason for this is the rectangle, because the main window of Qt supports multiple toolbars. You can drag and drop the toolbar to a different location, so it's said to be area. We can display a few toolbars side-by-side here, just like Word2003, or you can place them separately, like Photoshop. Inside the toolbar area is the Dock widget region, which is the display area of the docking window. The so-called docking window, like the Photoshop Toolbox, can be docked in the main window for four weeks, or it can be displayed floating. The main window, called the central Widget, is the workspace of our program. Usually we will place the main work area of the program here, like Word's paper or Photoshop canvas and so on.

For the general Qt application, all we need to do is write our main window code, mainly to add a variety of components, such as menus, toolbars, etc., of course, the most important is the work area. When we have done this, basically the tools of the program can be implemented well.

Usually our program main window will inherit from QMainWindow , in order to get QMainWindow the various convenient functions provided. This is what the Qt Creator generated code does.

Since QMainWindow this class does not change in QT 5, the code above can be compiled directly from QT 4. In fact, the code we generate using QT Creator can be compiled directly in Qt 4. Just one thing to note: Qt 4 does not have a widgets module, so in pro files we usually need to write this:

QT +=4): qt + =    widgetstarget= qtdemotemplate  = appsources   + =         main.cpp  mainwindow.cppheaders+ = Mainwindow.h

Explain the pro file briefly. First, we define QT, which tells the compiler which modules to use. These modules are described in the previous section. We usually need to add the core and GUI. The second line, if the major version number () of QT is QT_MAJOR_VERSION greater than 4, which is QT 5, you need to add widgets (because in QT 5, all components are defined in the Widgets module). TARGET is the name of the generated program. The template is used to generate makefile templates, such as the app is compiled into an executable program, and Lib is compiled into a link library (the default is the dynamic link library). SOURCES and HEADERS, as the name implies, are the source code files and header files required by the project. Now we just need to use the default pro file. As the project continues to grow, pro files are often very complex.

Qt Learning Route 2 (7): MainWindow Introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.