




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第Qt简单实现密码器控件本文实例为大家分享了Qt自定义一个密码器控件的简单实现代码,供大家参考,具体内容如下
实现构思:
密码器的功能可以看成是计算器和登陆界面的组合,所以在实现功能的过程中借鉴了大神的计算器的实现代码和登陆界面实现的代码。
实现的效果:
关于密码器控件的不足:
窗口的标题栏不够漂亮,但是由于对时间长度和任务进度的权衡,下次一定进行重绘。
代码思路:
由于我司不用样式表,所以背景由贴图函数完成。在widget中添加按钮控件和文本编辑控件。使用布局函数进行布局,在加上一些简单的逻辑处理功能即可。
首先创建一个工程文件,添加新文件,选择qt设计师界面类,如下:
进入创建的ui界面后,添加控件进行布局,单一的使用了珊格布局,如下:
在自定义控件的布局中遇到了一些与布局相关的问题:
问题1:如何改变布局内控件的大小?ui中修改方式如下,纯代码实现也可以去帮助手册中查找相同的接口函数。
问题2:布局中控件的位置如何进行更改?
*ui-gridLayout-setContentsMargins(QMargins(10,60,0,0));
ui-gridLayout-setVerticalSpacing(10);*
具体size,自行可以调整到比较合适的位置。
源码实现:
calculaterform.h
#defineCALCULATERFORM_H
#include"calacutorbutton.h"
#includeQWidget
#includeQLineEdit
namespaceUi{
classCalculaterForm;
classCalculaterForm:publicQWidget
Q_OBJECT
public:
explicitCalculaterForm(QWidget*parent=nullptr);
~CalculaterForm();
void
addLineEdit();
voidaddBackImg();//可以进行提供一个背景图片
privateslots:
voidon_pushButton_clicked(boolchecked);
voidon_pushButton_2_clicked(boolchecked);
voidon_pushButton_3_clicked(boolchecked);
voidon_pushButton_4_clicked(boolchecked);
voidon_pushButton_5_clicked(boolchecked);
voidon_pushButton_6_clicked(boolchecked);
voidon_pushButton_7_clicked(boolchecked);
voidon_pushButton_8_clicked(boolchecked);
voidon_pushButton_9_clicked(boolchecked);
voidon_pushButton_10_clicked(boolchecked);
voidon_pushButton_11_clicked(boolchecked);
voidon_pushButton_12_clicked(boolchecked);
voidon_pushButton_13_clicked(boolchecked);
voidon_pushButton_15_clicked(boolchecked);
voidon_pushButton_14_clicked(boolchecked);
private:
Ui::CalculaterForm*ui;
floatmNum1,mNum2,mResult;
charmSign;
intmMark;
QStringmKeyStr="0000";//密码字符串
QStringS;
QLineEdit*mLineEdit;
#endif//CALCULATERFORM_H
calculaterform.cpp
#include"calculaterform.h"
#include"ui_calculaterform.h"
#includeQLineEdit
#includeQDebug
#includeQMessageBox
CalculaterForm::CalculaterForm(QWidget*parent):
QWidget(parent),
ui(newUi::CalculaterForm)
ui-setupUi(this);
mNum1=0.0;
mNum2=0.0;
mResult=0.0;
S="";
mMark=1;
ui-pushButton_13-setMyIcon("/home/rabbitchenc/Image/dialog_cancel.png");
ui-pushButton_14-setMyIcon("/home/rabbitchenc/Image/ime_icon_del.png");
ui-pushButton_15-setMyIcon("/home/rabbitchenc/Image/dialog_ok.png");
mLineEdit=newQLineEdit(this);
setFixedSize(width(),height());
ui-gridLayout-setContentsMargins(QMargins(10,60,0,0));
ui-gridLayout-setVerticalSpacing(10);
addBackImg();
addLineEdit();
ui-pushButton_10-setEnabled(false);
ui-pushButton_12-setEnabled(false);
//添加文本编辑
void
CalculaterForm::addLineEdit()
if(mLineEdit!=nullptr){
mLineEdit-resize(width(),40);
mLineEdit-setStyleSheet("background:transparent;border-width:0;border-style:outset");
mLineEdit-setAlignment(Qt::AlignHCenter);
mLineEdit-setEchoMode(QLineEdit::Password);
}
//添加背景图片
voidCalculaterForm::addBackImg()
QStringfilename="/home/rabbitchenc/Image/ime_bg.png";
QPixmappixmap(filename);
QPalettepal;
pixmap=pixmap.scaled(width(),height());
pal.setBrush(QPalette::Window,QBrush(pixmap));
setPalette(pal);
CalculaterForm::~CalculaterForm()
deleteui;
voidCalculaterForm::on_pushButton_clicked(boolchecked)
S+="1";
mLineEdit-setText(S);
voidCalculaterForm::on_pushButton_2_clicked(boolchecked)
S+="2";
mLineEdit-setText(S);
voidCalculaterForm::on_pushButton_3_clicked(boolchecked)
S+="3";
mLineEdit-setText(S);
voidCalculaterForm::on_pushButton_4_clicked(boolchecked)
S+="4";
mLineEdit-setText(S);
voidCalculaterForm::on_pushButton_5_clicked(boolchecked)
S+="5";
mLineEdit-setText(S);
voidCalculaterForm::on_pushButton_6_clicked(boolchecked)
S+="6";
mLineEdit-setText(S);
voidCalculaterForm::on_pushButton_7_clicked(boolchecked)
S+="7";
mLineEdit-setText(S);
voidCalculaterForm::on_pushButton_8_clicked(boolchecked)
S+="8";
mLineEdit-setText(S);
voidCalculaterForm::on_pushButton_9_clicked(boolchecked)
S+="9";
mLineEdit-setText(S);
voidCalculaterForm::on_pushButton_10_clicked(boolchecked)
voidCalculaterForm::on_pushButton_11_clicked(boolchecked)
S+="0";
mLineEdit-setText(S);
voidCalculaterForm::on_pushButton_12_clicked(boolchecked)
voidCalculaterForm::on_pushButton_13_clicked(boolchecked)
this-close();
voidCalculaterForm::on_pushButton_15_clicked(boolchecked)
if(S==mKeyStr)
{
qDebug()"right";
this-close();
}else{
qDebug()"false";
QMessageBox*messageBox=newQMessageBox(QMessageBox::Warning,"错误提示","密码错误");
messageBox-show();
}
voidCalculaterForm::on_pushButton_14_clicked(boolchecked)
S=S.left(S.length()-1);
mLineEdit-setText(S);
}
自定义的按钮源码:
calacutorbutton.h
#ifndefCALACUTORBUTTON_H
#defineCALACUTORBUTTON_H
#includeQPushButton
classCalacutorButton:publicQPushButton
Q_OBJECT
public:
explicitCalacutorButton(QWidget*parent=nullptr);
~CalacutorButton();
voidsetText(constQStringtext);
voidsetMyIcon(constQStringicon);
voidsetImageName(constQStringimg);
voidsetPressImg(constQStringimg);
protected:
voidpaintEvent(QPaintEvent*event);
voiddrawText(QPainter*painter);
voiddrawImage(QPainter*painter);
voiddrawIcon(QPainter*painter);
QPixmap*ninePatch(QStringpicName,doubleiHorzSplit,doubleiVertSplit,doubleDstWidth,doubleDstHeight);
QPixmapgeneratePixmap(constQPixmapimg_in,intradius1,intradius2);
private:
QString
mFileName;
QStringmPressImgN
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 经济数据分析试题及答案全解析
- 中介租房水电费合同范例
- 深入浅出的中级经济师考试解析试题及答案
- 2025年工程项目管理咨询技巧试题及答案
- 剪辑员工合同范例
- 出口发票合同范例
- 2025年建筑工程试题及答案集锦
- 2025年经济法概论试题及答案分享
- 大服装店空间设计与品牌展示方案
- 2025年经济法学习指南试题及答案
- 手表买卖合同协议书
- 2023门面装修合同范本
- 《错误是最好的成长机会》主题班会课课件
- 直接作业环节的“7+1”安全管理制度课件
- 烟花爆竹行业特种作业人员安全管理培训
- 婴幼儿体格测量胸围的测量
- 幼儿园故事课件:《胸有成竹》
- 锂离子电池内阻的影响因素
- DB34-T 4170-2022 软阔立木材积表
- 《山西革命文化》课程教学大纲
- 工程质量问题整改回执单
评论
0/150
提交评论