文章目录
- 1 效果
- 2 核心代码
- 3 代码下载
1 效果
有背景音乐,登陆成功背景音乐消失,返回登陆,又有背景音乐。登陆账号和密码为空都会报错,并且重设焦点为空的控件上。账号密码存在mysql数据库中,使用mysqlsql驱动连接,查询库中表验证账号密码。
窗口边框圆角化(后补):
2 核心代码
连接数据库:
static bool createConnection(){//连接第一个数据库//QMYSQLQSqlDatabase db2 = QSqlDatabase::addDatabase("QMYSQL", "connection1");db2.setHostName("127.0.0.1");db2.setUserName("root");db2.setPassword("root");db2.setPort(8889);//test_majiang.dbdb2.setDatabaseName("test_majiang");if (!db2.open()) {//critical(QWidget *parent, const QString &title,//const QString &text,//QMessageBox::StandardButtons buttons = Ok,//QMessageBox::StandardButton defaultButton = NoButton)QMessageBox::critical(0, "Cannot open database","Unable to establish a database connection", QMessageBox::Cancel);return false;}return true;
}
验证密码:
void Login::on_lButton_clicked()
{username = ui->aComboBox->currentText() ;password = ui->pLineEdit->text();//qDebug() << "username" <<username<< endl;//qDebug() << "password" << password <<endl;if(username.isEmpty()){QMessageBox::information(this, tr("请输入账号"),tr("请先输入账号再登陆,谢谢!"),QMessageBox::Ok);ui->aComboBox->setFocus();}else if(password.isEmpty()){QMessageBox::information(this, tr("请输入密码"),tr("请先输入密码再登陆,谢谢!"),QMessageBox::Ok);ui->pLineEdit->setFocus();}else{if(!createConnection()){QMessageBox::information(this, tr("提示"),tr("请先连接网络,谢谢!"),QMessageBox::Ok);}else{QSqlDatabase db = QSqlDatabase::database("connection1");QSqlQuery query(db);QString str = QString("select * from user where account = '%0' and password = '%1'").arg(username).arg(password);query.exec(str);int record = query.size();qDebug() << "record:" << record<< endl;//未连接上数据库返回-1,连接上查询失败返回0,查到返回1if(record == -1 || record == 0){QMessageBox::information(this,tr("提示"),tr("用户名或密码错误!"),QMessageBox::Ok);ui->pLineEdit->clear();ui->pLineEdit->setFocus();}else{QMessageBox::information(this,tr("提示"),tr("登陆成功!"),QMessageBox::Ok);//QDialog::accept();if(isrembered == 1){m_userNameList.push_front(username);writeUsernameList(m_userNameList);}emit mainwidgetShow();ui->aComboBox->clear();music->stop();this->close();}}}
}
边框去圆角化:
QBitmap bmp(this->size());bmp.fill();QPainter p(&bmp);p.setPen(Qt::NoPen);p.setBrush(Qt::black);p.setPen(Qt::transparent);//表示RGBA值为(0,0,0,0)的透明色。p.setRenderHint(QPainter::Antialiasing); // 反锯齿;p.drawRoundedRect(bmp.rect(), 20, 20);setMask(bmp);
读入ini配置中的账号到ComboBox的下拉框中:
QString fileName = QCoreApplication::applicationDirPath() + "/login.ini";IniFile iniUsername(fileName);m_userNameList = iniUsername.getValue(QString("config"),QString("username"));ui->accountComboBox->addItems(m_userNameList);if(m_userNameList.size()>0){username = m_userNameList.at(0);qDebug() <<"m_userNameList.at(0)" << m_userNameList.at(0);}if(isrembered == 1){ui->accountCheckBox->setChecked(true);ui->accountComboBox->setCurrentText(username);}
写入记住的账号到ini文件中:
QString fileName = QCoreApplication::applicationDirPath() + "/login.ini";IniFile iniUsername(fileName);iniUsername.offCoverWrite();iniUsername.onValueDeduplication();iniUsername.onInsertDeduplication();iniUsername.saveValue(QString("config"),QString("username"), values);
动态背景:
QMovie *iconShow = new QMovie(":/image/yasuo.gif");ui->backgroundLabel->setMovie(iconShow);iconShow->start();
循环播放背景音乐:
QMediaPlaylist *playlist = new QMediaPlaylist();playlist->addMedia(QUrl("qrc:/music/LOL.mp3"));playlist->setPlaybackMode(QMediaPlaylist::Loop);backgroundMusic = new QMediaPlayer();backgroundMusic->setPlaylist(playlist);backgroundMusic->play();
3 代码下载
有积分的支持一下,谢谢:
https://download.csdn.net/download/qq_33375598/12345205
没有积分也可以下载的github地址,欢迎start:
https://github.com/fanxinglanyu/QtLogin