c/c++语言开发共享Qt简单实现密码器控件

本文实例为大家分享了qt自定义一个密码器控件的简单实现代码,供大家参考,具体内容如下实现构思:密码器的功能可以看成是计算器和登陆界面的组合,所以在实现功能的过程中借鉴了大神的计算器的实现代码和登陆界面

c/c++开发分享Qt简单实现密码器控件实例为大家分享了qt自定义一个密码器控件的简单实现代码,供大家参考,具体内容如下

实现构思:

密码器的功能可以看成是计算器和登陆界面的组合,所以在实现功能的过程中借鉴了大神的计算器的实现代码和登陆界面实现的代码。

实现的效果:

Qt简单实现密码器控件

关于密码器控件的不足:

窗口的标题栏不够漂亮,但是由于对时间长度和任务进度的权衡,下次一定进行重绘。

代码思路:

由于我司不用样式表,所以背景由贴图函数完成。在widget中添加按钮控件和文本编辑控件。使用布局函数进行布局,在加上一些简单的逻辑处理功能即可。

首先创建一个工程文件,添加新文件,选择qt 设计师界面类,如下:

Qt简单实现密码器控件

进入创建的ui界面后,添加控件进行布局,单一的使用了珊格布局,如下:

Qt简单实现密码器控件

在自定义控件的布局中遇到了一些与布局相关的问题:

问题1:如何改变布局内控件的大小? ui中修改方式如下,纯代码实现也可以去帮助手册中查找相同的接口函数。

Qt简单实现密码器控件

问题2:布局中控件的位置如何进行更改?

*ui->gridlayout->setcontentsmargins(qmargins(10,60,0,0));  ui->gridlayout->setverticalspacing(10);*

具体size,自行可以调整到比较合适的位置。

源码实现:

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();//可以进行提供一个背景图片  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,"错误提示","密码错误");          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>    //增加对按钮类型的设定  //按钮控件中缺少  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();    }      //添加图标    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();    }

以上就是c/c++开发分享Qt简单实现密码器控件的全部内容,希望对大家的学习有所帮助,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。

需要了解更多c/c++开发分享Qt简单实现密码器控件,都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/1237862.html

(0)
上一篇 2022年9月11日
下一篇 2022年9月11日

精彩推荐