学习记录之 使用 UI控件画出一个炫酷的界面

界面类定义

包括三种控件 QLabel QLineEdit QPushButton 分别是标签 编辑框 按钮

 #ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QLabel>
#include<QLineEdit>
#include<QPushButton>
class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();
    int generateRandomNumber();
private:
    QLabel *userlabel;
    QLabel *pwdlabel;
    QLabel *judelabel;
    QLineEdit *useredit;
    QLineEdit *pwdedit;
    QLineEdit *judeedit;
    QLineEdit *judeinputedit;
    QPushButton *entbtn;
    QPushButton *cancelbtn;
    QPushButton *regetbtn;



};

#endif // WIDGET_H

界面对象实现

一、Widget界面类以下几种方法的使用
1、setWindowTitle 设置标题
2、setGeometry设置像素点位置 大小
3、setWindowIcon 设置图标
this->setWindowTitle(QString::fromUtf8(“用户登录界面”));
this->setGeometry(100,100,400,400);
this->setWindowIcon(QIcon("./imge/1.jpg"));

二、三种控件
1、内容显示 调用构造函数实现
如:this->userlabel = new QLabel(QString::fromUtf8(“用户名:”),this);
2、setGeometry设置位置 以及显示大小
3、编辑框 和按钮的其他特殊方法等

三、设置背景图片
使用到QPalette 类
四、自动生成验证码

#include "widget.h"
#include <QIcon>
#include <QPalette>
#include <QTime>
#include <QDebug>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    //标题 图标 窗口的位置和大小
    this->setWindowTitle(QString::fromUtf8("用户登录界面"));
    this->setGeometry(100,100,400,400);
    this->setWindowIcon(QIcon("./imge/1.jpg"));
    // 标签
    this->userlabel = new QLabel(QString::fromUtf8("用户名:"),this);
    this->userlabel->setGeometry(50,100,50,20);
    this->pwdlabel = new QLabel(QString::fromUtf8("密码"),this);
    this->pwdlabel->setGeometry(50,150,50,20);
    this->judelabel = new QLabel(QString::fromUtf8("验证码: "),this);
    this->judelabel->setGeometry(50,200,50,20);
   //编辑框
    //用户
    this->useredit = new QLineEdit(this);
    this->useredit->setGeometry(110,100,150,30);
    this->useredit->setMaxLength(12);
    //密码
    this->pwdedit = new QLineEdit(this);
    //密文
    this->pwdedit->setEchoMode(QLineEdit::Password);
    //长度
    this->pwdedit->setMaxLength(8);
    this->pwdedit->setGeometry(110,150,150,30);
    //验证码 随机生成4位
    int i=this->generateRandomNumber();
    QString b=QString("%1").arg(i, 4, 10, QChar('0'));
    this->judeedit = new QLineEdit(b,this);
    this->judeedit->setGeometry(110,250,150,30);
    this->judeedit->setMaxLength(4);
    this->judeedit->setEnabled(false);//禁止编辑
    //验证码输入框
    this->judeinputedit = new QLineEdit(this);
    this->judeinputedit->setGeometry(110,200,150,30);
    this->judeinputedit->setMaxLength(4);

    //按钮
    this->entbtn = new QPushButton("确认",this);
     this->entbtn->setGeometry(50,300,50,30);
    this->cancelbtn = new QPushButton("取消",this);
     this->cancelbtn->setGeometry(300,300,50,30);
    this->regetbtn = new QPushButton("重新获取",this);
     this->regetbtn->setGeometry(300,200,100,30);
    //设置背景
    QPalette Palete(this->palette());
    Palete.setBrush(QPalette::Background,QBrush(QPixmap("./imge/1.jpg").scaled(this->size())));//scaled 适应大小
    this->setPalette(Palete);




}

Widget::~Widget()
{

}
int Widget::generateRandomNumber()
{
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
    for(int i=0; i<4; i++)
    {
        int test =qrand();
        return test;
    }
    return 0;
}

运行效果

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值