




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第Qt簡(jiǎn)單實(shí)現(xiàn)密碼器控件本文實(shí)例為大家分享了Qt自定義一個(gè)密碼器控件的簡(jiǎn)單實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)構(gòu)思:
密碼器的功能可以看成是計(jì)算器和登陸界面的組合,所以在實(shí)現(xiàn)功能的過(guò)程中借鑒了大神的計(jì)算器的實(shí)現(xiàn)代碼和登陸界面實(shí)現(xiàn)的代碼。
實(shí)現(xiàn)的效果:
關(guān)于密碼器控件的不足:
窗口的標(biāo)題欄不夠漂亮,但是由于對(duì)時(shí)間長(zhǎng)度和任務(wù)進(jìn)度的權(quán)衡,下次一定進(jìn)行重繪。
代碼思路:
由于我司不用樣式表,所以背景由貼圖函數(shù)完成。在widget中添加按鈕控件和文本編輯控件。使用布局函數(shù)進(jìn)行布局,在加上一些簡(jiǎn)單的邏輯處理功能即可。
首先創(chuàng)建一個(gè)工程文件,添加新文件,選擇qt設(shè)計(jì)師界面類,如下:
進(jìn)入創(chuàng)建的ui界面后,添加控件進(jìn)行布局,單一的使用了珊格布局,如下:
在自定義控件的布局中遇到了一些與布局相關(guān)的問(wèn)題:
問(wèn)題1:如何改變布局內(nèi)控件的大???ui中修改方式如下,純代碼實(shí)現(xiàn)也可以去幫助手冊(cè)中查找相同的接口函數(shù)。
問(wèn)題2:布局中控件的位置如何進(jìn)行更改?
*ui-gridLayout-setContentsMargins(QMargins(10,60,0,0));
ui-gridLayout-setVerticalSpacing(10);*
具體size,自行可以調(diào)整到比較合適的位置。
源碼實(shí)現(xiàn):
calculaterform.h
#defineCALCULATERFORM_H
#include"calacutorbutton.h"
#includeQWidget
#includeQLineEdit
namespaceUi{
classCalculaterForm;
classCalculaterForm:publicQWidget
Q_OBJECT
public:
explicitCalculaterForm(QWidget*parent=nullptr);
~CalculaterForm();
void
addLineEdit();
voidaddBackImg();//可以進(jìn)行提供一個(gè)背景圖片
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,"錯(cuò)誤提示","密碼錯(cuò)誤");
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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年休閑食品市場(chǎng)現(xiàn)狀調(diào)研及前景趨勢(shì)預(yù)測(cè)報(bào)告
- 石墨鋰電池負(fù)極材料項(xiàng)目可行性研究報(bào)告申請(qǐng)報(bào)告
- 特許經(jīng)營(yíng)合同終止協(xié)議書(shū)
- 線上教育行業(yè)變革
- 2025年濾光片項(xiàng)目可行性分析報(bào)告
- 聘用合同終止協(xié)議書(shū)范本
- 2025年矽鋼硅鋼項(xiàng)目提案報(bào)告模板
- 2025年食醋項(xiàng)目深度研究分析報(bào)告
- 中國(guó)珠光顏料項(xiàng)目創(chuàng)業(yè)計(jì)劃書(shū)
- 2025年建筑信息化項(xiàng)目投資分析及可行性報(bào)告
- GB/T 12605-2008無(wú)損檢測(cè)金屬管道熔化焊環(huán)向?qū)咏宇^射線照相檢測(cè)方法
- 閩侯縣國(guó)土空間總體規(guī)劃(2021-2035年)
- 烙鐵溫度點(diǎn)檢表
- 倉(cāng)庫(kù)溫濕度記錄表
- 初中 初二 物理 流體壓強(qiáng)與流速的關(guān)系 教學(xué)設(shè)計(jì)
- 霍蘭德職業(yè)興趣測(cè)試題(卷)完整版
- 飛控板安裝運(yùn)行調(diào)試pix固定翼
- 《中國(guó)古代文學(xué)史:唐宋文學(xué)》PPT課件(完整版)
- 5Why分析法經(jīng)典培訓(xùn)(43頁(yè))
- 2018二建繼續(xù)教育(市政公用工程)試題庫(kù)(有答案解析)
- 全面依法治國(guó)(課堂PPT)
評(píng)論
0/150
提交評(píng)論