




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
第Qt簡單實(shí)現(xiàn)密碼器控件本文實(shí)例為大家分享了Qt自定義一個(gè)密碼器控件的簡單實(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)題欄不夠漂亮,但是由于對時(shí)間長度和任務(wù)進(jìn)度的權(quán)衡,下次一定進(jìn)行重繪。
代碼思路:
由于我司不用樣式表,所以背景由貼圖函數(shù)完成。在widget中添加按鈕控件和文本編輯控件。使用布局函數(shù)進(jìn)行布局,在加上一些簡單的邏輯處理功能即可。
首先創(chuàng)建一個(gè)工程文件,添加新文件,選擇qt設(shè)計(jì)師界面類,如下:
進(jìn)入創(chuàng)建的ui界面后,添加控件進(jìn)行布局,單一的使用了珊格布局,如下:
在自定義控件的布局中遇到了一些與布局相關(guān)的問題:
問題1:如何改變布局內(nèi)控件的大???ui中修改方式如下,純代碼實(shí)現(xiàn)也可以去幫助手冊中查找相同的接口函數(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等.壓縮文件請下載最新的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)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年榆林市公共交通總公司招聘(57人)筆試參考題庫附帶答案詳解
- 紡織品設(shè)計(jì)師證書考試評估體系試題及答案
- 幼兒園聘用幼兒教師臨時(shí)用工勞動合同書
- 家電營銷策劃合同協(xié)議書
- 中小學(xué)送餐合同協(xié)議書
- 合股協(xié)議書合同
- 個(gè)人保安員合同協(xié)議書
- 分錢合同協(xié)議書
- 合同協(xié)議書合同模板
- 合同協(xié)議書定金
- 婚戀-職場-人格學(xué)習(xí)通超星期末考試答案章節(jié)答案2024年
- (高清版)JTG 2111-2019 小交通量農(nóng)村公路工程技術(shù)標(biāo)準(zhǔn)
- xx學(xué)校研學(xué)旅行活動告家長書
- (格式已排好)國家開放大學(xué)電大《計(jì)算機(jī)應(yīng)用基礎(chǔ)(專)》終結(jié)性考試大作業(yè)答案任務(wù)一
- 圣地非遺-魯錦紋樣特征
- 中秋節(jié)英文PPT
- 項(xiàng)目二:旅游電子商務(wù)概述(授課PPT)教學(xué)課件
- 餐廳前期籌備工作計(jì)劃匯編
- MBR運(yùn)行管理手冊(共21頁)
- 512護(hù)士節(jié)開場節(jié)目快閃ppt模板
- 鋼材質(zhì)量證明書模板
評論
0/150
提交評論