通过QT制作一个模仿微信主界面的界面(不要求实现具体通信功能)

时间:2024-02-17 08:44:28

main.cpp

#include "widget.h"
#include "second.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    //实例化第二个界面
    Second s;
    QObject::connect(&w, &Widget::my_jump, &s, &Second::jump_slot);

    return a.exec();
}

second.cpp

#include "second.h"
#include "ui_second.h"

Second::Second(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Second)
{
    ui->setupUi(this);
}

Second::~Second()
{
    delete ui;
}

//第二个界面自定义的槽函数的实现
void Second::jump_slot()
{
    this->show();
}

void Second::on_sendBtn_clicked()
{
    QString msg = ui->msgEdit->text();
    ui->msgWidget->addItem(msg);
}

widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->setupUi(this);
    this->setWindowFlag(Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground);
    connect(ui->exit_btn, &QPushButton::clicked, this, &Widget::on_exit_btn_clicked);
    connect(ui->login_btn, &QPushButton::clicked, this, &Widget::on_login_btn_clicked);

}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_login_btn_clicked()
{
    if(ui->name_edit->text() == "admin" && ui->code_edit->text() == "123456"){
        QMessageBox::information(this,
                                 "提示",
                                 "登录成功",
                                 QMessageBox::Ok
                                 );
        this->close();
        emit my_jump();
    }else{
        QMessageBox msg(
                    QMessageBox::Warning,
                    "警告",
                    "账号和密码不匹配,是否重新登陆",
                    QMessageBox::Yes | QMessageBox::No,
                    this);
        int ret = msg.exec();
        if(ret == QMessageBox::Yes){  //判断警报
            this->show();
            ui->code_edit->clear();   //清除
        }else{
            this->close();   //关闭
        }
    }
}

void Widget::on_exit_btn_clicked()
{
    this->close();
}

second.ui

widget.ui