1 Star 0 Fork 1

第三方项目代码/QIpEditor

forked from Jackal/QIpEditor 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
qipeditor.cpp 3.19 KB
一键复制 编辑 原始数据 按行查看 历史
Jackal 提交于 2014-11-11 12:18 +08:00 . Init sources
#include "qipeditor.h"
#include "ui_qipeditor.h"
#include <QList>
#include <QtNetwork/QHostAddress>
#include <QKeyEvent>
QIpEditor::QIpEditor(QWidget *parent) :
QWidget(parent),
ui(new Ui::QIpEditor)
{
ui->setupUi(this);
ui->spinBox1->installEventFilter(this);
ui->spinBox2->installEventFilter(this);
ui->spinBox3->installEventFilter(this);
ui->spinBox4->installEventFilter(this);
}
QIpEditor::~QIpEditor()
{
delete ui;
}
QString QIpEditor::ipAddressString()
{
QString ip = QString("%1.%2.%3.%4")
.arg(ui->spinBox1->value())
.arg(ui->spinBox2->value())
.arg(ui->spinBox3->value())
.arg(ui->spinBox4->value());
return ip;
}
void QIpEditor::setIpAddress(QString &string)
{
QStringList list = string.split(".", QString::SkipEmptyParts);
int i;
int temp;
bool changed = false;
for(i = 0;i < list.count(); i++){
switch(i){
case 0:
temp = list[i].toInt();
if(temp != ui->spinBox1->value()){
changed = true;
ui->spinBox1->setValue(temp);
}
break;
case 1:
temp = list[i].toInt();
if(temp != ui->spinBox1->value()){
changed = true;
ui->spinBox2->setValue(temp);
}
break;
case 2:
temp = list[i].toInt();
if(temp != ui->spinBox1->value()){
changed = true;
ui->spinBox3->setValue(temp);
}
break;
case 3:
temp = list[i].toInt();
if(temp != ui->spinBox1->value()){
changed = true;
ui->spinBox4->setValue(temp);
}
break;
default:
break;
}
}
if(changed){
emit ipAddressStringChanged(ipAddressString());
}
}
void QIpEditor::spinBoxValueChanged()
{
emit ipAddressStringChanged(ipAddressString());
}
bool QIpEditor::eventFilter(QObject *object, QEvent *event)
{
QList<QSpinBox *> listOfQSpinBox;
listOfQSpinBox.append(ui->spinBox1);
listOfQSpinBox.append(ui->spinBox2);
listOfQSpinBox.append(ui->spinBox3);
listOfQSpinBox.append(ui->spinBox4);
QSpinBox *spinBox = NULL;
foreach (QSpinBox *box, listOfQSpinBox) {
if(box == object){
spinBox = box;
}
}
if(spinBox){
if(QEvent::FocusIn == event->type()){
spinBox->selectAll();
return true;
}
if(QEvent::KeyRelease == event->type()){
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
int context = spinBox->value();
if(keyEvent->key() != Qt::Key_Tab){
if((context > 25 || context > 99)){
this->focusNextChild();
// listOfQSpinBox[(listOfQSpinBox.indexOf(spinBox) + 1) % listOfQSpinBox.count()]->setFocus();
}else if(spinBox->text().length() >= 3){
this->focusNextChild();
}else if(keyEvent->key() < Qt::Key_0 || keyEvent->key() > Qt::Key_9){
this->focusNextChild();
}
}
}
}
return false;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/third_party_project_code/QIpEditor.git
git@gitee.com:third_party_project_code/QIpEditor.git
third_party_project_code
QIpEditor
QIpEditor
master

搜索帮助