




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(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)功能的過程中借鑒了大神的計(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)的問題:
問題1:如何改變布局內(nèi)控件的大???ui中修改方式如下,純代碼實(shí)現(xiàn)也可以去幫助手冊(cè)中查找相同的接口函數(shù)。
問題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. 本站所有資源如無特殊說明,都需要本地電腦安裝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ù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 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年金屬單質(zhì)鐵氧化物項(xiàng)目立項(xiàng)申請(qǐng)報(bào)告
- 賽跑比賽記事作文8篇
- 2025年消防安全設(shè)施維護(hù)與管理操作規(guī)范操作規(guī)范操作規(guī)范操作規(guī)范操作規(guī)范操作規(guī)范考試題庫(kù)
- 2025年P(guān)CM脈碼調(diào)制終端設(shè)備項(xiàng)目立項(xiàng)申請(qǐng)報(bào)告
- 2025年心理咨詢師基礎(chǔ)理論知識(shí)測(cè)試卷(心理咨詢實(shí)踐案例分析)
- 2025年保險(xiǎn)從業(yè)資格考試保險(xiǎn)業(yè)務(wù)產(chǎn)品開發(fā)案例分析科目試卷
- 我和我的動(dòng)物朋友:寫物作文10篇
- 2025年電梯檢驗(yàn)員資格考試全真模擬試卷(含答案解析)
- 2025年法律職業(yè)資格考試客觀題試卷一法律職業(yè)道德與案例分析
- 軟件測(cè)試服務(wù)協(xié)議
- 2022年廣東高考成績(jī)一分一段表重磅出爐
- 新版病人搬運(yùn)(輪椅)操作評(píng)分標(biāo)準(zhǔn)
- 重癥監(jiān)護(hù)ICU護(hù)理實(shí)習(xí)生出科考試試題及答案
- GB/Z 22074-2008塑料外殼式斷路器可靠性試驗(yàn)方法
- GB/T 32360-2015超濾膜測(cè)試方法
- GB/T 15558.1-2015燃?xì)庥寐竦鼐垡蚁?PE)管道系統(tǒng)第1部分:管材
- 中藥學(xué)全套(完整版)課件
- 工程施工停止點(diǎn)檢查表
- 國(guó)開??啤锻鈬?guó)文學(xué)》十年期末考試題庫(kù)及答案
- 《滅火器維修》GA95-2015(全文)
- 浙江義務(wù)教育學(xué)校校園飲水質(zhì)量提升工程建設(shè)和維護(hù)浙江教育廳
評(píng)論
0/150
提交評(píng)論