用QT实现的用于显示输入IP的LineEdit控件

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

你可能感兴趣的:(QT,qt,signal)