Subclassing qmainwindow
The main window of the application is completed by creating the derived class of qmainwindow. Both qmainwindow and qdialog are derived from qwidget.
Closeevent () is a virtual function provided by qwidget. It is automatically called when the user closes the window.
Setcentralwidget () sets a widget as the central widget of the main window, and the central widget means that it occupies the central position of the main window during display.
Gui programming in QT supports multiple graphic formats. Images can be provided to applications in multiple ways, the most common of which include:
1) store images in files and load them at runtime.
2). Include the XPM file in the source code (the XPM file is also a legal C ++ file ).
3). Use the resource mechanism of QT.
Qt's resource mechanism is more convenient than runtime loading, and can work well for all supported image formats.
To use the resource mechanism of QT, you need to create a resource file and add a row in the. Pro file to identify the resource file. For example:
Resources = spreadsheet. qrc
The resource file uses a simple XML format. It is compiled into the executable files of the program, so it will not be lost. When locating resources, use the path prefix ":/", for example, ":/images/icon.png ". Resources can be any type of files.
3.2 creating menus and toolbars
Qt simplifies the programming of menu and toolbar by introducing the concept of action. An action can be added to any number of menus and toobar.
Programming menu and toolbar in QT involves three steps:
1). Create and set action
2). Create a menu and add action to it.
3) create a toolbar and add an action to it.
Action is created through the qaction class. For each action, you can set attributes such as accelerator, parent, shortcut key, visibility, and status tip for it. You can call connect () set the action to be executed when the action is triggered.
The base class qiniaceitemview of qtablewidget provides the selectall () slot.
The qapplication class provides the slot aboutqt (), which can be used through the global variable qapp (a pointer of the qapplication type.
In QT, menus are represented by instances of the qmenu class. Qmenu is to be placed in qmenubar. Function qmainwindow: menubar () returns a pointer of qmenubar * type. Qmenubar: addmenu () creates a qmenu widget based on the specified text and adds it to menubar. Qmenu: addaction () adds action to menu.
Any QT widget can have a series of related qactions. You can add an action for a widget by calling qwidget: addaction. This feature can be used to create context menus.
3.3 setting up the status bar
Qmainwindow: statusbar () returns a pointer to the status bar. the status bar is created when statusbar () is called for the first time.
3.4 implementing the menu
The qmessagebox: defalut modifier makes the modified button the default button, while the qmessage: Escape modifier makes the ESC key automatically trigger the modified button.
Qmessagebox: Warning () is used to pop up the prompt dialog box. This function is a static convenicence function provided by QT.
Static convenience Function
Qfiledialog: getopenfilename () can be used to obtain the file name from the user. This function displays a file selection dialog box, asking users to select a file and return the file name, or an empty string is returned when you select "cancel. The first parameter of this function is its parent widget. For dialog and other widgets, the parent-child relationship is different. A dialog is always an independent window, but if it has a parent, it is displayed in the center by default.
When you close the window, the slot qwidget: Close () will be called, and the slot will send the Close event to the corresponding widget. Re-implement qwidget: closeevent () can intercept this event to determine whether to close the window and prevent misoperation.
Each qwidget has a windowmodified attribute. It should be set to true when the window document is modified; otherwise, it is set to false.
The qstring: Arg () function replaces "% N" with the lowest number in the string and returns the replaced string.
Each action can have associated data of the qvariant type.
The qobject_cast <t> () mechanism in QT can also work properly for dynamic libraries.
3.5 using Dialog
Modeless window -- one that runs independently of any other windows in the Application
For modeless dialog, when it is popped up, there may be three situations:
1). This is the first time this dialog box has been activated.
2). This dialog box was previously activated, but the user closed it.
3). This dialog box has been activated and is still visible.
Show () changes a hidden window to visible, and activatewindow () changes the window status to active.
Model window -- pops up when invoked and blocks the application, preventing any other processing or interactions until it is closed.
If a dialog is activated using show (), it is modeless dialog; if it is activated through exec (), it is model dialog. You can also call setmodel () to set the dialog display mode.
Qdialog: exec () returns true when dialog is confirmed; otherwise, false.
Creating a model dialog on the stack is a good programming practice, because it is no longer needed after use, and the model dialog will be automatically destroyed after the scope ends.
Because the about boxes of most applications are highly identical, QT provides a convenient static convenicence function qmessage: About (). This function and qmessagebox: Warning () very similar.
3.6 storing setting
In QT, the application settings are stored in the platform-related location through the qsettings class-stored in the registry in windows, and there is a text file in UNIX.
The constructor of qsettings contains two parameters: organization's name and application's name. QT uses these two parameters to locate application settings.
Qsettings stores information in the form of key-value pair.
3.7 multiple documents
To implement multi-document programs, you must first create the main window on the stack through new instead of creating the main window on the stack.
Qaplication: closeallwindows () This slot closes all windows of the application, unless a window rejects the Close event. Programmers do not need to worry about unsaved changes, because this will be handled by qwidget: closeevent.
By calling the setattribute () function in the mainwindow constructor to set the QT: wa_deleteonclose attribute, QT can be automatically destroyed when the window is closed.
Qt supports the creation of SDI and MDI programs on all available platforms.
3.8 splash screnns
Adding a splash screen to a program in QT is very simple and can be implemented through the qsplashscreen class.
Generally, Code related to the splash screen is stored in main () and appears before calling qapplication: exec.