-
UID:81635
-
- 注册时间2009-07-03
- 最后登录2020-09-21
- 在线时间298小时
-
- 发帖571
- 搜Ta的帖子
- 精华0
- 金钱11122
- 威望594
- 贡献值18
- 好评度584
-
访问TA的空间加好友用道具
|
這是以前寫的一個類。。。 - #ifndef LOGINDLG_H
- #define LOGINDLG_H
- #include <QtGui>
- #include <QtCore>
- #include <QtSql>
- #include "controlpanel.h"
- #include "ce_communicator.h"
- #include "pa_database.h"
- extern QString g_username;
- class LoginDlg : public QDialog
- {
- Q_OBJECT
- public:
- LoginDlg(QWidget *parent = 0);
- ~LoginDlg();
- QString m_ip,m_password;
- QString getIP(){return m_ip;}
- QString getPassword(){return m_password;}
- private:
- ce_Communicator *m_sender;
- QLineEdit* m_le_ip;
- QLineEdit* m_le_username;
- QLineEdit* m_le_password;
- QProgressBar* m_progress;
- QTimer* m_timer;
- QTimer* m_cTimer;//checktimer
- bool m_flag;
- int m_value;
- bool m_bRight;
- private slots:
- void cTimerSlot();
- void timeoutSlot();
- void accept();
- void reject();
- private:
- void paintEvent(QPaintEvent *event);
- };
- #endif // LOGINDLG_H
- #include "logindlg.h"
- QString g_username;
- LoginDlg::LoginDlg(QWidget *parent)
- : QDialog(parent)
- {
- QString path = QCoreApplication::applicationDirPath();
- QDir::setCurrent(path);
- #ifdef Q_OS_WIN32
- path.replace("/","\\");
- path.append("\\Nanya.exe");
- QSettings *reg = new QSettings("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
- QSettings::NativeFormat);
- reg->setValue("Nanya",QVariant(path));
- delete reg;
- #endif
- #ifdef Q_OS_LINUX
- //modify the configure file to make the executable file autorun when setup
- #endif
- // this->setWindowFlags(Qt::WindowStaysOnTopHint |Qt::Dialog );
- this->setStyleSheet("QLineEdit{border:2px solid gray;border-style:outset;border-radius:5px;}"
- "QLineEdit:focus{border:2px solid darkcyan;border-style:inset;}"
- "QPushButton{border:2 solid gray;border-style:inset;border-radius:5px;}"
- "QPushButton:pressed{border-style:outset;background-color:darkgreen;}"
- "QPushButton:focus{border:2px dashed darkcyan;}"
- "QProgressBar {border-radius: 5px;border: 1px solid grey;}"
- "QProgressBar:chunk {min-height:15px;border-radius: 3px;background-color: green;background: qlineargradient(x1: 0, y1: 0.5, x2: 1, y2: 0.5, stop: 0 red, stop: 1 white); }");
- QHBoxLayout* lo_head = new QHBoxLayout;
- QLabel* la_headimg = new QLabel(this);
- la_headimg->setPixmap(QPixmap(":/images/nanya_log.png"));
- la_headimg->setFixedSize(QPixmap(":/images/nanya_log.png").size());
- QLabel* la_head = new QLabel(tr("Nanya University"),this);
- la_head->setFont(QFont("Helvetica [Cronyx]",36,85));
- la_head->setStyleSheet("color:Yellow;");
- lo_head->addWidget(la_headimg);
- lo_head->addWidget(la_head);
- lo_head->setAlignment(Qt::AlignHCenter);
- QLabel* la_subhead = new QLabel(tr("Intelligent Control System of Building Keding"),this);
- la_subhead->setFont(QFont("Helvetica [Cronyx]",30,70));
- la_subhead->setFixedWidth(420);
- la_subhead->setStyleSheet("color:cyan;");
- la_subhead->setAlignment(Qt::AlignHCenter);
- QLabel* la_ip = new QLabel(tr("IP Address:"),this);
- la_ip->setFont(QFont("Helvetica [Cronyx]",20,65));
- m_le_ip = new QLineEdit("192.168.0.196",this);
- m_le_ip->setFont(QFont("Helvetica [Cronyx]",20,65));
- m_le_ip->setFixedSize(240,40);
- QHBoxLayout* lo_ip = new QHBoxLayout;
- lo_ip->addWidget(la_ip);
- lo_ip->addWidget(m_le_ip);
- lo_ip->setAlignment(Qt::AlignHCenter);
- QLabel* la_username = new QLabel(tr("Username:"),this);
- la_username->setFont(QFont("Helvetica [Cronyx]",20,65));
- m_le_username = new QLineEdit(this);
- m_le_username->setFont(QFont("Helvetica [Cronyx]",20,65));
- m_le_username->setFixedSize(240,40);
- m_le_username->setAlignment(Qt::AlignHCenter);
- QHBoxLayout* lo_username = new QHBoxLayout;
- lo_username->addWidget(la_username);
- lo_username->addWidget(m_le_username);
- lo_username->setAlignment(Qt::AlignHCenter);
- QLabel* la_password = new QLabel(tr("Password:"),this);
- la_password->setFont(QFont("Helvetica [Cronyx]",20,65));
- m_le_password = new QLineEdit(this);
- m_le_password->setFont(QFont("Helvetica [Cronyx]",20,65));
- m_le_password->setFixedSize(240,40);
- m_le_password->setEchoMode(QLineEdit::PasswordEchoOnEdit);
- m_le_password->setAlignment(Qt::AlignHCenter);
- QHBoxLayout* lo_password = new QHBoxLayout;
- lo_password->addWidget(la_password);
- lo_password->addWidget(m_le_password);
- lo_password->setAlignment(Qt::AlignHCenter);
- QHBoxLayout* lo_btn = new QHBoxLayout;
- QPushButton* okBtn = new QPushButton(tr("OK"),this);
- QPushButton* cancelBtn = new QPushButton(tr("Cancel"),this);
- okBtn->setFocus();
- okBtn->setFont(QFont("Helvetica [Cronyx]",20,65));
- okBtn->setFixedSize(120,40);
- cancelBtn->setFont(QFont("Helvetica [Cronyx]",20,65));
- cancelBtn->setFixedSize(120,40);
- connect(okBtn,SIGNAL(clicked()),this,SLOT(accept()));
- connect(cancelBtn,SIGNAL(clicked()),this,SLOT(reject()));
- m_progress = new QProgressBar(this);
- m_progress->setFixedSize(140,15);
- m_progress->setTextVisible(false);
- m_progress->setRange(0, 100);
- m_progress->setOrientation(Qt::Horizontal);
- m_progress->setFormat(" %p");
- m_progress->setVisible(false);
- m_timer = new QTimer(this);
- connect(m_timer,SIGNAL(timeout()),this,SLOT(timeoutSlot()));
- m_cTimer = new QTimer(this);
- connect(m_cTimer,SIGNAL(timeout()),this,SLOT(cTimerSlot()));
- lo_btn->addSpacing(150);
- lo_btn->addWidget(okBtn);
- lo_btn->addSpacing(100);
- lo_btn->addWidget(cancelBtn);
- lo_btn->addWidget(m_progress);
- lo_btn->setAlignment(Qt::AlignLeft);
- QPushButton* boundary = new QPushButton(this);
- boundary->setFixedSize(this->width(),22);
- boundary->setStyleSheet("border:1px solid gray;margin:10px 0px 10px 0px;padding:0px;border-style:outset;");
- boundary->setEnabled(false);
- QVBoxLayout* l = new QVBoxLayout(this);
- l->addLayout(lo_head);
- l->addWidget(la_subhead,0,Qt::AlignHCenter);
- l->addLayout(lo_ip);
- l->addLayout(lo_username);
- l->addLayout(lo_password);
- l->addWidget(boundary);
- l->addLayout(lo_btn);
- setLayout(l);
- m_sender = NULL;
- }
- LoginDlg::~LoginDlg()
- {
- }
- void LoginDlg::paintEvent(QPaintEvent *event)
- {
- Q_UNUSED(event);
- QPainter painter;
- painter.begin(this);
- painter.drawPixmap(QPoint(0,0),QPixmap(":/images/nanya.png"));
- painter.end();
- }
- void LoginDlg::timeoutSlot()
- {
- if(m_value <= 0 || m_value >= 100)
- {
- m_bRight = (!m_bRight);
- }
- if(m_bRight)
- {
- // m_progress->setInvertedAppearance(true);
- m_value+=33;
- m_progress->setValue(m_value);
- }
- else
- {
- // m_progress->setInvertedAppearance(false);
- m_value-=33;
- m_progress->setValue(m_value);
- }
- }
- void LoginDlg::cTimerSlot()
- {
- m_cTimer->stop();
- if(m_sender != NULL)
- {
- m_sender->exit();
- m_flag = true;
- QMessageBox::critical(0,tr("Login Timeout"),tr("Failed to login on time."));
- m_progress->setVisible(false);
- qApp->quit();
- m_sender->deleteInstance();
- m_sender = NULL;
- }
- }
- void LoginDlg::accept()
- {
- m_progress->setVisible(true);
- m_progress->setValue(1);
- m_value = 1;
- m_bRight = true;
- m_flag = false;
- m_timer->start(100);
- qApp->processEvents();
- m_ip = m_le_ip->text();
- g_username = m_le_username->text();
- m_password = m_le_password->text();
- if(m_sender == NULL)
- {
- m_sender=::ce_Communicator::getInstance();
- }
- bool flag = true;
- while(!m_sender->init(getIP()))
- {
- if(flag)
- {
- m_cTimer->start(10*1000);
- flag = false;
- }
- qApp->processEvents();
- if(m_flag)
- {
- break;
- }
- }
- pa_database::init();
- qApp->processEvents();
- if(pa_database::m_inited)
- {
- m_le_ip->setEnabled(false);
- bool check = false;
- QSqlQuery query;
- query.exec("select username,password from userinfo");
- while(query.next())
- {
- if(query.value(0).toString()==g_username && query.value(1).toString()==getPassword())
- {
- check = true;
- }
- }
- if(check)
- {
- ControlPanel* panel = new ControlPanel;
- panel->showFullScreen();
- m_cTimer->stop();
- return QDialog::accept();
- }
- else
- {
- m_progress->setVisible(false);
- QMessageBox::critical(0,tr("Login Error"),tr("Username Or/And Password Error!"));
- }
- }
- else
- {
- m_progress->setVisible(false);
- }
- }
- void LoginDlg::reject()
- {
- // if(m_sender != NULL)
- // {
- // m_sender->exit();
- //
- // m_sender->deleteInstance();
- // m_sender = NULL;
- // }
- qApp->quit();
- }
|