分类: 嵌入式
2009-12-21 15:32:23
Qt给C程序做界面的简单应用(笔记)
2009-12-20(NNF)
关键点:理解Qt只是一个图形库,给C做界面是把C函数嵌入到C++中去.
将要被调用的函数声明为extern,然后在Qt程序中使用
extern “C” int fun1(); extern “C” {int fun1();double fun2();}; |
的方式添加进去.
下面是一个实例:
/*To calculate your days from birthday to current time.*/ #include #include #include static int is_LeapOrFract(const int year) /*Leap year of Fractional year*/ { if((year%4==0&&year%100!=0)||year%400==0) return 366; else return 365; } static int is_Month_day(const int month,const int year) /*How many days of this month*/ { if(month<1||month>12) { printf("Wrong number!\n"); exit(-1); } if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) return 31; else if(month==2) { if(is_LeapOrFract(year)==366) return 29; else return 28; } else return 30; } static int first_part(const int year,const int month,const int day) /*The last days of birthyear*/ { int i,sum=0; for(i=12;i>month;i--) { sum+=is_Month_day(i,year); } return is_Month_day(month,year)-day+sum; } static int second_part(const int year,const int nyear) /*The days of the years between birthyear and this year*/ { int i,sum=0; i=year+1; for(;i { sum+=is_LeapOrFract(i); } return sum; } static int third_part(const int year,const int month,const int day) /*The past days of this year*/ { int i,sum=0; for(i=1;i { sum+=is_Month_day(i,year); } return sum+day; } extern int sumBirth(const int year,const int month,const int day) { time_t now; struct tm *curtime; int nyear,nmonth,nday; int sum=0,sumfirst=0,sumsecond=0,sumthird=0; time(&now); /*Get the arguments of current time (year month and day)*/ curtime=localtime(&now); nyear=curtime->tm_year+1900; nmonth=curtime->tm_mon+1; nday=curtime->tm_mday; sumfirst=first_part(year,month,day); sumsecond=second_part(year,nyear); sumthird=third_part(nyear,nmonth,nday); if(year==nyear) sum=sumthird-third_part(year,month,day); else sum=sumfirst+sumsecond+sumthird; return sum; } |
上面为一个简单的C 语言程序,用来计算出生到当前日期的天数.要为这个程序做一个简单的界面.只是使用几个简单的Widget就可以实现.下面是Qt源码.分三个文件:ConversionScreen.h是头文件,里面声明了所需的类及函数和变量.ConversionScreen.cpp是程序的主体部分,包含了界面的实现和C函数的调用以及Signal与Slot的连接.main.cpp是程序的框架.将程序分开几部分写是个好习惯,会对以后的维护和代码重用很有帮助!
ConversionScreen.h
#ifndef CONVERSION_SCREEN_H #define CONVERSION_SCREEN_H #include class QLabel; class QHBoxLayout; class QSpinBox; class ConversionScreen:public QWidget { Q_OBJECT public: ConversionScreen(); //构造函数,会在ConversionScreen.cpp里实现 ~ConversionScreen(){}; private slots: //自定义的槽实现.(想想规则) void setDays(); private: void createScreen(); //用到的函数 void createDays(); QWidget *window; //用到的Widget QSpinBox *spinYear; QSpinBox *spinMon; QSpinBox *spinDay; QLabel *labelYear; QLabel *labelMon; QLabel *labelDay; QLabel *labelDays; QSpinBox *textDay; QHBoxLayout *layout; int yearNum; 用到的变量 int monNum; int dayNum; int daysNum; }; #endif //CONVERSION_SCREEN_H |
ConversionScreen.cpp
#include #include #include #include #include"ConversionScreen.h" extern "C"
{int sumBirth(const int year,const int mon,const int day);}; 将C函数包含进来 ConversionScreen::ConversionScreen():QWidget() //构造函数的实现 { createScreen(); //调用自定义函数 } void ConversionScreen::createScreen() { //window = new QWidget; this->setWindowTitle("Enter Your BirthDay"); spinYear=new QSpinBox; spinMon=new QSpinBox; spinDay=new QSpinBox; spinYear->setRange(1900,2100); spinMon->setRange(1,12); spinDay->setRange(1,31); spinYear->setValue(1988); labelYear=new QLabel("year"); labelMon=new QLabel("mon"); labelDay=new QLabel("day"); labelDays=new QLabel("Days"); textDay=new QSpinBox; textDay->setRange(0,100000); layout=new QHBoxLayout; // 信号与槽的连接(想想为什么用this) QObject::connect(spinYear,SIGNAL(valueChanged(int)),this,SLOT(setDays())); QObject::connect(spinMon,SIGNAL(valueChanged(int)),this,SLOT(setDays())); QObject::connect(spinDay,SIGNAL(valueChanged(int)),this,SLOT(setDays())); layout->addWidget(spinYear); layout->addWidget(labelYear); layout->addWidget(spinMon); layout->addWidget(labelMon); layout->addWidget(spinDay); layout->addWidget(labelDay); layout->addWidget(textDay); layout->addWidget(labelDays); this->setLayout(layout); this->show();
//这个不能忘了 } void ConversionScreen::createDays() { yearNum=spinYear->value(); monNum=spinMon->value(); dayNum=spinDay->value(); daysNum=sumBirth(yearNum,monNum,dayNum); //C 函数的调用,其实很简单. } void ConversionScreen::setDays() //slot函数实现.(想想slot的规则) { createDays(); textDay->setValue(daysNum); } |
Main.cpp
#include #include"ConversionScreen.h" //包含自己写的头文件 int main(int argc,char **argv) { QApplication app(argc,argv); //函数参数的传入 ConversionScreen screen; //创建对象 screen.show(); //显示 return app.exec(); //返回等待处理 } |
下图为程序在qvfb中的运行界面.
Bye