Results 1 to 5 of 5

Thread: How do increment/decrement an String IP Address?

  1. #1
    Join Date
    Aug 2011
    Posts
    42
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default How do increment/decrement an String IP Address?

    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)?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How do increment/decrement an String IP Address?

    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.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How do increment/decrement an String IP Address?

    Ignoring network masks and other potential errors:
    Qt Code:
    1. QString ip("192.168.1.1");
    2.  
    3. QHostAddress next(QHostAddress(ip).toIPv4Address() + 1);
    4. qDebug() << next.toString();
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to ChrisW67 for this useful post:

    premroxx (23rd March 2012)

  5. #4
    Join Date
    Aug 2011
    Posts
    42
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How do increment/decrement an String IP Address?

    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

  6. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How do increment/decrement an String IP Address?

    Yes


    Try adding:
    Qt Code:
    1. QT += network
    To copy to clipboard, switch view to plain text mode 
    to your pro file.

  7. The following user says thank you to ChrisW67 for this useful post:

    premroxx (23rd March 2012)

Similar Threads

  1. QProgressBar doesn't increment!!
    By riskio in forum Qt Programming
    Replies: 6
    Last Post: 16th September 2011, 12:57
  2. QListWidget string increment
    By nagabathula in forum Qt Programming
    Replies: 2
    Last Post: 20th December 2010, 09:19
  3. Increment and Decrement Program
    By priya in forum Qt Programming
    Replies: 3
    Last Post: 3rd February 2010, 13:27
  4. QSqlQuery: Getting an Auto Increment value
    By Al_ in forum Qt Programming
    Replies: 3
    Last Post: 23rd December 2009, 22:15
  5. Custom spinner - fast increment/decrement
    By MrGarbage in forum Qt Programming
    Replies: 2
    Last Post: 11th October 2007, 17:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.