QT day2

该代码示例展示了使用Qt框架创建一个登录界面,包括设置窗口属性、添加图像标签、文本编辑框和按钮。当用户点击登录按钮并输入正确凭证时,会触发跳转到另一个界面的信号。登录失败则显示错误信息。同时,定义了取消按钮关闭窗口。在主函数中,连接了登录成功后的跳转信号和目标界面的槽函数。
摘要由CSDN通过智能技术生成

1、登录系统添加跳转

widget.cpp


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

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    //设置窗口固定尺寸
    this->setFixedSize(500,400);
    //设置窗口标题
    this->setWindowTitle("登录系统");
    //设置窗口图标
    this->setWindowIcon(QIcon("D:\\QT\\hqyj\\QT\\icon\\QQ.png"));

    //标签
    QLabel *lab1 = new QLabel(this);
    lab1->resize(500,150);
    lab1->setPixmap(QPixmap("D:\\QT\\hqyj\\QT\\icon\\logo.png"));
    lab1->move(0,0);
    lab1->setScaledContents(true);

    QLabel *lab2 = new QLabel(this);
    lab2->resize(60,40);
    lab2->setPixmap(QPixmap("D:\\QT\\hqyj\\QT\\icon\\denglu.png"));
    lab2->move(130,150);
    lab2->setScaledContents(true);

    QLabel *lab3 = new QLabel("密码:",this);
    lab3->resize(lab2->size());
    lab3->move(130,190);
    lab3->setPixmap(QPixmap("D:\\QT\\hqyj\\QT\\icon\\denglumima.png"));
    lab3->setScaledContents(true);

    edit1 = new QLineEdit(this);
    edit1->resize(200,30);
    edit1->move(190,160);
    //edit1->setStyleSheet("border:none;");
    edit1->setPlaceholderText("QQ号/微信/邮箱");

    edit2 = new QLineEdit("密码",this);
    edit2->resize(edit1->size());
    edit2->move(190,200);
    edit2->setPlaceholderText("密码");
    edit2->setEchoMode(QLineEdit::Password);

    //登录按钮
    QPushButton *btn1 = new QPushButton;
    btn1->setParent(this);
    btn1->setText("登录");
    btn1->resize(90,30);
    btn1->move(220,240);
    btn1->setIcon(QIcon("D:\\QT\\hqyj\\QT\\icon\\denglu_1.png"));
    connect(btn1,SIGNAL(clicked()),this,SLOT(btn1_clicked()));


    //取消按钮
    QPushButton *btn3 = new QPushButton("取消",this);
    btn3->resize(btn1->size());
    btn3->move(220,280);
    btn3->setIcon(QIcon("D:\\QT\\hqyj\\QT\\icon\\quxiao.png"));
    connect(btn3,SIGNAL(clicked()),this,SLOT(close()));
}

void Widget::btn1_clicked()
{
    if(edit1->text() == "admin" && edit2->text() == "123456")
    {
        qDebug() << "登录成功";
        this->close();
        emit jump();
    }
    else
    {
        qDebug() << "账号或密码错误";
    }
}

Widget::~Widget()
{

}


new.cpp

#include "new.h"
#include "ui_new.h"


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

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

//处理跳转信号函数对应的槽函数
void New::jump_slot()
{
    this->show();       //将当前界面进行展示
}

main.cpp

#include "widget.h"
#include <QApplication>
#include "new.h"

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

    New n;

    QObject::connect(&w,&Widget::jump,&n,&New::jump_slot);

    return a.exec();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值