//在3t2目录下
//adder.h
/* qt简单加法计算器 */ #ifndef ADDER_H #define ADDER_H #include<QDialog> #include<QLineEdit> #include<QtWidgets/QPushButton> #include<QtWidgets/QLabel> class Adder:public QDialog{ Q_OBJECT//让自定义的槽函数生效 private: QLineEdit * add1; QLabel * oper; QLineEdit *add2; QPushButton * equ; QLineEdit * res; QPushButton * bclose; public: Adder(); ~Adder(); /*自定义槽函数*/ public slots: void getres(); }; #endif
//adder.cpp
#include "adder.h" /*#include<QDialog> #include<QLineEdit> #include<QtWidgets/QPushButton> #include<QtWidgets/QLabel> */ Adder::Adder(){ this->resize(400,300); add1=new QLineEdit(this); add1->resize(80,20); add1->move(0,20); oper=new QLabel("+",this); oper->resize(20,20); oper->move(80,20); add2=new QLineEdit(this); add2->resize(80,20); add2->move(100,20); equ=new QPushButton("=",this); equ->resize(20,20); equ->move(180,20); res=new QLineEdit(this); res->resize(80,20); res->move(200,20); bclose=new QPushButton("close",this); bclose->resize(80,20); bclose->move(160,160); connect(bclose,SIGNAL(clicked()),this,SLOT(close())); connect(equ,SIGNAL(clicked()),this,SLOT(getres())); } Adder::~Adder(){ delete add1; delete oper; delete add2; delete equ; delete res; delete bclose; } void Adder::getres(){ int c=add1->text().toInt()+add2->text().toInt(); /* QString qa=add1->text(); QString qb=add2->text(); int a=qa.toInt(); int b=qb.toInt(); int c=a+b; */ res->setText(QString::number(c)); }
//qq.h
/*模拟qq登陆*/
#ifndef QQ_H #define QQ_H #include<QDialog> #include<QLineEdit> #include<QtWidgets/QPushButton> #include<QtWidgets/QLabel> class QQ:public QDialog { Q_OBJECT private: QLineEdit * username; QLineEdit* userpwd; QPushButton * login; QPushButton* cancel; public: QQ(); ~QQ(); /*函数,登陆和退出*/ public slots: void loginAndcancel(); }; #endif
//qq.cpp
#include "qq.h" QQ::QQ() { this->resize(400,300); username=new QLineEdit(this); username->resize(80,20); username->move(60,20); userpwd=new QLineEdit(this); userpwd->resize(80,20); userpwd->move(60,50); userpwd->setEchoMode(QLineEdit::Password); login=new QPushButton("login",this); login->resize(60,20); login->move(20,80); cancel=new QPushButton("cancel",this); cancel->resize(60,20); cancel->move(100,80); connect(login,SIGNAL(clicked()),this,SLOT(loginAndcancel())); connect(cancel,SIGNAL(clicked()),this,SLOT(loginAndcancel())); } QQ::~QQ() { delete username; delete userpwd; delete login; delete cancel; } #include<QMessageBox> void QQ::loginAndcancel() { /**/ QPushButton *b=(QPushButton*)sender(); if(b->text()=="login"){ if(username->text()=="admin"&& userpwd->text()=="123"){ qDebug("login success!"); } else{ qDebug("login failed"); } } else if(b->text()=="cancel"){ QMessageBox msg;//退出时提示 msg.setText("要退出吗?"); msg.setStandardButtons(QMessageBox::Yes|QMessageBox::No); if(msg.exec()==QMessageBox::Yes){ this->close(); } } }
//gettime.h
/*界面显示获取的当前时间 */
#ifndef GETTIME_H #define GETTIME_H #include<QDialog> #include<QLineEdit> #include<QtWidgets/QPushButton> #include<QtWidgets/QLabel> class GetTime:public QDialog{ Q_OBJECT private: QLabel *res; QPushButton *gettime; public: GetTime(); ~GetTime(); public slots: void settime(); //方案2,信号连接 public:signals: void ssettime(QString par); }; #endif
//gettime.cpp
#include "gettime.h" GetTime::GetTime() { this->resize(400,300); res=new QLabel("Cur Time",this); gettime=new QPushButton("gettime",this); gettime->move(0,50); connect(gettime,SIGNAL(clicked()),this,SLOT(settime())); connect(this,SIGNAL(ssettime(QString)),res,SLOT(setText(QString))); //connect(this); } GetTime::~GetTime() { delete res; delete gettime; } #include<QTime> /* #include<time.h> #include<stdio.h> */ void GetTime::settime(){ /* struct tm *tm1=localtime(time(0)); char buf[200]; sprintf(buf,"%4d-%2d-%2d %2d:%2d:%2d",tm1->tm_year+1700, tm1->tm_mon+1,tm1->tm_mday, tm1->tm_hour,tm1->tm_min,tm1->tm_sec); res->setText(buf); */ /* //1 直接调用res槽函数,设置值 res->setText(QTime::currentTime().toString("HH:mm:ss")); */ /*方案2,发送带参数的信号,再由信号参数去触发res的槽函数 emit */ emit ssettime(QTime::currentTime().toString("HH:mm:ss")); }
//main.cpp
/*测试上面三个功能 */ /* qt做简单的加法计算器 */ #include<QApplication> #include "adder.h" #include "qq.h" #include "gettime.h" int main(int argc,char * argv[]) { QApplication app(argc,argv); /*简单加法器 Adder add1; add1.show(); */ /*qq登陆框 QQ qq; qq.show(); */ /**/ GetTime t; t.show(); //qDebug("output");//在控制台上输出信息 app.exec(); }