最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Qt簡(jiǎn)單實(shí)現(xiàn)密碼器控件

 更新時(shí)間:2022年06月15日 08:26:45   作者:RabbitChenc  
這篇文章主要為大家詳細(xì)介紹了Qt簡(jiǎn)單實(shí)現(xiàn)密碼器控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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

#define CALCULATERFORM_H
#include "calacutorbutton.h"
#include <QWidget>
#include <QLineEdit>

namespace Ui {
class CalculaterForm;
}

class CalculaterForm : public QWidget
{
? ? Q_OBJECT

public:
? ? explicit CalculaterForm(QWidget *parent = nullptr);
? ? ~CalculaterForm();

? ? ?void ?addLineEdit();
? ? ?void addBackImg();//可以進(jìn)行提供一個(gè)背景圖片
private slots:

? ? void on_pushButton_clicked(bool checked);

? ? void on_pushButton_2_clicked(bool checked);

? ? void on_pushButton_3_clicked(bool checked);

? ? void on_pushButton_4_clicked(bool checked);

? ? void on_pushButton_5_clicked(bool checked);

? ? void on_pushButton_6_clicked(bool checked);

? ? void on_pushButton_7_clicked(bool checked);

? ? void on_pushButton_8_clicked(bool checked);

? ? void on_pushButton_9_clicked(bool checked);

? ? void on_pushButton_10_clicked(bool checked);

? ? void on_pushButton_11_clicked(bool checked);

? ? void on_pushButton_12_clicked(bool checked);

? ? void on_pushButton_13_clicked(bool checked);

? ? void on_pushButton_15_clicked(bool checked);

? ? void on_pushButton_14_clicked(bool checked);

private:
? ? Ui::CalculaterForm *ui;
? ? float mNum1,mNum2,mResult;
? ? char mSign;
? ? int mMark;
? ? QString mKeyStr = "0000";//密碼字符串
? ? QString S;
? ? QLineEdit *mLineEdit;

};

#endif // CALCULATERFORM_H

calculaterform.cpp

#include "calculaterform.h"
#include "ui_calculaterform.h"
#include <QLineEdit>
#include <QDebug>
#include <QMessageBox>
CalculaterForm::CalculaterForm(QWidget *parent) :
? ? QWidget(parent),
? ? ui(new Ui::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 = new QLineEdit(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);
? ? }
}
//添加背景圖片
void CalculaterForm::addBackImg()
{
? ? QString filename = "/home/rabbitchenc/Image/ime_bg.png";
? ? QPixmap pixmap(filename);
? ? QPalette pal;
? ? pixmap = pixmap.scaled(width(),height());
? ? pal.setBrush(QPalette::Window,QBrush(pixmap));
? ? setPalette(pal);

}

CalculaterForm::~CalculaterForm()
{
? ? delete ui;
}

void CalculaterForm::on_pushButton_clicked(bool checked)
{
? ? S += "1";
? ? mLineEdit->setText(S);

}

void CalculaterForm::on_pushButton_2_clicked(bool checked)
{
? ? S += "2";
? ? mLineEdit->setText(S);

}

void CalculaterForm::on_pushButton_3_clicked(bool checked)
{
? ? S += "3";
? ? mLineEdit->setText(S);
}

void CalculaterForm::on_pushButton_4_clicked(bool checked)
{
? ? S += "4";
? ? mLineEdit->setText(S);

}

void CalculaterForm::on_pushButton_5_clicked(bool checked)
{
? ? S += "5";
? ? mLineEdit->setText(S);
}

void CalculaterForm::on_pushButton_6_clicked(bool checked)
{
? ? S += "6";
? ? mLineEdit->setText(S);

}

void CalculaterForm::on_pushButton_7_clicked(bool checked)
{
? ? S += "7";
? ? mLineEdit->setText(S);

}

void CalculaterForm::on_pushButton_8_clicked(bool checked)
{
? ? S += "8";
? ? mLineEdit->setText(S);
}

void CalculaterForm::on_pushButton_9_clicked(bool checked)
{
? ? S += "9";
? ? mLineEdit->setText(S);
}

void CalculaterForm::on_pushButton_10_clicked(bool checked)
{

}

void CalculaterForm::on_pushButton_11_clicked(bool checked)
{
? ? S += "0";
? ? mLineEdit->setText(S);
}

void CalculaterForm::on_pushButton_12_clicked(bool checked)
{

}

void CalculaterForm::on_pushButton_13_clicked(bool checked)
{
? ? this->close();
}

void CalculaterForm::on_pushButton_15_clicked(bool checked)
{

? ? if(S == mKeyStr)
? ? {
? ? ? ? qDebug() << "right";
? ? ? ? this->close();
? ? }else{
? ? ? ? qDebug() << "false";
? ? ? ? QMessageBox *messageBox = new QMessageBox(QMessageBox::Warning,"錯(cuò)誤提示","密碼錯(cuò)誤");
? ? ? ? messageBox->show();
? ? }
}
void CalculaterForm::on_pushButton_14_clicked(bool checked)
{
? ? S = S.left(S.length() - 1);
? ? mLineEdit->setText(S);
}

