这两天研究qt,练习时做了个仿QQ登录界面,我这次实现的比较简单,先在这里记录一下,以后有空了会继续完善的。
(一)效果图
这里使用我的qq号测试的如图:
(二)工程文件
(三)代码实现
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE//登录状态
typedef enum
{ONLINE =1,ACTIVE,AWEY,BUSY,NOT_DISTURB,HIDE,OFFLINE
}LoginState;class MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private slots://登录的信号槽void login();void closethis();void on_btnOk_clicked();void on_btnCancel_clicked();void on_registuser_linkActivated(const QString &link);void on_registuser_linkHovered(const QString &link);void on_findpd_linkActivated(const QString &link);void on_findpd_linkHovered(const QString &link);private:Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QString>
#include <QImage>
#include <QMovie>
#include <QLabel>
#include "login.h"
#include "qqinterface.h"
#include <QMessageBox>
#include <QCompleter>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);this->setFixedSize(538,464);//设置窗口固定大小this->setWindowIcon(QIcon(":/Resource/QQ.ico"));//设置左上角图标//设置密码显示的格式ui->lineEdit_pd->setEchoMode(QLineEdit::Password);//构建账号补全列表QStringList listnumber;listnumber <<"1293957879"<<"123456789"<<"2020030421";//构建补全器QCompleter *number = new QCompleter(listnumber);//设置给编译器ui->lineEdit_uset->setCompleter(number);connect(ui->btnOk,SIGNAL(clicked(bool)),this,SLOT(login()));this->ui->registuser->setText("<a style = 'text-decoration:none' href='https://ssl.zc.qq.com/v3/index-chs.html'>注册账号</a>");this->ui->registuser->setOpenExternalLinks(true);this->ui->findpd->setText("<a style=' text-decoration:none' href= 'http://aq.qq.com/v2/uv_aq/html/reset_pwd/pc_reset_pwd_input_account.html'>找回密码</a>");//超链接this->ui->findpd->setOpenExternalLinks(true);this->setWindowTitle("明明的QQ~");}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::login()
{static int count =0;QString name = this->ui->lineEdit_uset->text();QString passwd = this->ui->lineEdit_pd->text();if(name == "123456789" && passwd == "1" ){qDebug()<<"登录成功";}else{qDebug()<<"登录失败";count ++;if(count ==3){disconnect(this->ui->btnOk,SIGNAL(clicked(bool)),this,SLOT(login()));}}
}void MainWindow::closethis()
{}void MainWindow::on_btnOk_clicked()
{QString name = this->ui->lineEdit_uset->text();QString passwd = this->ui->lineEdit_pd->text();if(name == "123456789" && passwd == "1" ){this->close();qqinterface * interface = new qqinterface();interface->show();}else{QMessageBox::about(NULL,"提示","账号/密码错误!");}
}void MainWindow::on_btnCancel_clicked()
{this->close();
}void MainWindow::on_registuser_linkActivated(const QString &link)
{qDebug()<<"activated";}void MainWindow::on_registuser_linkHovered(const QString &link)
{qDebug()<<"hovered";
}void MainWindow::on_findpd_linkActivated(const QString &link)
{qDebug()<<"activated";
}void MainWindow::on_findpd_linkHovered(const QString &link)
{qDebug()<<"hovered";
}
qqinterface.h
#ifndef QQINTERFACE_H
#define QQINTERFACE_H#include <QDialog>namespace Ui {
class qqinterface;
}class qqinterface : public QDialog
{Q_OBJECTpublic:explicit qqinterface(QWidget *parent = nullptr);~qqinterface();private:Ui::qqinterface *ui;
};#endif // QQINTERFACE_H
qqinterface.cpp
#include "qqinterface.h"
#include "ui_qqinterface.h"qqinterface::qqinterface(QWidget *parent) :QDialog(parent),ui(new Ui::qqinterface)
{ui->setupUi(this);// this->setWindowTitle("登录成功");
}qqinterface::~qqinterface()
{delete ui;
}
main.cpp
#include "mainwindow.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;w.show();return a.exec();
}
由于ui使用qt设计师设计的不能上传,看到这里你应该能够自己完成了,如果想参考源代码请自己下载我附上的源代码。
本文章仅供学习交流用禁止用作商业用途,文中所有内容均为原创未经授权不得转载
微信公众号:zhjj0729
微博:文艺to青年
简书:水枂