PDA

View Full Version : How do increment/decrement an String IP Address?



premroxx
23rd March 2012, 07:11
my ip address is declared as : const QString &ip

How can i increment/decrement the ip addresses stored in this string. (for ex: ip=192.168.1.1, then increment is 192.168.1.2)?

Lykurg
23rd March 2012, 08:48
Split the string by '.'. Convert the last to integer (toInt()), increment, convert it to a string (QString::number()) and join the previous splited strings together.

ChrisW67
23rd March 2012, 08:55
Ignoring network masks and other potential errors:


QString ip("192.168.1.1");

QHostAddress next(QHostAddress(ip).toIPv4Address() + 1);
qDebug() << next.toString();

premroxx
23rd March 2012, 18:35
Am i doing something wrong here?
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QDebug"
#include "QString"
#include "QtNetwork/qhostaddress.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

QString ip("192.168.1.1");
QHostAddress next(QHostAddress(ip).toIPv4Address() + 1);
qDebug() << next.toString();
}

I am getting the following LINK errors

mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: class QString __thiscall QHostAddress::toString(void)const " (__imp_?toString@QHostAddress@@QBE?AVQString@@XZ) referenced in function "public: __thiscall MainWindow::MainWindow(class QWidget *)" (??0MainWindow@@QAE@PAVQWidget@@@Z)
mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QHostAddress::~QHostAddress(void)" (__imp_??1QHostAddress@@QAE@XZ) referenced in function "public: __thiscall MainWindow::MainWindow(class QWidget *)" (??0MainWindow@@QAE@PAVQWidget@@@Z)
mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QHostAddress::QHostAddress(unsigned int)" (__imp_??0QHostAddress@@QAE@I@Z) referenced in function "public: __thiscall MainWindow::MainWindow(class QWidget *)" (??0MainWindow@@QAE@PAVQWidget@@@Z)
mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: unsigned int __thiscall QHostAddress::toIPv4Address(void)const " (__imp_?toIPv4Address@QHostAddress@@QBEIXZ) referenced in function "public: __thiscall MainWindow::MainWindow(class QWidget *)" (??0MainWindow@@QAE@PAVQWidget@@@Z)
mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QHostAddress::QHostAddress(class QString const &)" (__imp_??0QHostAddress@@QAE@ABVQString@@@Z) referenced in function "public: __thiscall MainWindow::MainWindow(class QWidget *)" (??0MainWindow@@QAE@PAVQWidget@@@Z)
debug\tag.exe : fatal error LNK1120: 5 unresolved externals

ChrisW67
23rd March 2012, 22:04
Yes ;)


Try adding:


QT += network

to your pro file.