界面类定义
包括三种控件 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;
}
运行效果