自定義的按鈕源碼:
calacutorbutton.h

#ifndef CALACUTORBUTTON_H
#define CALACUTORBUTTON_H
#include <QPushButton>

class CalacutorButton: public QPushButton
{
? ? Q_OBJECT

public:

? ? explicit CalacutorButton(QWidget *parent = nullptr);
? ? ~CalacutorButton();
? ? void setText(const QString&text);
? ? void setMyIcon(const QString&icon);
? ? void setImageName(const QString&img);
? ? void setPressImg(const QString&img);

protected:
? ? void paintEvent(QPaintEvent *event);
? ? void drawText(QPainter *painter);
? ? void drawImage(QPainter*painter);
? ? void drawIcon(QPainter*painter);
? ? QPixmap* ninePatch(QString picName,double iHorzSplit,double iVertSplit, double DstWidth, double DstHeight);
? ? QPixmap generatePixmap(const QPixmap& img_in, int radius1,int radius2);

private:
? ? QString ?mFileName;
? ? QString mPressImgName;
? ? QString mNormalImgName;
? ? QString mFocusImgName;
? ? QString mDisableName;
? ? QString mText;
? ? QString mIcon;
? ? int mWidth;
? ? int mHeight;
? ? bool pressed;
};

#endif // CALACUTORBUTTON_H

calacutorbutton.cpp

#include "calacutorbutton.h"

#include <QPainter>
#include <QBitmap>
#include <QMouseEvent>
#include <QSizePolicy>

//增加對(duì)按鈕類型的設(shè)定
//按鈕控件中缺少
CalacutorButton::CalacutorButton(QWidget *parent):QPushButton(parent)
{
? ? pressed = false;
? ? mText = "";
? ? mIcon = "";
? ? mPressImgName = "/home/rabbitchenc/Image/btn_ime.png";
? ? mNormalImgName = "";//不添加圖片背景
? ? mFocusImgName = "";
? ? mDisableName = "";
? ? mFileName = mNormalImgName;


? ? connect(this,&QPushButton::pressed,[=](){
? ? ? ? pressed = true;
? ? ? ? setImageName(mPressImgName);

? ? });

? ? connect(this,&QPushButton::released,[=](){

? ? ? ? pressed = false;
? ? ? ? setImageName(mNormalImgName);
? ? });
}

CalacutorButton::~CalacutorButton()
{

}

void CalacutorButton::paintEvent(QPaintEvent *event)
{
?QPainter painter(this);
?painter.setRenderHint(QPainter::Antialiasing);
?painter.setRenderHint(QPainter::TextAntialiasing);
?drawImage(&painter);
?drawText(&painter);
?drawIcon(&painter);

}
void CalacutorButton::drawImage(QPainter*painter)
{
? ? painter->save();
? ? QPixmap pixmap;
? ? mWidth = width();
? ? mHeight = height();

? ? if(isEnabled()){
? ? ? ? if(isCheckable()){
? ? ? ? ? ? if(isChecked()){
? ? ? ? ? ? ? ? mFileName = mPressImgName;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? mFileName = mNormalImgName;
? ? ? ? ? ? }
? ? ? ? ? ? if(pressed){
? ? ? ? ? ? ? ? mFileName = mFocusImgName;
? ? ? ? ? ? }
? ? ? ? }
? ? }else {
// ? ? ? ?mFileName = mDisableName;
}

? ? pixmap = QPixmap( mFileName);
? ? painter->drawPixmap(0,0,mWidth,mHeight,pixmap);
? ? painter->restore();
}

?//添加文字
? void CalacutorButton::drawText(QPainter *painter)
? {
? ? ? painter->save();
? ? ? QFont font = painter->font();
? ? ? painter->drawText(0,0,mWidth,mHeight,Qt::AlignCenter,mText);
? ? ? painter->restore();
? }

? //添加圖標(biāo)
? void CalacutorButton::drawIcon(QPainter*painter)
? {
? ? ? painter->save();

? ? ? QPixmap pixmap(mIcon);
? ? ? if(pressed){
? ? ? ? ? painter->drawPixmap((width()-pixmap.width())/2,(height()-pixmap.height())/2,pixmap.width(),pixmap.height(),pixmap);
? ? ? }else{
? ? ? ? ? painter->drawPixmap((width()-pixmap.width())/2,(height()-pixmap.height())/2,pixmap.width(),pixmap.height(),pixmap);
? ? ? }

? ? ? painter->restore();
? }

?void CalacutorButton::setText(const QString&text)
?{
? ? ?mText = text;
? ? ?update();
?}


void CalacutorButton::setMyIcon(const QString &icon)
{
? ? mIcon = icon;
? ? update();
}
void CalacutorButton::setImageName(const QString &img)
{
? ? mFileName = img;

? ? update();
}


