login.h内容:
#ifndef LOGIN_H #define LOGIN_H #include <QMainWindow> #include <QDebug> #include <QJsonObject> #include <QLabel> #include <QLineEdit> #include <QRadioButton> #include <QPushButton> #include <QMessageBox> #include <QThread> namespace Ui { class CLogin; } class CLogin : public QMainWindow { Q_OBJECT public: explicit CLogin(QWidget *parent = 0); ~CLogin(); void Init(); private: Ui::CLogin *ui; QLabel *pLabelUsrName; QLabel *pLabelUsrPwd; QLineEdit *pLineEditUsrName; QLineEdit *pLineEditUsrPwd; QRadioButton *pRadioButtonSavePwd; QRadioButton *pRadioButtonAutoLogin; QPushButton *pButtonLogin; private slots: void Login(); signals: void LoginAuto(); void PwdOK(); }; #endif // LOGIN_H
login,cpp内容:
#include "login.h" #include "ui_login.h" CLogin::CLogin(QWidget *parent) : QMainWindow(parent), ui(new Ui::CLogin) { ui->setupUi(this); this->setWindowTitle("登录"); this->setMinimumHeight(300); this->setMaximumHeight(300); this->setMinimumWidth(400); this->setMaximumWidth(400); Init(); } CLogin::~CLogin() { delete pLabelUsrName; delete pLabelUsrPwd; delete pLineEditUsrName; delete pLineEditUsrPwd; delete pRadioButtonSavePwd; delete pRadioButtonAutoLogin; delete pButtonLogin; delete ui; } void CLogin::Init() { pLabelUsrName = new QLabel(this); pLabelUsrName->setText("帐号:"); pLabelUsrName->setGeometry(100,100, 30,20); pLabelUsrPwd = new QLabel(this); pLabelUsrPwd->setText("密码:"); pLabelUsrPwd->setGeometry(100,130, 30,20); pLineEditUsrName = new QLineEdit(this); pLineEditUsrName->setText(""); pLineEditUsrName->setGeometry(150,100, 150,20); pLineEditUsrPwd = new QLineEdit(this); pLineEditUsrPwd->setText(""); pLineEditUsrPwd->setGeometry(150,130, 150,20); pLineEditUsrPwd->setEchoMode(QLineEdit::Password); pRadioButtonSavePwd = new QRadioButton(this); pRadioButtonSavePwd->setText("记住密码"); pRadioButtonSavePwd->setGeometry(100,170, 100,20); pRadioButtonSavePwd->setChecked(1); pRadioButtonAutoLogin = new QRadioButton(this); pRadioButtonAutoLogin->setText("自动登录"); pRadioButtonAutoLogin->setGeometry(200,170, 100,20); pRadioButtonAutoLogin->setChecked(0); pButtonLogin = new QPushButton(this); pButtonLogin->setText("登录"); pButtonLogin->setGeometry(130,200, 150,20); pButtonLogin->setShortcut(Qt::Key_Return); //pButtonLogin->setStyleSheet("background:blue"); #if 0 //设置背景图片 this->setAutoFillBackground(true); // 这句要加上, 否则可能显示不出背景图. QPalette palette = this->palette(); palette.setBrush(QPalette::Window, QBrush(QPixmap(":/images/login.png").scaled(// 缩放背景图. this->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation))); // 使用平滑的缩放方式 this->setPalette(palette); // 给widget加上背景图 //TAB this->setTabOrder(pLabelUsrName, pLabelUsrPwd); this->setTabOrder(pLabelUsrPwd, pLineEditUsrName); this->setTabOrder(pLineEditUsrName, pLineEditUsrPwd); this->setTabOrder(pLineEditUsrPwd, pRadioButtonSavePwd); this->setTabOrder(pRadioButtonSavePwd, pRadioButtonAutoLogin); this->setTabOrder(pRadioButtonAutoLogin,pButtonLogin); #endif this->show(); connect(pButtonLogin, SIGNAL(clicked()), this, SLOT(Login())); connect(this, SIGNAL(LoginAuto()), this, SLOT(Login())); if (pRadioButtonAutoLogin->isChecked()) { emit LoginAuto(); } qDebug() << "Init Ok :"; } void CLogin::Login() { QString UsrName = pLineEditUsrName->text(); QString UsrPwd = pLineEditUsrPwd->text(); qDebug() << "UsrName:" << UsrName; qDebug() << "UsrPwd :" << UsrPwd; QJsonObject Value; Value["UsrName"] = UsrName; Value["UsrPwd"] = UsrPwd; qDebug() << "QJson :" << Value; std::string strUsrName = UsrName.toStdString(); std::string strUsrPwd = UsrPwd.toStdString(); if (strcmp(strUsrName.c_str(),"root") || strcmp(strUsrPwd.c_str(),"123456")) { QMessageBox::warning(this,"告警提示",tr("输入帐号或密码有误,请确认后输入!")); } else { QMessageBox::information(this,"提示",tr("登录成功!")); emit PwdOK(); } }
window.h内容:
#ifndef WINDOW_H #define WINDOW_H #include <QWidget> namespace Ui { class CWindow; } class CWindow : public QWidget { Q_OBJECT public: explicit CWindow(QWidget *parent = 0); ~CWindow(); private: Ui::CWindow *ui; private slots: void PwdOK(){this->show();}; }; #endif // WINDOW_H
window.cpp内容:
#include "window.h" #include "ui_window.h" CWindow::CWindow(QWidget *parent) : QWidget(parent), ui(new Ui::CWindow) { ui->setupUi(this); this->setWindowTitle("订单管理系统"); this->setMinimumHeight(600); this->setMaximumHeight(600); this->setMinimumWidth(800); this->setMaximumWidth(800); //this->show(); } CWindow::~CWindow() { delete ui; }
main.cpp内容:
#include "login.h" #include "window.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); CLogin wLogin; CWindow wWindow; QObject::connect(&wLogin, SIGNAL(PwdOK()), &wWindow, SLOT(PwdOK())); return a.exec(); }
运行结果:
文章来源: QT控件笔记