0


C++,使用Qt设计一个登录窗口

要求如下:

1、给窗体改变名称并设置窗口图标、尺寸固定
2、中间放log图
3、用户名和密码使用图片完成
4、账户用明文模式,密码用密文模式
5、点击登录后,将界面上的用户名和“admin”比较,密码和“123456”比较,如果匹配成功,则输出登录成功,如果匹配失败,则输出“账户密码不匹配”,并清空密码框(clear)
6、点击取消后,关闭整个界面

追加:

    点击登录按钮后,判断账号和密码是否一致,如果匹配失败,则弹出错误对话框,文本内容“账号密码不匹配,是否重新登录”,给定两个按钮ok和cancel,点击ok后,会清除密码框中的内容,继续进行登录;如果点击cancel按钮,则关闭界面。

    如果账号和密码匹配,则弹出信息对话框,给出提示信息为“登录成功”,给出一个按钮ok,点击ok后,关闭整个登录界面,跳转到其他界面

    点击取消按钮后,弹出问题对话框,询问是否确定要退出登录,给出两个按钮,yes|no,点击yes,则直接关闭整个登录界面,如果点击no则进行进行登录

要求:对象版和静态成员函数版至少各实现一个

代码:

头文件:

#ifndef MYWND_H
#define MYWND_H

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

QT_BEGIN_NAMESPACE
namespace Ui { class MyWnd; }
QT_END_NAMESPACE

class MyWnd : public QWidget
{
    Q_OBJECT
public slots:
    void userLogin();
    void userExit();

signals:
    void mysignals(QString e);

public:
    MyWnd(QWidget *parent = nullptr);
    ~MyWnd();

private:
    Ui::MyWnd *ui;
};
#endif // MYWND_H

第二个窗口:

#ifndef NEWWINDOW_H
#define NEWWINDOW_H

#include <QWidget>
#include <QDebug>

namespace Ui {
class NewWindow;
}

class NewWindow : public QWidget
{
    Q_OBJECT

public:
    void newSlot(QString e);

public:
    explicit NewWindow(QWidget *parent = nullptr);
    ~NewWindow();

private:
    Ui::NewWindow *ui;
};

#endif // NEWWINDOW_H

main函数:

#include "mywnd.h"
#include "newwindow.h"

#include <QApplication>

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

    NewWindow f;

    //将w界面自定义信号函数,与f界面的槽函数进行链接
    QObject::connect(&w, &MyWnd::mysignals, &f, &NewWindow::newSlot);

    //使用qDebug打印数据
    return a.exec();
}

功能代码:

#include "mywnd.h"
#include "ui_mywnd.h"

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

    //设置固定长度
    this->setFixedSize(QSize(540,410));

    //设置窗口标题
    this->setWindowTitle("登录");

    //设置窗口图标
    this->setWindowIcon(QIcon(":/logo/login.png"));

    //设置背景色
    this->setStyleSheet("background-color:white");

    //设置logo
    QLabel *imgLabel = ui->logo;
    imgLabel->setPixmap(QPixmap(":/logo/mid.png"));
    imgLabel->setScaledContents(true);

    QLabel *account_logo = ui->account_logo;
    account_logo->setPixmap(QPixmap(":/logo/account.png"));
    account_logo->setScaledContents(true);

    QLabel *passwd_logo = ui->password_logo;
    passwd_logo->setPixmap(QPixmap(":/logo/passwd.png"));
    passwd_logo->setScaledContents(true);

    //账号边框
    QLineEdit *account = ui->account;
    account->setStyleSheet("border-top:0px;border-bottom:1px solid;border-left:0px;border-right:0px;");
    account->setPlaceholderText("用户名");

    //密码边框
    QLineEdit *passwd = ui->password;
    passwd->setStyleSheet("border-top:0px;border-bottom:1px solid;border-left:0px;border-right:0px;");
    passwd->setEchoMode(QLineEdit::Password);
    passwd->setPlaceholderText("密码");

    //登录边框
    QPushButton *login = ui->logon;
    login->setStyleSheet("border-radius:5px;background-color:#4d85ff;color:white;");

    //取消边框
    QPushButton *cancel = ui->cancel;
    cancel->setStyleSheet("border-radius:5px;background-color:#4d85ff;color:white;");

    //登录事件
    connect(ui->logon, &QPushButton::clicked, this, &MyWnd::userLogin);

    //退出事件
    connect(ui->cancel, &QPushButton::clicked, this, &MyWnd::userExit);

    //信号
    //connect(this, &MyWnd::mysignals, [](QString e){});

}

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

