首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 其他相关 >

Qt学习札记之一,登录界面

2013-10-08 
Qt学习笔记之一,登录界面。今天完成了第一个Qt项目,登录界面。环境:Qt5.1 Ubuntu 12.04 LTS效果:代码:#inclu

Qt学习笔记之一,登录界面。

今天完成了第一个Qt项目,登录界面。


环境:Qt5.1 Ubuntu 12.04 LTS


效果:

Qt学习札记之一,登录界面

代码:

#include "mainwindow.h"#include "ui_mainwindow.h"CLoginDlg::CLoginDlg(QWidget *parent) :    QDialog(parent){    QLabel* usrLabel = new QLabel(tr("user name:"));    QLabel* pwdLabel = new QLabel(tr("password:"));    usrLineEdit = new QLineEdit;    pwdLineEdit = new QLineEdit;    pwdLineEdit->setEchoMode(QLineEdit::Password);    QGridLayout* gridLayout = new QGridLayout;    gridLayout->addWidget(usrLabel,0,0,1,1);    gridLayout->addWidget(pwdLabel,1,0,1,1);    gridLayout->addWidget(usrLineEdit,0,1,1,3);    gridLayout->addWidget(pwdLineEdit,1,1,1,3);    QPushButton* okBtn = new QPushButton(tr("OK"));    QPushButton* cancelBtn = new QPushButton(tr("Cancel"));    QHBoxLayout* hBoxLayout = new QHBoxLayout;    hBoxLayout->setSpacing(60);    hBoxLayout->addWidget(okBtn);    hBoxLayout->addWidget(cancelBtn);    QVBoxLayout* vBoxLayout = new QVBoxLayout;    vBoxLayout->setMargin(40);    vBoxLayout->addLayout(gridLayout);    vBoxLayout->addStretch(60);    vBoxLayout->addLayout(hBoxLayout);    setLayout(vBoxLayout);    QObject::connect(okBtn, SIGNAL(clicked()), this, SLOT(accept()));    QObject::connect(cancelBtn, SIGNAL(clicked()), this, SLOT(reject()));    setWindowTitle("login");    resize(300,200);}void CLoginDlg::accept(){    if(usrLineEdit->text().trimmed() == tr("duan") && pwdLineEdit->text() == tr("duan"))    {        QMessageBox::warning(this,tr("Success!"),usrLineEdit->text(),QMessageBox::Cancel,QMessageBox::Yes);        QDialog::accept();      //success    }    else    {        QMessageBox::warning(this,tr("Warning!"),tr("The username or the password is wrong"),QMessageBox::Cancel,QMessageBox::Yes);        usrLineEdit->clear();        pwdLineEdit->clear();        usrLineEdit->setFocus();        QDialog::reject();    }}CLoginDlg::~CLoginDlg(){    delete usrLineEdit;    delete pwdLineEdit;}

继续加油!

热点排行