打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
用QT实现的用于显示输入IP的LineEdit控件
userphoto

2015.07.16

关注
QT下没有可以直接用于输入IP这种字符串的控件,自己写了一个。
    可以实现根据用户设置的控件大小画出正确的外观,验证输入数字在正确范围内,提供了简单的代码设置获取IP的接口。
尚缺少连续输入和删除功能。
    废话少说,贴代码优先,详见注释~
  1. //.h file
  2. #ifndef IPLINEEDIT_H
  3. #define IPLINEEDIT_H

  4. #include <QLineEdit>

  5. class COneIPLineEdit : public QLineEdit
  6. {
  7.     Q_OBJECT

  8. public:
  9.     COneIPLineEdit(QWidget *parent);
  10.     ~COneIPLineEdit();

  11. //signals:
  12. //  void SignalFinished();
  13. //
  14. //public slots:
  15. //  void SlotPreFinished();
  16. //
  17. //private:
  18. //  void SlotTextChanged();

  19. };

  20. /*********************************************************************************/
  21. class CIPLineEdit : public QLineEdit
  22. {
  23.     Q_OBJECT

  24. public:
  25.     CIPLineEdit(QWidget *parent);
  26.     ~CIPLineEdit();
  27.     void setGeometry(int x, int y, int w, int h);
  28.     bool SetIPText(int nIP, int nIndex);
  29.     int GetIPText(int nIndex);

  30. private:
  31.     void paintEvent(QPaintEvent *e);
  32.     
  33. private:
  34.     COneIPLineEdit *m_pLineEdit[4];
  35. };

  36. #endif // IPLINEEDIT_H



  37. //.cpp file
  38. #include "IPLineEdit.h"
  39. #include <QPainter>
  40. #include <QPen>
  41. #include <QRegExp>
  42. #include <QValidator>

  43. COneIPLineEdit::COneIPLineEdit( QWidget *parent )
  44. :QLineEdit(parent)
  45. {
  46.     setAlignment(Qt::AlignHCenter);
  47.     setMaxLength(3);
  48.     setMinimumSize(25, 20);
  49.     QRegExp rx("(2[0-5]{2}|2[0-4][0-9]|1?[0-9]{1,2})");

  50.     setValidator(new QRegExpValidator(rx, this));

  51.     connect(this, SIGNAL(textChanged(QString)), this, SLOT(SlotTextChanged()));
  52. }

  53. COneIPLineEdit::~COneIPLineEdit()
  54. {

  55. }

  56. //void COneIPLineEdit::SlotPreFinished()
  57. //{
  58. //
  59. //}
  60. //
  61. //void COneIPLineEdit::SlotTextChanged()
  62. //{
  63. //  if (text().length() == 3)
  64. //  {
  65. //
  66. //  }
  67. //}

  68. /*******************************************************************************************/

  69. CIPLineEdit::CIPLineEdit(QWidget *parent)
  70. : QLineEdit(parent)
  71. {
  72.     
  73. }

  74. CIPLineEdit::~CIPLineEdit()
  75. {

  76. }

  77. void CIPLineEdit::paintEvent( QPaintEvent *e )
  78. {
  79.     QLineEdit::paintEvent(e);
  80.     
  81.     int nWidth = width() - 5;//3个点,两头边框各一个像素
  82.     int nAverageWidth = nWidth / 4;
  83.     int nPointY = height() / 2;
  84.     int nTrans = 0;
  85.     //若除以4且算上点、框宽度之后仍有2或3的空余,则左边框留两个像素显示
  86.     if (nWidth - nAverageWidth * 4 - 5 >= 2)
  87.     {
  88.         nTrans = 1;
  89.     }

  90.     QPainter painter(this);
  91.     painter.setPen(QPen(Qt::black));
  92.     painter.save();

  93.     for (int i = 0; i < 3; i++)
  94.     {
  95.         painter.drawPoint(nAverageWidth*(i+1) + i+1 + nTrans, nPointY);
  96.     }

  97.     painter.restore();
  98. }

  99. void CIPLineEdit::setGeometry( int x, int y, int w, int h )
  100. {
  101.     QLineEdit::setGeometry(x, y, w, h);

  102.     int nWidth = w - 5;//3个点,两头边框各一个像素
  103.     int nAverageWidth = nWidth / 4;
  104.     int nAverageHeight = h - 2;
  105.     int nTrans = 0;
  106.     //若除以4且算上点、框宽度之后仍有2或3的空余,则左边框留两个像素显示
  107.     if (nWidth - nAverageWidth * 4 - 5 >= 2)
  108.     {
  109.         nTrans = 1;
  110.     }
  111.     for (int i = 0; i < 4; i++)
  112.     {
  113.         m_pLineEdit[i] = new COneIPLineEdit(this);
  114.         m_pLineEdit[i]->setFrame(false);
  115.         m_pLineEdit[i]->setGeometry(nAverageWidth*i+ i + 1, 1, nAverageWidth, nAverageHeight);
  116.     }
  117. }

  118. bool CIPLineEdit::SetIPText( int nIP, int nIndex )
  119. {
  120.     if (nIndex < 0 || nIndex > 3)
  121.     {
  122.         return false;
  123.     }
  124.     m_pLineEdit[nIndex]->setText(QString(nIP));
  125.     return true;
  126. }

  127. int CIPLineEdit::GetIPText( int nIndex )
  128. {
  129.     if (nIndex < 0 || nIndex > 3)
  130.     {
  131.         return -1;
  132.     }

  133.     return m_pLineEdit[nIndex]->text().toInt();
  134. }

  135. //main file
  136. #include <QtGui/QApplication>
  137. #include <QMainWindow>
  138. #include "IPLineEdit.h"

  139. int main(int argc, char *argv[])
  140. {
  141.     QApplication a(argc, argv);
  142.     
  143.     QMainWindow mainwindow;
  144.     mainwindow.setFixedSize(300, 200);

  145.     CIPLineEdit *pIPLineEdit = new CIPLineEdit(&mainwindow);
  146.     pIPLineEdit->setGeometry(50, 50, 120, 26);

  147.     mainwindow.show();
  148.     a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
  149.     return a.exec();
  150. }

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Java读取BMP格式图片(源代码,转)
读取24位BMP图像并生成JPG缩略图
书写函数规范
Qt之Tab键切换焦点顺序
rgb
在CListCtrl中多项拖动时显示图标
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服