用QT实现简单请求登陆界面设计

来源:互联网 发布:hao123淘宝 编辑:程序博客网 时间:2024/05/01 11:41

mainWindows.h

#ifndef MAINWINDOW_H

#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void paintEvent(QPaintEvent *);//声明私有成员
private:
    QPushButton *pButton;
    QLabel *pUserName;
    QLabel *pLogin;
    QLineEdit *pNameEdit;
    QLineEdit *pLoginEdit;//创建槽
public slots:
    void login();
};
#endif // MAINWINDOW_H
mainwindow.cpp



#include "mainwindow.h"

#include <qwidget.h>
#include <Qpainter.h>
#include <qpixmap.h>
#include <QtGui>
#include <QMessageBox>
#include <QPalette>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    //设置窗口大小
    this->resize(500,300);
    this->setWindowTitle("模拟qq");
    //标签--账号、密码
    pUserName = new QLabel("账号:",this);
    pUserName->setGeometry(220,100,30,30);
    pLogin = new QLabel("密码:",this);
    pLogin->setGeometry(220,130,30,30);
    //文本框--账号密码输入
    pNameEdit = new QLineEdit(this);
    pNameEdit->setGeometry(250,100,100,20);
    pLoginEdit = new QLineEdit(this);
    pLoginEdit->setGeometry(250,130,100,20);
    //登陆按钮
    pButton = new QPushButton("登陆",this);
    pButton->setGeometry(300,200,50,35);
    QPalette pal(this->palette());
    //设置背景黑色
    pal.setColor(QPalette::Background, Qt::red);
    this->setAutoFillBackground(true);
    this->setPalette(pal);
    connect(pButton,SIGNAL(clicked(bool)),this,SLOT(login()));
}
MainWindow::~MainWindow()
{
}
void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load("C:/Users/abc/Pictures/3.jpg");
    painter.drawPixmap(100,90,100,100,pix);
}
void MainWindow::login()
{
    //获取文本框中的信息
    QString strU = pNameEdit ->text();
    QString strP = pLoginEdit->text();
    //登陆判断
    if(strU==NULL)
        QMessageBox::warning(NULL, "warning", "用户名不能为空");
    else if(strP==NULL)
        QMessageBox::warning(NULL, "warning", "密码不能为空");
    if(strU =="admin" && strP=="123")
        QMessageBox::information(NULL, "sucessful", "登陆成功");
    else
        QMessageBox::critical(NULL, "fail", "登陆失败");
}
main.cpp

#include "mainwindow.h"

#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

以上就是简单的全部代码,博主也是新手,所有代码的功能还是很不完善,就是记录自己每天的工作日常,能帮到大家学习自然是更好了,下面是程序运行截图

1.主页面

2.当用户名为空时

3.当输入的密码为空时

4.当用户密码输入正确的时候


4 0
原创粉丝点击