今天完成了第一个Qt项目,登录界面。
环境:Qt5.1 Ubuntu 12.04 LTS
效果:
代码:
#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; }