void CalacutorButton::setPressImg(const QString&img)
{
? ? mPressImgName = img;

? ? update();

}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C++計(jì)算ICMP頭的校驗(yàn)和實(shí)例

    C++計(jì)算ICMP頭的校驗(yàn)和實(shí)例

    這篇文章主要介紹了C++計(jì)算ICMP頭的校驗(yàn)和的方法,代碼簡(jiǎn)單實(shí)用,對(duì)于校驗(yàn)ICMP報(bào)文來(lái)說(shuō)有不錯(cuò)的實(shí)用價(jià)值,需要的朋友可以參考下
    2014-10-10
  • C++?Boost?weak_ptr智能指針超詳細(xì)講解

    C++?Boost?weak_ptr智能指針超詳細(xì)講解

    智能指針是一種像指針的C++對(duì)象,但它能夠在對(duì)象不使用的時(shí)候自己銷毀掉。雖然STL提供了auto_ptr,但是由于不能同容器一起使用(不支持拷貝和賦值操作),因此很少有人使用。它是Boost各組件中,應(yīng)用最為廣泛的一個(gè)
    2022-11-11
  • C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的抽獎(jiǎng)系統(tǒng)

    C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的抽獎(jiǎng)系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的抽獎(jiǎng)系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • C語(yǔ)言選擇排序算法及實(shí)例代碼

    C語(yǔ)言選擇排序算法及實(shí)例代碼

    本篇文章主要介紹了 C語(yǔ)言選擇排序算法,這里提供代碼實(shí)例以便大家理解,通過(guò)本文,更好的理解排序算法
    2016-07-07
  • C++異常處理方式實(shí)例詳解(超級(jí)詳細(xì)!)

    C++異常處理方式實(shí)例詳解(超級(jí)詳細(xì)!)

    程序有時(shí)會(huì)遇到運(yùn)行階段錯(cuò)誤,導(dǎo)致程序無(wú)法正常執(zhí)行下去,c++異常為處理這種情況提供了一種功能強(qiáng)大的而靈活的工具,下面這篇文章主要給大家介紹了關(guān)于C++異常處理方式的相關(guān)資料,需要的朋友可以參考下
    2023-04-04
  • C++代碼實(shí)現(xiàn)逆波蘭表達(dá)式

    C++代碼實(shí)現(xiàn)逆波蘭表達(dá)式

    這篇文章主要為大家詳細(xì)介紹了C++代碼實(shí)現(xiàn)逆波蘭表達(dá)式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • select函數(shù)實(shí)現(xiàn)高性能IO多路訪問(wèn)的關(guān)鍵示例深入解析

    select函數(shù)實(shí)現(xiàn)高性能IO多路訪問(wèn)的關(guān)鍵示例深入解析

    這篇文章主要為大家介紹了select函數(shù)實(shí)現(xiàn)高性能IO多路訪問(wèn)的關(guān)鍵示例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • C語(yǔ)言圖書管理系統(tǒng)簡(jiǎn)潔版

    C語(yǔ)言圖書管理系統(tǒng)簡(jiǎn)潔版

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言圖書管理系統(tǒng)簡(jiǎn)潔版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • 基于OpenCV和C++ 實(shí)現(xiàn)圖片旋轉(zhuǎn)

    基于OpenCV和C++ 實(shí)現(xiàn)圖片旋轉(zhuǎn)

    這篇文章主要介紹了基于OpenCV和C++ 實(shí)現(xiàn)圖片旋轉(zhuǎn),幫助大家更好的利用c++處理圖片,感興趣的朋友可以了解下
    2020-12-12
  • 使用C++實(shí)現(xiàn)位圖處理

    使用C++實(shí)現(xiàn)位圖處理

    本文介紹了如何使用C++語(yǔ)言處理位圖圖像,包括讀取、修改、保存等操作。通過(guò)具體的代碼示例,讀者可以學(xué)習(xí)到如何利用C++中的位運(yùn)算、數(shù)組和文件操作等知識(shí)點(diǎn)完成位圖處理任務(wù)。同時(shí),本文也提供了一些常用的圖像處理算法供讀者參考,幫助讀者更好地理解位圖處理過(guò)程
    2023-04-04

最新評(píng)論

广水市| 临西县| 靖安县| 闽侯县| 永和县| 岱山县| 炎陵县| 米易县| 卓资县| 孟州市| 南乐县| 阳谷县| 稷山县| 元氏县| 连南| 吴旗县| 宜良县| 瑞丽市| 平顶山市| 六枝特区| 青川县| 新蔡县| 沙田区| 内江市| 扎鲁特旗| 洛浦县| 宁海县| 阜康市| 永春县| 泾川县| 封丘县| 安多县| 临江市| 北碚区| 中阳县| 九江县| 龙里县| 集安市| 郸城县| 双柏县| 鹤庆县|