mainwindow.h
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include #include QT_BEGIN_NAMESPACEnamespace Ui { class MainWindow; }QT_END_NAMESPACEclass MainWindow : public QMainWindow{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();signals:void my_signal();public slots:void my_slot();//自定义一个槽函数void loginbtn_slot();private:Ui::MainWindow *ui;};#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow){ui->setupUi(this);//窗口的相关设置this->setWindowTitle("QQ");this->setWindowIcon(QIcon(":/pictrue/qq.png"));//标签的相关设置ui->logolabel->setPixmap(QPixmap(":/pictrue/logo.png"));ui->logolabel->setScaledContents(true);//账号和密码ui->usernamelabel->resize(35,35);ui->usernamelabel->setPixmap(QPixmap(":/pictrue/userName.jpg"));ui->usernamelabel->setScaledContents(true);ui->passwordlabel->resize(35,35);ui->passwordlabel->setPixmap(QPixmap(":/pictrue/passwd.jpg"));ui->passwordlabel->setScaledContents(true);ui->usernamelineEdit->setPlaceholderText("QQ账号/手机号码/邮箱"); // ui->usernamelineEdit->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred); // ui->usernamelineEdit->adjustSize();ui->passwordlineEdit->setEchoMode(QLineEdit::Password);//登录按钮和取消登录按钮ui->loginbtn->setIcon(QIcon(":/pictrue/login.png"));ui->cancelbtn->setIcon(QIcon(":/pictrue/cancel.png"));//手动连接信号和槽函数,基于qt4版本 是不友好的连接connect(ui->cancelbtn,SIGNAL(clicked()),this,SLOT(my_slot()));//qt5版本 判断账号是否为admin,密码是否为123456connect(ui->loginbtn,&QPushButton::clicked,this,&MainWindow::loginbtn_slot);}MainWindow::~MainWindow(){delete ui;}void MainWindow::my_slot(){this->close();}void MainWindow::loginbtn_slot(){if(ui->usernamelineEdit->text()=="admin" && ui->passwordlineEdit->text()=="123456"){qDebug() << "登录成功";}else{qDebug() <usernamelineEdit->setText("");ui->passwordlineEdit->setText("");this->close();}}