QT GUI Summary

Source: Internet
Author: User
Tags qt designer

QT provides designer tools that allow you to easily draw the interface using a mouse drag and drop. Automatically generates an. h file of an interface after drawing, such as Ui_mainwindow.h, which contains an automatically generated Ui_mainwindow class, the core function in this class is SETUPUI, which receives a qwidget based on the Interface Wizard. Parameter or Qmainwindow * parameter. This function automatically creates a visual control on the incoming qwidget or Qmainwindow based on a designer-drawn interface. There are two ways to use this automatically generated class, one is to create a member UI of the Ui_mainwindow type when defining Qwidget or Qmainwindow, to call its Setupui method Ui.setupui (this) in the constructor, or to use C + + The unique multi-inheritance method, when the subclass is defined with Ui_mainwindow as the base class, calls Setupui (this) directly in the constructor. You can now display the drawing interface in the custom part subclass. To access a visual control of the drawing interface, use either the Ui-> control name or the control name to refer directly to it in either of these ways.

Now that the display is no longer a problem, we also need to set the signal-slot of the interface element so that it can respond to events. QT provides a default signal-slot correspondence: On_ Control Name _ signal function name (parameter list). When such a function is defined, QT automatically relates the function to the interface control's signal, which is triggered when the control triggers the related event. Another way is to use the Qobject directly: : Connect function to correlate signals and slots. The syntax is:

Connect (signaled object pointer, SIGNAL (signal function prototype), response signal object, slot (slot function prototype));

Let's talk about the difference between the signal and the slot and the normal member function, except that the defined area is different. The signal function needs to be declared behind the signals: keyword in the header file, without implementing it. The groove needs to be defined in the Public/private slots: After the keyword, the implementation is the same as the normal function, Can be called directly, and of course the call permission is related to the range control keyword (public/private) in front of slots. Here are a few highlights:

1) A signal can be associated with multiple slots using the Connect function multiple times, and these slots do not guarantee the call order after the signal is triggered, but they will be executed again.

2) Multiple signals can be connected to the same slot.

3) The signals can be interconnected (the syntax is connect (object 1,signal (signal function prototype), Object 2, SIGNAL (signal function Prototype)), and when the first signal triggers another signal will also trigger

4) Use the Disconnect function to remove the signal-Slot Association

5) The signal and groove parameters must be in the same order, the same type, if the parameters of the signal is more than the parameters of the slot, redundant parameters are ignored, and vice versa.

6) The mechanism of signals and slots is not limited to interface development, and ordinary classes can also declare signals and slots, and establish associations

The syntax of the trigger signal is: emit signal name (argument list);

Organize interface elements using the layout mechanism : Layout controls have Qhboxlayout,qvboxlayout and qgridlayout. are horizontal layouts, vertical layouts, and grid layout controls. Other controls can be placed on top of them, The position and size of the interface elements are automatically adjusted when the size of the form changes. QT also provides a spreader control that places the rest of the elements in the location of the layout control and keeps the other element in place.

Create a menu bar on the main form, toolbars: Declare the Qaction object in the header file, and each qaction corresponds to a menu bar, toolbar object. Instantiate the action in the constructor and display it on the menu bar or toolbar.

Newaction = new Qaction (tr ("&new"), this);

Newaction->seticon (Qicon (":/images/new.png"));//Set the picture file in the resource as its icon

Newaction->setshortcut (qkeysequence::new);//Setting the shortcut key value is related to the system

