作业要求:
通过QT制作一个模仿微信主界面的界面(不要求实现具体通信功能)
作业答案:
home.h
#ifndef HOME_H
#define HOME_H
#include <QWidget>
namespace Ui {
class HOME;
}
class HOME : public QWidget
{
Q_OBJECT
public:
explicit HOME(QWidget *parent = nullptr);
~HOME();
public slots:
void dump();
private slots:
void on_Exitbtn_clicked();
void on_Sendbtn_clicked();
void on_clearbtn_clicked();
private:
Ui::HOME *ui;
};
#endif // HOME_H
login.h
#ifndef LOGIN_H
#define LOGIN_H
#include <QWidget>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class Login; }
QT_END_NAMESPACE
class Login : public QWidget
{
Q_OBJECT
public:
Login(QWidget *parent = nullptr);
~Login();
signals:
void dump();
private slots:
void on_pushButton_2_clicked();
void on_pushButton_clicked();
private:
Ui::Login *ui;
};
#endif // LOGIN_H
home.cpp
#include "home.h"
#include "ui_home.h"
HOME::HOME(QWidget *parent) :
QWidget(parent),
ui(new Ui::HOME)
{
this->setWindowFlag(Qt::FramelessWindowHint);//去掉窗口头部
this->setAttribute(Qt::WA_TranslucentBackground);//去掉窗口其他部分
ui->setupUi(this);
}
HOME::~HOME()
{
delete ui;
}
//界面跳转处理槽函数
void HOME::dump()
{
this->show();
}
//退出按钮
void HOME::on_Exitbtn_clicked()
{
this->close();
}
//发送按钮
void HOME::on_Sendbtn_clicked()
{
ui->listWidget->addItem(ui->lineEdit->text());
ui->lineEdit->clear();
}
//清空按钮
void HOME::on_clearbtn_clicked()
{
ui->listWidget->clear();
}
login.cpp
#include "login.h"
#include "ui_login.h"
Login::Login(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Login)
{
ui->setupUi(this);
this->setWindowFlag(Qt::FramelessWindowHint);//去掉窗口头部
this->setAttribute(Qt::WA_TranslucentBackground);//去掉窗口其他部分
}
Login::~Login()
{
delete ui;
}
//X号按钮
void Login::on_pushButton_2_clicked()
{
this->close();
}
//登录按钮
void Login::on_pushButton_clicked()
{
if(ui->accountLE->text() == "admin" && ui->PasswordLE->text() == "123456")
{
QMessageBox::information(this,"","登录成功!");
ui->accountLE->text().clear();
ui->PasswordLE->text().clear();
emit dump();
this->close();
}
else
{
QMessageBox::information(this,"","登录失败,请重试");
ui->accountLE->text().clear();
ui->PasswordLE->text().clear();
}
}
main.cpp
#include "login.h"
#include "home.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Login w;
w.show();
HOME h;
QObject::connect(&w,&Login::dump,&h,&HOME::dump);
return a.exec();
}
login.ui
home.ui