Developing a program in this section requires multiple source files, including several header files and several definition files. Therefore, developing and passing variables among multiple source programs becomes a key issue. Generally, there are two ways to pass variables between multiple source programs. One is to use extern to declare global variables for transmission, and the other is to define global variables as static variables of a class, call by Class Name: variable name.
After several debugging, the first method is successfully completed. The following are the key points to note:
Wild. h file:
# Ifndef form1_h
# Define form1_h
/* Class Wild
{
Public:
Static int S;
};*/
Extern int num;
# Endif
Wild. cpp file:
# Include "Wild. H"
// Wild: S = 10;
Int num = 10;
Form1.h file:
/*************************************** *************************************
** Form interface generated from reading UI file 'form1. Ui'
**
** Created: February 9 11:13:23 2008
** By: the user interface Compiler ($ ID: QT/Main. cpp 3.1.1 edited Nov 21 :40 $)
**
** Warning! All changes made in this file will be lost!
**************************************** ************************************/
# Ifndef form1_h
# Define form1_h
# Include <qvariant. h>
# Include <qwidget. h>
// # Include "Wild. H"
Class qvboxlayout;
Class qhboxlayout;
Class qgridlayout;
Class qlineedit;
Class qpushbutton;
// Class Wild;
Class form1: Public qwidget
{
Q_object
Public:
Form1 (qwidget * parent = 0, const char * name = 0, wflags FL = 0 );
~ Form1 ();
Qlineedit * lineedit1;
Qpushbutton * pushbutton1;
Protected:
Protected slots:
Virtual void languagechange ();
Virtual void sett ();
};
# Endif // form1_h
Form1.cpp file:
/*************************************** *************************************
** Form implementation generated from reading UI file 'form1. Ui'
**
** Created: February 9 11:13:35 2008
** By: the user interface Compiler ($ ID: QT/Main. cpp 3.1.1 edited Nov 21 :40 $)
**
** Warning! All changes made in this file will be lost!
**************************************** ************************************/
# Include "form1.h"
// # Include "Wild. H"
# Include <qvariant. h>
# Include <qlineedit. h>
# Include <qpushbutton. h>
# Include <qlayout. h>
# Include <qtooltip. h>
# Include <qwhatsthis. h>
# Include <qimage. h>
# Include <qpixmap. h>
# Include <iostream. h>
Extern int num;
/*
* Constructs a form1 as a child of 'parent', with
* Name 'name' and Widget flags set to 'F '.
*/
Form1: form1 (qwidget * parent, const char * Name, wflags FL)
: Qwidget (parent, name, FL)
{
If (! Name)
Setname ("form1 ");
Lineedit1 = new qlineedit (this, "lineedit1 ");
Lineedit1-> setgeometry (qrect (50, 80,191, 61 ));
Pushbutton1 = new qpushbutton (this, "pushbutton1 ");
Pushbutton1-> setgeometry (qrect (60,190,161, 71 ));
Languagechange ();
Resize (qsize (600,480). expandedto (minimumsizehint ()));
// Signals and slots connections
Connect (pushbutton1, signal (clicked (), this, slot (sett ()));
Connect (pushbutton1, signal (clicked (), this, slot (adjustsize ()));
}
/*
* Destroys the object and frees any allocated resources
*/
Form1 ::~ Form1 ()
{
// No need to delete child widgets, QT does it all for us
}
/*
* Sets the strings of the subwidgets using the current
* Language.
*/
Void form1: languagechange ()
{
Setcaption (TR ("form1 "));
Pushbutton1-> settext (TR ("pushbutton1 "));
}
Void form1: sett ()
{
// Lineedit1-> settext (wild: Sum );
Cout <num;
}
Main file:
# Include <qapplication. h>
# 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.exe C ();
}
Note the following:
(1) The extern keyword must be added when the header file declares the global variable. The definition of the global variable can be directly added to the header file or placed in the definition file, however, you must add the variable type in the definition file.
(2) The source file that calls the global variable does not need to include the header file where the global variable is located, but it is best to add it because the mutual inclusion of makefiles between several files is solved.
(3) When using global variables, declare them first, for example, extern int num; put them in front of the header file of this file.
The above is the use of extern to solve the problem of global variables, but this method has drawbacks, mainly if the system library function has a variable with the same name as the defined global variables, it will cause a conflict, the second is that its structure does not conform to the object-oriented idea, so it is better to use the second method.