Newaction->setstatustip (TR ("Create a new Spreadsheet file");//text that appears on the status bar when the mouse passes over

Connect (newaction, SIGNAL (triggered ()), this, slot (NewFile ()));//Set Signal-Slot Association

Filemenu = MenuBar ()->addmenu (tr ("&file"));//Create Top-level menu File menu

Filemenu->addaction (newaction);//Add the action created above to the menu

Filetoolbar = Addtoolbar (tr ("&file"));//Create a toolbar

Filetoolbar->addaction (newaction);//Add the action you created above to the toolbar

If you want to create a right-click menu for a part, you need to add the action to the part and set the policy for the part's context menu. A more advanced implementation is to rewrite the Contextmenuevent method of the part, create a qmenu window, Call the EXEC () function after adding an action in it.

Part Object->addaction (newaction);

Part Object->setcontextmenupolicy (Qt::actionscontextmenu);

Setting the status bar first requires adding controls to the status bar to display the information.

Locationlabel = new Qlabel ("W999");

Locationlabel->setalignment (Qt::alignhcenter);

Locationlabel->setminimumsize (Locationlabel->sizehint ());

Locationlabel->setindent (3);//Set extension factor, the size of the status bar changes is automatically adjusted area, the default extension factor is 0, indicating that the size does not change.

StatusBar ()->addwidget (Locationlabel);//Add controls to the status bar

The QT splash screen (Splash) is used to display information when the program is started and to wait for it to be canceled when it is started. It needs to be written in the main () function:

Qsplashscreen *splash = new Qsplashscreen ();

Splash->setpixmap (':/images/splash.png ');

Splash->show ();

Qt::alignmeng TopRight = Qt::alignright | Qt::aligntop;

Splash->showmessage (qobject.tr ("Information to be Displayed"), TopRight, qt::white);

The ShowMessage method can be called several times later to display different information to the user. Closed after use.

Splash->finish (&mainwin);//handing control over to Mainwin form

Delete Splash;

Qmainwindow central window part properties are used to specify the parts that are displayed in the central area of the main form, which can be a normal multiline control, a custom form part, a part with layout management, or a qsplitter part of a segmented window. Or to implement MDI-style Qmdiarea parts. Here's an MDI -style form implementation. First inherits a subclass from the Qmainwindow class, in the constructor of the subclass:

Mdiarea = new Qmdiarea ();

Setcentralwidget (Mdiarea);

Mdiarea->addsubwindow (part type pointer);//Add part (Qwidget type) to the area as a child window

Mdiarea->activenextsubwindow ();//Make the next subform the current active form

Mdiarea->activesubwindow () returns the current subform

Mdiarea->setactivesubwindow (Child Form object pointer)//sets the current subform. The subform object must be included in the Mdiarea, otherwise an error will be provided.

The above function basically implements the MDI form, but the opening of the subform closes, or the current subform changes, affecting the state of the main menu and the items in the toolbar, where the signal-slot mechanism needs to be flexibly applied to adjust the Enabled property when a state change occurs.

Custom QT Controls : After inheriting qwidget to implement a specific function visualization part, you can add it to the QT Designer tool using the plug-in method, This allows you to add a custom part to the form with the mouse drag-and-drop. You first need to inherit a subclass from Qdesignercustomwidgetfnterface, override several functions to specify the name of the control, the group in which the message is located, and so on, The key function is the Createwidget method, which returns an instance of a particular window part. Finally, add the Q_EXPORT_PLUGIN2 macro to the designer by adding the plugin to the end of the plugin source file.

QT4 greatly improved the efficiency of the drawing refresh, but in the special case also need to use a double buffer mechanism , the idea is to create a qpixmap, need to draw the graph now it is finished drawing, This pixmap is brushed directly above the image area of the screen when the PaintEvent event is triggered.

qt Event mechanism : to handle the event of an object, you can override the Qobject::event () function, which distributes specific events such as mouse event mousepressevent, keyboard event keypressevent, And PaintEvent, you can override these events if you are concerned about a particular event. You can override the TimerEvent function if you need a timer mechanism for timed triggering. First int mytimeid = Starttimer (interval ms); in TimerEvent : if (event->timerid () = = Mytimeid) {...}

QT provides an event monitoring mechanism that invokes the object 1->installeventfilter (object 2) in the constructor, so that object 1 can be monitored with objects and object 2 overrides its EventFilter function. The target parameter is used to determine the issuer of the event, and the event can be intervened. QT events are passed from child objects to the parent object, and stop passing if the child object handles the event. Note that the Installeventfilter function is defined on the Qobject, so you can set up event monitoring on the qapplication. An event that controls the entire application. The override Qapplication::notify () function intercepts an event as soon as the event is emitted. If you want the interface to respond to events at dense run time, you need to call Qapplication::p rocessevents ();

Add an icon to the QT project generated executable file

1. Find an ICO file, which is the appropriate name. such as Exeicon.ico.
2. Manually create a corresponding RC file. such as exeicon.rc:
Idi_icon1 ICON discardable "Exeicon.ico"
3. Put the above two files in the current directory and add the hdssmap.rc to the project. Regenerate.

Add an icon to the Qt form

In the main function:

Qapplication A (argc, argv);
A.setwindowicon (Qicon (":/datamanager/resources/statics.png"));

To set the default skin for QT programs:

Qapplication A (argc, argv);
A.setstyle ("windowsxp");

can also be set to: "Windows", "motif", "CDE", "Motifplus", "Platinum", "SGI" and "compact". Depending on the platform, "windowsxp", "Aqua" or "Macintosh" are also available.

QT calculates the run time of a program:

#include <QTime>

Qtime Tmptime;

Tmptime.start ();

Qdebug () << tmptime.elapsed () << "MS";

QT GUI Summary

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.