新项目要用到Qt,所以最近研究Qt,昨天安装好部署好Qt Creator现在马上开始第一个程序。

做一个简单的登陆界面功能

效果图如下

代码部分

View Code
#include <QtGui/QApplication>
#include "mainwindow.h"
#include "LoginDlg.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w; //主界面

    LoginDlg login; //登陆界面
    if(login.exec()==QDialog::Accepted)
    {
         w.show();
         return a.exec();
    }
    else return 0;

}
View Code
#ifndef LOGINDLG_H
#define LOGINDLG_H

#include <QDialog>

namespace Ui {
    class LoginDlg;
}

class LoginDlg : public QDialog {
    Q_OBJECT
public:
    LoginDlg(QWidget *parent = 0);
    ~LoginDlg();

protected:
    void changeEvent(QEvent *e);

private:
    Ui::LoginDlg *ui;

private slots:
    void on_pushButton_2_clicked();
    void on_pushButton_clicked();
};

#endif // LOGINDLG_H

View Code

#include "logindlg.h"
#include "ui_logindlg.h"
#include "QMessageBox.h"
#include "QTextCodec.h"
LoginDlg::LoginDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LoginDlg)
{
    ui->setupUi(this);
}

LoginDlg::~LoginDlg()
{
    delete ui;
}

void LoginDlg::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void LoginDlg::on_pushButton_clicked()
{
  if(this->ui->txtname->text().trimmed()== tr("admin") &&
     this->ui->txtpwd->text().trimmed()==tr("admin"))
  {
      accept();
  }
  else
  {
      QTextCodec::setCodecForTr( QTextCodec::codecForName("GBK") );
      QMessageBox::warning(this,tr("警告"),tr("用户名或密码错误!"),QMessageBox::Yes);
      this->ui->txtname->clear();
      this->ui->txtpwd->clear();
      this->ui->txtname->setFocus();
  }
}

void LoginDlg::on_pushButton_2_clicked()
{
    this->close();
}

代码很简单实现基本功能

posted on 2012-12-29 13:23  夜&枫  阅读(32848)  评论(0编辑  收藏  举报