Chapter 2 Hello World
As a beginner, the first thing is installed after QT, of course, Hello World, usually introduces the programmed textbooks starting from Hello World, I don't know if this tradition will bring any consequences, I have no courage now. Be a person who is the first to eat crabs. If you are not the first contact QT, you can skip this chapter. The following is to write the Hello World with two classic examples.
2.1 starting from two examples
Example 1:
Run Qt Designer, click on Menu File-> New, create a new project,
Here is C Project to determine. Next, you will prompt the project to save the location.
Select the save path and file name, determine, then click on the menu file-> new, select C Source File, determine, enter the following:
#include
#include
INT main (int Argc, char ** argv)
{
QAPPlication A (Argc, Argv);
Qpushbutton Hellobtn ("Hello World!", 0);
Hellobtn.resize (100, 20);
A.SETMAINWIDGET (& HELLOBTN);
Hellobtn.show ();
Return a.exec ();
}
Code description:
#include
This line contains the definition of the QApplication class. A QAPLICATION object must be used in each application using QT. QApplication manages a wide range of resources, such as default fonts and cursors.
#include
This line contains the definition of the QPushButton class. The top part of the reference document is mentioned which classes you need to include which header files must contain.
QpushButton is a classic graphical user interface button, and the user can press it or release. It manages yourself, just like every Qwidget. One window component is a user interface object that can handle user input and draw graphics. Programmers can change its full view and many of its main properties (such as colors), as well as this window component. A QPushButton can display a piece of text or a QPixMap.
INT main (int Argc, char ** argv)
{
The main () function is the entry of the program. Almost all cases of QT, Main () only needs to perform some initialization before transferring the control to the QT library, and then the QT library tells the user to inform the user to the program through the event.
ARGC is the number of command line variables, and Argv is an array of command line variables. This is a C / C feature. It is not qt proprietary, no matter how QT needs to handle these variables (see below).
QAPPlication A (Argc, Argv);
A is the QAPPlication of this program. It is created here and processes these command line variables (such as -Display under the X window). Note that all command line parameters identified by QT will be removed from ArgV (and ARGC is therefore reduced). See QApplication :: Argv () documentation for details.
Note: It is necessary to create a QAPPlication object before any Qt window system components are used.
Qpushbutton Hellobtn ("Hello World!", 0);
Here, after QAPPlication, the first window system code is then created: a button is created. This button is set to display "Hello World!" And it constitutes a window (because the constructor specifies 0 for its parent window, the button is positioned in this parent window).
Hellobtn.resize (100, 20);
This wine is set to 100 pixels wide, 20 pixels high (plus window system border). In this case, we don't have to consider the location of the button, and we accept the default.
A.SETMAINWIDGET (& HELLOBTN);
This button is selected as the main window component of this application. If the user turns off the main window component, the application exits.
You don't have to set up a main window component, but most of the programs have one.
Hellobtn.show ();
When you create a window component, it is invisible. You must call Show () to make it visible.
Return a.exec ();
Here is main () to transfer control to QT, and when exec () will return when the application exits.
In EXEC (), Qt accepts and processes events of users and systems and passes them to the appropriate window components.
}
You can now try to compile and run this program.
Compile
Compile a C app where you need to create a makefile. The easiest way to create a Qt Makefile is to use the QT provided QMKE. If you have saved main.cpp to its own directory, what you have to do is:
qmake hello.pro
The first command calls qmake to generate a .pro (item) file, generate a Makefile after running. You can now enter make (or nmake if you use Visual Studio), then run your first QT application!
qmake hello.pro
Make
./hello
Running display window
In fact, using Qt Designer can completely generate the above code, let's take another example,
Example 2:
Run Qt Designer, click on Menu File-> New, create a new project,
Here is C Project to determine. Next, you will prompt the project to save the location.
Select the save path and file name, determine.
Select Menu File-> New, create a window, select Dialog, determine
Set Form1 CAPTION to "My Programs", in the Property Editor Settings window properties, if you can't see the Property Editor on your IDE, please select the property editor / signal handlers by menu Windows-> Views.
Then put a TextLabel on the window, select TextLabe on CommON Widget on Toolbox,
Add another button, Pushbutton,
Set the TextLabel1's text to "Hello World", and the text of PushButton1 is set to "Off". Next to the Close button to add an event, select Menu Edit-> Connections ..., pop up the View and Edit Connections window,
Click on the button "New" in the new income, Sender selection PushButton1, Signal Select Clicked (), Receiver Select Form1, Slot Select Close (), OK, then save, here, basically Complete our Hello World, we still Need a main.cpp file, click Menu File-> New, select C Main File (Main.cpp)
File name filename: main.cpp, program main window main-form: form, determine, QT will automatically generate main.cpp file code,
#include
#include "form1.h"
INT main (int Argc, char ** argv)
{
QAPPlication A (Argc, Argv);
Form1 w;
W.SHOW ();
A.Connect (& A, Signal (LastWindowClosed ()), & A, Slot (Quit ()))
Return a.exec ();
}
Ok, here our second Hello World program is designed. Save the project, then compile the program:
Qmake HelloWorld.Pro
Make
./helloworld
If you save the project name is Hellowrold.Pro, you can compile it with the above command. QMake HelloWorld.Pro made Makefile, make starts compiling programs, and we can run our program after compiling. ./helloWorld runs,
Click the "Close" button to close the program.
2.2 This chapter section
This chapter takes you into the QT world through two simple examples. If you are already a C / C programmer, you will find that QT will be very easy to get started, the two simple examples of this chapter tell how to use Qt Designer to quickly Establish an app. In the second example, how to create a new window, add components, and how to set them. The above two examples mainly use two classes (CLASS):
QAPPlication class
QAPLICATION class management graphical user interface application control flow and main settings,
Public member
QApplication (int & argc, char ** argv)
QApplication (int & argc, char ** argv, bool guienable)
Enum type {TTY, Guiclient, GuiServer}
QApplication (int & argc, char ** argv, type type)
QApplication (Display * DPY, HANDLE VISUAL = 0, Handle ColorMap = 0)
QApplication (Display * DPY, INT ARGC, Char ** argv, handle visual = 0, handle colorap = 0)
Virtual ~ qapplication ()
Int argc () const
Char ** argv () const
TYPE TYPE () Const
Enum colorspec {normalcolor = 0, CustomColor = 1, Manycolor = 2}
Qwidget * mainwidget () const
Virtual void setmainwidget (Qwidget * mainwidget) Virtual Void Polish (Qwidget * W)
Qwidget * focuswidget () const
Qwidget * ActiveWindow () Const
int exec ()
Void Processevents ()
Void Processevents (IntMaxTime)
Void ProcessoneEvent ()
Bool haspendingevents ()
INT ENTER_LOOP ()
Void exit_loop ()
int letlevel () const
Virtual Bool Notify (QObject * Receiver, QEvent * e)
Void setDefaultcodec (QTextCodec * Codec)
QTextCodec * defaultcodec () Const
Void InstallTranslator (QTranslator * MF)
Void RemoveTranslator (QTranslator * MF)
Enum encoding {defaultcodec, unicodeutf8}
QString Translate (const char * sourcetext, const char * comment = 0, encoding encoding = defaultcodec) constec) Const
Virtual Bool MaceventFilter (Eventref)
Virtual Bool Wineventfilter (msg *)
Virtual Bool X11EventFilter (Xevent *)
INT X11ProCessevent (Xevent * Event)
Virtual Bool QWSEventfilter (QWSEvent *)
Void QWSSETCUSTOMCOLORS (QRGB * ColORTABLE, INT Start, Int Numcolors)
Void Winfocus (Qwidget * Widget, Bool Gotfocus)
Bool issessionRESTORED () Const
Qstring sessionid () const
Virtual Void CommitData (QSessionManager & SM)
Virtual Void Savestate (QSessionManager & SM)
Void wakeupguithread ()
Void Lock ()
Void unlock (Bool Wakeupgui = true)
Bool lock ()
BOOL TRYLOCK ()
Mate
Void quit ()
Void CloseAllWindows ()
related functions
Void Qaddpostroutine (QTCLEANUPFUNCTION P)
Const char * qversion ()
Bool QsysInfo (int * Wordsize, Bool * BigenDian)
Void Qdebug (const char * msg, ...)
Void Qwarning (const char * msg, ...)
Void Qfatal (const char * msg, ...)
Void QSystemWarning (const char * msg, int code)
Void Q_ASSERT (BOOL TEST)
Void Q_Check_ptr (void * p) qtmsghandler qinstallmsghandler (QtMsgHandler H)
It contains the main event loop, all events from window systems and other resources are processed and scheduled. It also handles the initialization and end of the application, and provides dialog management. It also handles the vast majority of system ranges and application ranges.
For any graphical user interface application using QT, there is just a QApplication object, regardless of the 0, 1, 2 or more windows in the same time.
QAPPlication object is accessible through global variable QAPP. It is responsible for:
It uses the user's desktop settings, such as Palette (), font (), and DoubleClickInterval () to initialize the application. If the user changes a global desktop, for example by some control panels, it keeps tracking these properties.
It performs event processing, that is, it receives events from a low window system and assists them to the relevant window components. By using sendevent () and poste (), you can send your own events to the window parts.
It analyzes command line parameters and set internal states according to them.
It defines the sense of view of applications encapsulated by QStyle objects. In operation, you can change it through setStyle ().
It specifies how the application assigns colors. Refer to setColorspec ().
It defines the default text encoding (please refer to SetDefaultCodec ()) and provides a localized string visible through translate ().
It provides a magic object like Desktop () and Clipboard ().
It knows the window of the application. You can use widget () to ask which window components exist on a determination point to get a list of TopLevelWidgets () (top-level widgets) and close all windows, and so on.
It manages the mouse cursor processing of the application, refer to setoverridecursor () and setglobalmousetracking ().
On the X window system, it provides a function of refreshing and synchronizing the traffic stream, you can refer to Flushx () and Syncx ().
It provides complex dialogue management support. This allows the application to end when the user is logged out, and if it is unable to terminate, the shutdown process is unable to close the process, and even the state of the entire application is retained.
Qpushbutton class
QpushButton is primarily used for command buttons,
#include
Inherited qbutton.
Public member
2 qpushbutton (Qwidget * parent, const char * name = 0)
2 qpushbutton (const qstring & text, qwidget * parent, const char * name = 0)
2 qpushbutton (Const Qiconset & icon, Const Qstring & Text, Qwidget * Parent, Const Char * Name = 0)
2 ~ qpushbutton ()
2 void setTogglebutton (bool)
2 bool autodefault () const
2 Virtual void setautodefault (bool autodef)
2 bool isdefault () const
2 Virtual Void SetDefault (Bool Def) 2 Virtual Void Setismenubutton (Bool Enable) (Abandon)
2 bool ismenubutton () Const (abandoned)
2 void setPopup (QPopupMenu * popup)
2 qpopupmenu * popup () const
2 void seticonset (Const Qiconset &)
2 qiconset * iconset () const
2 void setflat (bool)
2 bool isflat () const
Mate
Virtual void seton (bool)
Attributes
n Bool AutodeFault - Push button is an automatic default button
N BOOL Automask - Automatic mask feature in the button is valid (read only)
n BOOL Default - Push button is the default button
n BOOL FLAT - Whether the edge is invalid
n qiconset iconset - Icon pushing the button
n Bool Menubutton - Push the button has a menu button on it (abandoned)
n BOOL ON - Push button is switched
n BOOL ToggleButton - button is not a switch button
Push button or command button may be the most common window component in any graphical user interface. Push (click) button to command the computer to perform some operations or answer a question. Typical buttons are determined (OK), Apply, Cancel, Close, is (YES), No (no) and Help (Help).
The command button is a rectangular and typically displays a text tag to describe its operation. There is a loop in the label (in front of it "&" in the text) indicates shortcuts, for example:
Qpushbutton * PB = New Qpushbutton ("& Download", this);
In this example, the accelerator is Alt D, and the text tag will be displayed as Download.