您的位置:首页 > 编程语言 > Qt开发

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

2011-11-30 16:46 696 查看
QT下没有可以直接用于输入IP这种字符串的控件,自己写了一个。
    可以实现根据用户设置的控件大小画出正确的外观,验证输入数字在正确范围内,提供了简单的代码设置获取IP的接口。
尚缺少连续输入和删除功能。
    废话少说,贴代码优先,详见注释~

//.h file
#ifndef IPLINEEDIT_H
#define IPLINEEDIT_H

#include <QLineEdit>

class COneIPLineEdit : public QLineEdit
{
    Q_OBJECT

public:
    COneIPLineEdit(QWidget *parent);
    ~COneIPLineEdit();

//signals:
//  void SignalFinished();
//
//public slots:
//  void SlotPreFinished();
//
//private:
//  void SlotTextChanged();

};

/*********************************************************************************/
class CIPLineEdit : public QLineEdit
{
    Q_OBJECT

public:
    CIPLineEdit(QWidget *parent);
    ~CIPLineEdit();
    void setGeometry(int x, int y, int w, int h);
    bool SetIPText(int nIP, int nIndex);
    int GetIPText(int nIndex);

private:
    void paintEvent(QPaintEvent *e);
    
private:
    COneIPLineEdit *m_pLineEdit[4];
};

#endif // IPLINEEDIT_H

//.cpp file
#include "IPLineEdit.h"
#include <QPainter>
#include <QPen>
#include <QRegExp>
#include <QValidator>

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

    setValidator(new QRegExpValidator(rx, this));

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

COneIPLineEdit::~COneIPLineEdit()
{

}

//void COneIPLineEdit::SlotPreFinished()
//{
//
//}
//
//void COneIPLineEdit::SlotTextChanged()
//{
//  if (text().length() == 3)
//  {
//
//  }
//}

/*******************************************************************************************/

CIPLineEdit::CIPLineEdit(QWidget *parent)
: QLineEdit(parent)
{
    
}

CIPLineEdit::~CIPLineEdit()
{

}

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

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

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

    painter.restore();
}

void CIPLineEdit::setGeometry( int x, int y, int w, int h )
{
    QLineEdit::setGeometry(x, y, w, h);

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

bool CIPLineEdit::SetIPText( int nIP, int nIndex )
{
    if (nIndex < 0 || nIndex > 3)
    {
        return false;
    }
    m_pLineEdit[nIndex]->setText(QString(nIP));
    return true;
}

int CIPLineEdit::GetIPText( int nIndex )
{
    if (nIndex < 0 || nIndex > 3)
    {
        return -1;
    }

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

//main file
#include <QtGui/QApplication>
#include <QMainWindow>
#include "IPLineEdit.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    
    QMainWindow mainwindow;
    mainwindow.setFixedSize(300, 200);

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

    mainwindow.show();
    a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    return a.exec();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