QT5+VS自定义IP地址控件生成及使用

本文使用环境为QT5.9.7+VS2015,其他环境未做测试,大同小异。

VS新建QT自定义控件类:

QT5+VS自定义IP地址控件生成及使用_第1张图片

自动生成解决方案文件

QT5+VS自定义IP地址控件生成及使用_第2张图片

.修改QIPLineEdit.h

#pragma once

#include 
#include 

class QIPLineEdit : public QLineEdit
{
	Q_OBJECT

public:
	QIPLineEdit(QWidget *parent = 0);
	~QIPLineEdit();

	void setText(const QString &strIP);
	QString text() const;
protected:
	void paintEvent(QPaintEvent *event);
	bool eventFilter(QObject *obj, QEvent *ev);

	int getIndex(QLineEdit *pEdit);
	bool isTextValid(const QString &strIP);
private:
	QLineEdit *m_lineEidt[4];
};

QIPLineEdit.cpp

#include "QIPLineEdit.h"
#include 
#include 
#include 
#include 
#include 
#include 

QIPLineEdit::QIPLineEdit(QWidget *parent)
	: QLineEdit(parent)
{
	QRegExp rx("(2[0-5]{2}|2[0-4][0-9]|1?[0-9]{1,2})");
	QHBoxLayout *pHBox = new QHBoxLayout(this);
	pHBox->setSpacing(10);
	pHBox->setContentsMargins(0, 0, 0, 0);
	for (int i = 0; i < 4; i++)
	{
		m_lineEidt[i] = new QLineEdit(this);
		m_lineEidt[i]->setFrame(false);
		m_lineEidt[i]->setMaxLength(3);
		m_lineEidt[i]->setAlignment(Qt::AlignCenter);
		m_lineEidt[i]->installEventFilter(this);
		m_lineEidt[i]->setValidator(new QRegExpValidator(rx, this));
		m_lineEidt[i]->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
		pHBox->addWidget(m_lineEidt[i]);
	}
	this->setReadOnly(true);
}

QIPLineEdit::~QIPLineEdit()
{

}

void QIPLineEdit::paintEvent(QPaintEvent *event)
{
	__super::paintEvent(event);
	QPainter painter(this);
	QBrush brush;
	brush.setStyle(Qt::BrushStyle::SolidPattern);
	brush.setColor(Qt::black);
	painter.setBrush(brush);

	int width = 0;
	for (int i = 0; i < 3; i++)
	{
		width += m_lineEidt[i]->width() + (i == 0 ? 3 : 10);//布局的间隔
		painter.drawEllipse(width, height() / 2 - 2, 4, 4);
	}
}

int QIPLineEdit::getIndex(QLineEdit *pEdit)
{
	int index = -1;
	for (int i = 0; i < 4; i++)
	{
		if (pEdit == m_lineEidt[i])
			index = i;
	}
	return index;
}

bool QIPLineEdit::eventFilter(QObject *obj, QEvent *ev)
{
	if (children().contains(obj) && QEvent::KeyPress == ev->type())
	{
		QKeyEvent *keyEvent = dynamic_cast(ev);
		QLineEdit *pEdit = qobject_cast(obj);
		switch (keyEvent->key())
		{
		case Qt::Key_0:
		case Qt::Key_1:
		case Qt::Key_2:
		case Qt::Key_3:
		case Qt::Key_4:
		case Qt::Key_5:
		case Qt::Key_6:
		case Qt::Key_7:
		case Qt::Key_8:
		case Qt::Key_9:
		{
			QString strText = pEdit->text();
			if (pEdit->selectedText().length())
			{
				pEdit->text().replace(pEdit->selectedText(), QChar(keyEvent->key()));
			}
			else if (strText.length() == 3 || strText.length() < 3 && strText.toInt() * 10 > 255)
			{
				int index = getIndex(pEdit);
				if (index != -1 && index != 3)
				{
					m_lineEidt[index + 1]->setFocus();
					m_lineEidt[index + 1]->selectAll();
				}
			}
			else if (strText.length() == 2 && strText.toInt() * 10 < 255)
			{
				if (Qt::Key_0 == keyEvent->key() && strText.toInt())
				{
					pEdit->setText(strText.insert(pEdit->cursorPosition(), QChar(Qt::Key_0)));
				}
			}
			return __super::eventFilter(obj, ev);
		}
		break;
		case Qt::Key_Backspace:
		{
			QString strText = pEdit->text();
			if (!strText.length() || strText.length() && !pEdit->cursorPosition())
			{
				int index = getIndex(pEdit);
				if (index != -1 && index != 0)
				{
					m_lineEidt[index - 1]->setFocus();
					int length = m_lineEidt[index - 1]->text().length();
					m_lineEidt[index - 1]->setCursorPosition(length ? length : 0);
				}
			}
			return __super::eventFilter(obj, ev);
		}
		case Qt::Key_Left:
		{
			if (!pEdit->cursorPosition())
			{
				int index = getIndex(pEdit);
				if (index != -1 && index != 0)
				{
					m_lineEidt[index - 1]->setFocus();
					int length = m_lineEidt[index - 1]->text().length();
					m_lineEidt[index - 1]->setCursorPosition(length ? length : 0);
				}
			}
			return __super::eventFilter(obj, ev);
		}
		case Qt::Key_Right:
		{
			if (pEdit->cursorPosition() == pEdit->text().length())
			{
				int index = getIndex(pEdit);
				if (index != -1 && index != 3)
				{
					m_lineEidt[index + 1]->setFocus();
					m_lineEidt[index + 1]->setCursorPosition(0);
				}
			}
			return __super::eventFilter(obj, ev);
		}
		default:
			break;
		}
	}
	return false;
}

void QIPLineEdit::setText(const QString &strIP)
{
	if (!isTextValid(strIP))
	{
		QMessageBox::warning(this, "Attention", "Your IP Address is Invalid!", QMessageBox::StandardButton::Ok);
		return;
	}
	else
	{
		int i = 0;
		QStringList ipList = strIP.split(".");
		for each (QString ip in ipList)
		{
			m_lineEidt[i]->setText(ip);
			i++;
		}
	}
}

bool QIPLineEdit::isTextValid(const QString &strIP)
{
	QRegExp rx2("\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b");
	if (!rx2.exactMatch(strIP))
		return false;
	return true;
}

QString QIPLineEdit::text() const
{
	QString strIP;
	for (int i = 0; i < 4; i++)
		strIP.append(m_lineEidt[i]->text());
	return strIP;
}

编译生成.dll及.lib,拷贝.dll到对应VS安装控件目录

QT5+VS自定义IP地址控件生成及使用_第3张图片

拷贝QIPLineEdit.h及QIPLineEdit.cpp文件到使用自定义控件类的工程目录并添加

现在我们可以在QT类界面编辑器中看到我们的控件了,拖入直接使用。

QT5+VS自定义IP地址控件生成及使用_第4张图片

到此OK,nice.

不过这样也不属于封装,只是方便而已,后续再研究~

你可能感兴趣的:(QT-界面开发)