qt中的全局变量

来源:互联网 发布:苹果阴阳师网络未连接 编辑:程序博客网 时间:2024/04/30 11:48

转自:点击打开链接

这一段开发一个程序,需要多个源文件,包括若干个头文件和若干个定义文件。因此如何在多个源程序间开发传递变量就成了一个关键问题。一般来说在多个源程序间传递变量大概有两种方法,一是利用extern声明全局变量来进行传递,二是将全局变量定义成一个类的静态变量,通过类名::变量名进行调用。

通过若干次调试,第一种方法终于成功,现将注意要点记录如下:

WILD.H文件:

 #ifndef FORM1_H
#define FORM1_H

/*class wild
{
  public:
    static int s;
};*/

extern int num;

#endif

WILD.CPP文件:

#include "wild.h"

//wild::s=10;
int num=10;

FORM1.H文件:

/****************************************************************************
** Form interface generated from reading ui file 'form1.ui'
**
** Created: 六  2月 9 11:13:23 2008
**      by: The User Interface Compiler ($Id: qt/main.cpp   3.1.1   edited Nov 21 17: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文件:

/****************************************************************************
** Form implementation generated from reading ui file 'form1.ui'
**
** Created: 六  2月 9 11:13:35 2008
**      by: The User Interface Compiler ($Id: qt/main.cpp   3.1.1   edited Nov 21 17: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 the 
 *  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文件:

#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.exec();
}

要注意以下几点:

(1)头文件中声明全局变量时要加上extern关键字,全局变量的定义可以直接加在头文件中,也可以放到定义文件中,但在定义文件中的时候要注意加上变量类型符。

(2)调用全局变量的源文件可以不用包含全局变量所在的头文件,但最好是加上,因为几个文件间的相互包含makefile都给解决了。

(3)使用全局变量的时候需先声明,如extern int num;将其放在本文件的头文件的前面。

以上就是使用extern来解决全局变量的问题,但是这种方法有弊端,主要是如果在系统库函数中有和定义的全局变量同名的变量,将会造成冲突,其二是其结构不符合面向对象的思想,因此还是使用第二种方法为好。

原创粉丝点击