当前位置: 首页 > article >正文

qt登录界面的完善

头文件1

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include<QPushButton>
#include<QLineEdit>
#include<QLabel>

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    int btn;
    int btn1;
signals:
    //void my_signal();
    void jump();
public slots:
    //void on_btn2_my_slot();
private slots: // 确保在这里声明槽函数
    void onLoginClicked();
    void onloginclicked1();

private:
    QLineEdit *edit1; // 声明为类成员
    QLineEdit *edit2; // 声明为类成员
};
#endif // WIDGET_H

头文件2

#ifndef SECOND_H
#define SECOND_H

#include <QWidget>

namespace Ui {
class second;
}

class second : public QWidget
{
    Q_OBJECT

public:
    explicit second(QWidget *parent = nullptr);
    ~second();
public slots:
    void jump_slot()
    {
        this->show();

    }

private:
    Ui::second *ui;
};

#endif // SECOND_H

主函数

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

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    second s;
    //s.show();
    QObject::connect(&w,&Widget::jump,&s,&second::jump_slot);

    return a.exec();
}

源文件1

#include "widget.h"
#include <QMessageBox> // 添加QMessageBox头文件

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    this->setWindowTitle("QQ");
    this->setWindowIcon(QIcon("D:\\嵌入式学习\\墨镜.png"));
    this->setFixedSize(800, 600);

    QPushButton * btn1 = new QPushButton;
    btn1->setParent(this);
    btn1->setText("登录");
    btn1->setStyleSheet("color:white; background-color:skyblue; border-radius:10px;");
    btn1->resize(100, 50);
    btn1->move(270, 450);

    QPushButton * btn2 = new QPushButton("取消", this);
    btn2->setStyleSheet("color:white; background-color:skyblue; border-radius:10px;");
    btn2->resize(btn1->size());
    btn2->move(btn1->x() + btn1->width() + 20, btn1->y());

    connect(btn1, &QPushButton::clicked, this, &Widget::onLoginClicked); // 使用Qt5版本的信号与槽连接
    connect(btn2, &QPushButton::clicked, this, &Widget::onloginclicked1); // 使用Qt5版本的信号与槽连接

    QLabel * lab1 = new QLabel("密码", this);
    lab1->move(btn1->x() - 50, btn1->y() - 80);

    QLabel * lab2 = new QLabel("账号", this);
    lab2->move(lab1->x(), lab1->y() - 50);

    edit1 = new QLineEdit(this); // 将edit1声明为类成员
    edit1->move(lab1->x() + lab1->width() - 50, lab1->y() - 5);
    edit1->setStyleSheet("color:black; background-color:pink;");
    edit1->resize(300, 30);
    edit1->setPlaceholderText("密码");
    edit1->setEchoMode(QLineEdit::Password);
    edit1->setAlignment(Qt::AlignCenter);

    edit2 = new QLineEdit(this); // 将edit2声明为类成员
    edit2->move(edit1->x(), lab2->y() - 5);
    edit2->resize(300, 30);
    edit2->setStyleSheet("color:black; background-color:pink;");

    QLabel * lab3 = new QLabel("hhh", this);
    lab3->resize(800, 250);
    lab3->setScaledContents(true);
    lab3->setPixmap(QPixmap("C:\\Users\\许曾嘉\\Desktop\\许曾嘉\\许曾嘉\\微信图片_20240928195223"));
}

void Widget::onLoginClicked() {
    QString username = edit2->text(); // 获取账号输入
    QString password = edit1->text(); // 获取密码输入

    // 设置正确的账号和密码
    if (username == "xzj" && password == "123456") {
        QMessageBox::information(this, "登录成功", "欢迎登录!");
        this->close();
        emit jump();
    } else {
        btn1=QMessageBox::warning(this, "登录失败", "账号或密码错误,是否重试。",QMessageBox::Yes|QMessageBox::No);
        if(btn1==QMessageBox::Yes)
        {
          edit1->clear();


        }
        else if(btn1==QMessageBox::No)
        {
            this->close();
        }
    }
}
void Widget::onloginclicked1()
{
  QMessageBox box(QMessageBox::Question,"问题","确定取消登录",QMessageBox::Yes|QMessageBox::No,this);
  btn=box.exec();
  if(btn==QMessageBox::Yes)
  {
      this->close();
  }
  else if(btn==QMessageBox::No)
  {
      box.close();

  }

源文件2

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

second::second(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::second)
{
    ui->setupUi(this);
    this->setFixedSize(800,600);
}

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


http://www.kler.cn/news/339101.html

相关文章:

  • 【CSS in Depth 2 精译_045】7.1 CSS 响应式设计中的移动端优先设计原则(上)
  • Python | Leetcode Python题解之第456题132模式
  • 0-1开发自己的obsidian plugin DAY 8
  • springboot 打包部署jsp页面两种方式war/jar
  • 中文llama3仿openai api实战
  • Python虚拟环境打包
  • 【题解】【模拟】—— [NOIP2013 普及组] 表达式求值
  • 【物流配送中心选址问题】基于改进粒子群算法
  • 回归预测 | Matlab基于SABO-SVR减法平均算法优化支持向量机的数据多输入单输出回归预测
  • 力扣之603.连续空余座位
  • Spring Boot:打造下一代医院管理系统
  • Go语言实现长连接并发框架 - 消息
  • 根据视频id查询播放量
  • ARTS Week 43
  • GO网络编程(一):基础知识
  • 【数据结构】【顺序表算法】 删除特定值
  • 搜索引擎相关的一段实习经历
  • AR智能眼镜之战:Meta vs Snap
  • 探索计算机科学的奥秘:从基础到进阶
  • Py2neo 教程