void MyWnd::userLogin(){
    QString account = ui->account->text();
    QString password = ui->password->text();

    if(account == "admin" && password == "123456"){
        qDebug() << "匹配成功";

        QMessageBox box2(QMessageBox::NoIcon,"success", "登录成功!",
                         QMessageBox::Ok);
        int res = box2.exec();
        if(res == QMessageBox::Ok){
            this->close();
            emit mysignals("hello world");
        }
    }
    else{
        qDebug() << "账户密码不匹配";
        QMessageBox box1(QMessageBox::Critical,"error", "账号密码错误!",
                         QMessageBox::Ok | QMessageBox::Cancel);

        int res = box1.exec();

        if(res == QMessageBox::Ok){
            ui->password->clear();
        }
        else if(res == QMessageBox::Cancel){
            this->close();
        }

    }

}

void MyWnd::userExit(){
    QMessageBox box3(QMessageBox::Warning, "退出", "要退出吗?",
                     QMessageBox::Yes | QMessageBox::No);
    int res = box3.exec();

    if(res == QMessageBox::Yes){
        this->close();
    }
    else if(res == QMessageBox::No){
        ui->account->clear();
        ui->password->clear();
    }

}

第二个:

#include "newwindow.h"
#include "ui_newwindow.h"

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

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

void NewWindow::newSlot(QString e)
{
    qDebug() << e;
    this->show();
}

ui文件:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MyWnd</class>
 <widget class="QWidget" name="MyWnd">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>540</width>
    <height>410</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MyWnd</string>
  </property>
  <widget class="QLabel" name="logo">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>540</width>
     <height>160</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <family>Adobe Devanagari</family>
     <pointsize>20</pointsize>
    </font>
   </property>
   <property name="styleSheet">
    <string notr="true">background-color: rgb(85, 255, 127);</string>
   </property>
   <property name="text">
    <string/>
   </property>
  </widget>
  <widget class="QLabel" name="account_logo">
   <property name="geometry">
    <rect>
     <x>120</x>
     <y>210</y>
     <width>30</width>
     <height>30</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <family>Adobe Devanagari</family>
     <pointsize>18</pointsize>
    </font>
   </property>
   <property name="text">
    <string/>
   </property>
  </widget>
  <widget class="QLabel" name="password_logo">
   <property name="geometry">
    <rect>
     <x>120</x>
     <y>260</y>
     <width>30</width>
     <height>30</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <family>Adobe Devanagari</family>
     <pointsize>18</pointsize>
    </font>
   </property>
   <property name="text">
    <string/>
   </property>
  </widget>
  <widget class="QLineEdit" name="account">
   <property name="geometry">
    <rect>
     <x>150</x>
     <y>210</y>
     <width>265</width>
     <height>30</height>
    </rect>
   </property>
  </widget>
  <widget class="QLineEdit" name="password">
   <property name="geometry">
    <rect>
     <x>150</x>
     <y>260</y>
     <width>265</width>
     <height>30</height>
    </rect>
   </property>
   <property name="echoMode">
    <enum>QLineEdit::Password</enum>
   </property>
  </widget>
  <widget class="QPushButton" name="logon">
   <property name="geometry">
    <rect>
     <x>120</x>
     <y>340</y>
     <width>145</width>
     <height>45</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>18</pointsize>
    </font>
   </property>
   <property name="text">
    <string>登录</string>
   </property>
  </widget>
  <widget class="QPushButton" name="cancel">
   <property name="geometry">
    <rect>
     <x>270</x>
     <y>340</y>
     <width>145</width>
     <height>45</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>18</pointsize>
    </font>
   </property>
   <property name="text">
    <string>取消</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

第二个:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>NewWindow</class>
 <widget class="QWidget" name="NewWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>80</x>
     <y>80</y>
     <width>241</width>
     <height>101</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <family>Mongolian Baiti</family>
     <pointsize>18</pointsize>
     <weight>50</weight>
     <italic>false</italic>
     <bold>false</bold>
    </font>
   </property>
   <property name="styleSheet">
    <string notr="true">color: rgb(255, 0, 0);
background-color: rgb(0, 255, 0);
font: 18pt &quot;Mongolian Baiti&quot;;</string>
   </property>
   <property name="text">
    <string>欢迎来到我的世界</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

结果如下:

标签: c++ 开发语言 qt

本文转载自: https://blog.csdn.net/heyu0311/article/details/129976457
版权归原作者 heyu0311 所有, 如有侵权,请联系我们删除。

“C++,使用Qt设计一个登录窗口”的评论:

还没有评论