




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
第Qt簡單實現(xiàn)密碼器控件本文實例為大家分享了Qt自定義一個密碼器控件的簡單實現(xiàn)代碼,供大家參考,具體內(nèi)容如下
實現(xiàn)構(gòu)思:
密碼器的功能可以看成是計算器和登陸界面的組合,所以在實現(xiàn)功能的過程中借鑒了大神的計算器的實現(xiàn)代碼和登陸界面實現(xiàn)的代碼。
實現(xiàn)的效果:
關(guān)于密碼器控件的不足:
窗口的標(biāo)題欄不夠漂亮,但是由于對時間長度和任務(wù)進(jìn)度的權(quán)衡,下次一定進(jìn)行重繪。
代碼思路:
由于我司不用樣式表,所以背景由貼圖函數(shù)完成。在widget中添加按鈕控件和文本編輯控件。使用布局函數(shù)進(jìn)行布局,在加上一些簡單的邏輯處理功能即可。
首先創(chuàng)建一個工程文件,添加新文件,選擇qt設(shè)計師界面類,如下:
進(jìn)入創(chuàng)建的ui界面后,添加控件進(jìn)行布局,單一的使用了珊格布局,如下:
在自定義控件的布局中遇到了一些與布局相關(guān)的問題:
問題1:如何改變布局內(nèi)控件的大???ui中修改方式如下,純代碼實現(xiàn)也可以去幫助手冊中查找相同的接口函數(shù)。
問題2:布局中控件的位置如何進(jìn)行更改?
*ui-gridLayout-setContentsMargins(QMargins(10,60,0,0));
ui-gridLayout-setVerticalSpacing(10);*
具體size,自行可以調(diào)整到比較合適的位置。
源碼實現(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)行提供一個背景圖片
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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 園區(qū)低頻噪音管理辦法
- “數(shù)實融合”在皮革行業(yè)高質(zhì)量發(fā)展中的作用研究
- 公務(wù)接待超市管理辦法
- 干濕和氧化條件下生物炭對溶液中Cd2吸附機(jī)制的研究
- 農(nóng)墾食品安全管理辦法
- 小學(xué)德育安全副校長工作總結(jié)
- 醫(yī)用口罩銷售管理辦法
- 造價員安全職責(zé)
- 夏季北黃海三倍體長牡蠣的養(yǎng)殖環(huán)境與健康狀況調(diào)查
- 關(guān)于安全生產(chǎn)重要論述六大要點的安全心得
- 物流分揀中心勞務(wù)承攬服務(wù)方案(投標(biāo)方案)
- 配產(chǎn)配注方法培訓(xùn)
- 發(fā)動機(jī)缸徑測量實訓(xùn)課件
- 八五普法考試答案
- 國家電網(wǎng)考試歷年真題(含解析)
- 部編版九年級語文上冊教案
- 2023-2024學(xué)年黑龍江省寧安市初中語文七年級下冊期末高分通關(guān)試卷
- GB/T 6075.3-2011機(jī)械振動在非旋轉(zhuǎn)部件上測量評價機(jī)器的振動第3部分:額定功率大于15 kW額定轉(zhuǎn)速在120 r/min至15 000 r/min之間的在現(xiàn)場測量的工業(yè)機(jī)器
- GB/T 5594.4-2015電子元器件結(jié)構(gòu)陶瓷材料性能測試方法第4部分:介電常數(shù)和介質(zhì)損耗角正切值測試方法
- 預(yù)防保健科護(hù)理質(zhì)量控制考核標(biāo)準(zhǔn)
- 林州重機(jī)710采煤機(jī)電控箱裝配流程
評論
0/150
提交評論