Results 1 to 13 of 13

Thread: How To Get System Network Interface List in using qt4.5

  1. #1
    Join Date
    Mar 2009
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How To Get System Network Interface List in using qt4.5

    hai,
    i am new to qt and linux..my problem..in my project we display list network interface using qt4.5 version in ferdora 8.i am using platform is c++..i am see Qnetworkinterface but i am code some ....but it gives some errors...pls help me any one....i am send code..this wrong give right way...once again help me...
    thanks in adavance....
    Q_GLOBAL_STATIC(QNetworkInterfaceManager, manager)

    QNetworkInterfaceManager::QNetworkInterfaceManager ()
    {
    }

    QNetworkInterfaceManager::~QNetworkInterfaceManage r()
    {
    }

    QSharedDataPointer<QNetworkInterfacePrivate> QNetworkInterfaceManager::interfaceFromName(const QString &name)
    {
    QList<QSharedDataPointer<QNetworkInterfacePrivate> > interfaceList = allInterfaces();
    QList<QSharedDataPointer<QNetworkInterfacePrivate> >::ConstIterator it = interfaceList.constBegin();
    for ( ; it != interfaceList.constEnd(); ++it)
    if ((*it)->name == name)
    return *it;

    return empty;
    }

    QSharedDataPointer<QNetworkInterfacePrivate> QNetworkInterfaceManager::interfaceFromIndex(int index)
    {
    QList<QSharedDataPointer<QNetworkInterfacePrivate> > interfaceList = allInterfaces();
    QList<QSharedDataPointer<QNetworkInterfacePrivate> >::ConstIterator it = interfaceList.constBegin();
    for ( ; it != interfaceList.constEnd(); ++it)
    if ((*it)->index == index)
    return *it;

    return empty;
    }

    QList<QSharedDataPointer<QNetworkInterfacePrivate> > QNetworkInterfaceManager::allInterfaces()
    {
    QList<QNetworkInterfacePrivate *>list=scan();
    QList<QSharedDataPointer<QNetworkInterfacePrivate> > result;

    foreach (QNetworkInterfacePrivate *ptr, list)
    result << QSharedDataPointer<QNetworkInterfacePrivate>(ptr);

    return result;
    }

    QString QNetworkInterfacePrivate::makeHwAddress(int len, uchar *data)
    {
    QString result;
    for (int i = 0; i < len; ++i) {
    if (i)
    result += QLatin1Char(':');

    char buf[3];
    #if defined(Q_OS_Lin) && defined(_MSC_VER) && _MSC_VER >= 1400
    sprintf_s(buf, 3, "%02hX", ushort(data[i]));
    #else
    sprintf(buf, "%02hX", ushort(data[i]));
    #endif
    result += QLatin1String(buf);
    }
    return result;
    }


    QNetworkAddressEntry::QNetworkAddressEntry()
    : d(new QNetworkAddressEntryPrivate)
    {
    }

    /*!
    Constructs a QNetworkAddressEntry object that is a copy of the
    object \a other.
    */
    QNetworkAddressEntry::QNetworkAddressEntry(const QNetworkAddressEntry &other)
    : d(new QNetworkAddressEntryPrivate(*other.d))
    {
    }



    QNetworkInterface::QNetworkInterface()
    : d(0)
    {
    }

    /*!
    Frees the resources associated with the QNetworkInterface object.
    */
    QNetworkInterface::~QNetworkInterface()
    {
    }

    /*!
    Creates a copy of the the QNetworkInterface object contained in \a
    other.
    */
    QNetworkInterface::QNetworkInterface(const QNetworkInterface &other)
    : d(other.d)
    {
    }

    /*!
    Copies the contents of the QNetworkInterface object contained in \a
    other into this one.
    */
    QNetworkInterface &QNetworkInterface:perator=(const QNetworkInterface &other)
    {
    d = other.d;
    return *this;
    }

    /*!
    Returns true if this QNetworkInterface object contains valid
    information about a network interface.
    */
    bool QNetworkInterface::isValid() const
    {
    return !name().isEmpty();
    }

    /*!
    Returns the name of this network interface. On Unix systems, this
    is a string containing the type of the interface and optionally a
    sequence number, such as "eth0", "lo" or "pcn0". On Windows, it's
    an internal ID that cannot be changed by the user.
    */
    QString QNetworkInterface::name() const
    {
    return d ? d->name : QString();
    }

    /*!
    Returns the flags associated with this network interface.
    */
    QNetworkInterface::InterfaceFlags QNetworkInterface::flags() const
    {
    return d ? d->flags : InterfaceFlags(0);
    }

    /*!
    Returns the low-level hardware address for this interface. On
    Ethernet interfaces, this will be a MAC address in string
    representation, separated by colons.

    Other interface types may have other types of hardware
    addresses. Implementations should not depend on this function
    returning a valid MAC address.
    */
    QString QNetworkInterface::hardwareAddress() const
    {
    return d ? d->hardwareAddress : QString();
    }

    /*!
    Returns the list of IP addresses that this interface possesses
    along with their associated netmasks and broadcast addresses.

    If the netmask or broadcast address information is not necessary,
    you can call the addresses() function to obtain just the IP
    addresses.
    */
    QList<QNetworkAddressEntry> QNetworkInterface::addressEntries() const
    {
    return d ? d->addressEntries : QList<QNetworkAddressEntry>();
    }

    /*!
    Returns a QNetworkInterface object for the interface named \a
    name. If no such interface exists, this function returns an
    invalid QNetworkInterface object.

    \sa name(), isValid()
    */
    QNetworkInterface QNetworkInterface::interfaceFromName(const QString &name)
    {
    QNetworkInterface result;
    result.d = manager()->interfaceFromName(name);
    return result;
    }

    /*!
    Returns a QNetworkInterface object for the interface whose internal
    ID is \a index. Network interfaces have a unique identifier called
    the "interface index" to distinguish it from other interfaces on
    the system. Often, this value is assigned progressively and
    interfaces being removed and then added again get a different
    value every time.

    This index is also found in the IPv6 address' scope ID field.
    */
    QNetworkInterface QNetworkInterface::interfaceFromIndex(int index)
    {
    QNetworkInterface result;
    result.d = manager()->interfaceFromIndex(index);
    return result;
    }

    /*!
    Returns a listing of all the network interfaces found on the host
    machine.
    */
    QList<QNetworkInterface> QNetworkInterface::allInterfaces()
    {
    QList<QSharedDataPointer<QNetworkInterfacePrivate> > privs = manager()->allInterfaces();
    QList<QNetworkInterface> result;
    foreach (QSharedDataPointer<QNetworkInterfacePrivate> p, privs) {
    QNetworkInterface item;
    item.d = p;
    result << item;
    }

    return result;
    }

    /*!
    This convenience function returns all IP addresses found on the
    host machine. It is equivalent to calling addresses() in all the
    objects returned by allInterfaces().
    */
    QList<QHostAddress> QNetworkInterface::allAddresses()
    {
    QList<QSharedDataPointer<QNetworkInterfacePrivate> > privs = manager()->allInterfaces();
    QList<QHostAddress> result;
    foreach (const QSharedDataPointer<QNetworkInterfacePrivate> p, privs) {
    foreach (const QNetworkAddressEntry &entry, p->addressEntries)
    result += entry.ip();
    }

    return result;
    }

    #ifndef QT_NO_DEBUG_STREAM
    static inline QDebug flagsDebug(QDebug debug, QNetworkInterface::InterfaceFlags flags)
    {
    if (flags & QNetworkInterface::IsUp)
    debug.nospace() << "IsUp ";
    if (flags & QNetworkInterface::IsRunning)
    debug.nospace() << "IsRunning ";
    if (flags & QNetworkInterface::CanBroadcast)
    debug.nospace() << "CanBroadcast ";
    if (flags & QNetworkInterface::IsLoopBack)
    debug.nospace() << "IsLoopBack ";
    if (flags & QNetworkInterface::IsPointToPoint)
    debug.nospace() << "IsPointToPoint ";
    if (flags & QNetworkInterface::CanMulticast)
    debug.nospace() << "CanMulticast ";
    return debug.nospace();
    }

    static inline QDebug operator<<(QDebug debug, const QNetworkAddressEntry &entry)
    {
    debug.nospace() << "(address = " << entry.ip();
    if (!entry.netmask().isNull())
    debug.nospace() << ", netmask = " << entry.netmask();
    if (!entry.broadcast().isNull())
    debug.nospace() << ", broadcast = " << entry.broadcast();
    debug.nospace() << ")";
    return debug.space();
    }

    QDebug operator<<(QDebug debug, const QNetworkInterface &interface)
    {
    debug.nospace() << "QNetworkInterface(name = " << interface.name()
    << ", hardware address = " << interface.hardwareAddress()
    << ", flags = ";
    flagsDebug(debug, interface.flags());
    debug.nospace() << ", entries = " << interface.addressEntries()
    << ")\n";
    return debug.space();
    }
    #endif

  2. #2
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How To Get System Network Interface List in using qt4.5

    just use QNetworkInterface::allInterfaces().
    Example:
    Qt Code:
    1. QList<QNetworkInterface> list = QNetworkInterface::allInterfaces(); // now you have interfaces list
    2. foreach (QNetworkInterface iface, list) // this should print all interfaces' names
    3. {
    4. qDebug() << iface.name();
    5. }
    To copy to clipboard, switch view to plain text mode 
    Didn't test it, but it should be something like this.

    P.S. Use CODE tags for pasting code.
    P.S.2. It's better to see your code where you want to solve your problem than Qt source code. And you can read about almost every class and method in Qt Assistant, or on http://doc.trolltech.com
    Last edited by faldzip; 18th March 2009 at 07:03. Reason: typo...
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How To Get System Network Interface List in using qt4.5

    PS3 ... Its better to submit long codes as attachments / zip files

  4. #4
    Join Date
    Mar 2009
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How To Get System Network Interface List in using qt4.5

    Thanks for ur replays...i can use that code, can do something like but it gives error..please very this and please correct way.....pls help me...
    QList<QNetworkInterface> NetList = QNetworkInterface::allInterfaces();
    int NetCount = 0;
    int Neti = 0;
    QString MacStr;
    QNetworkInterface thisNet;
    NetCount = NetList.count();
    for(Neti = 0;Neti < NetCount; Neti++)
    {
    if(NetList[Neti].isValid())
    {
    thisNet = NetList[Neti];
    break;
    }
    qDebug() << thisNet.name();
    }

    MacStr = thisNet.hardwareAddress();

  5. #5
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How To Get System Network Interface List in using qt4.5

    Use CODE tags in posts

    And what error did you get?
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  6. #6
    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 To Get System Network Interface List in using qt4.5

    ...for me it works fine

  7. #7
    Join Date
    Mar 2009
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How To Get System Network Interface List in using qt4.5

    its working but the network interface not display in the form i can use this way...in if condition
    Qt Code:
    1. ui.combobox->addItem(NetInter.Name)...
    2. but it gives error in the ui is not declared in scope..
    3. pls give any wrong this process
    To copy to clipboard, switch view to plain text mode 
    Last edited by rakesh; 18th March 2009 at 12:51. Reason: missing [code] tags

  8. #8
    Join Date
    Mar 2009
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How To Get System Network Interface List in using qt4.5

    how to display in combobox,how to work in combobox...

  9. #9
    Join Date
    Mar 2009
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How To Get System Network Interface List in using qt4.5

    hai,any one help me...for display items in combobox..i am connect combobox to class after how to display networkInterface list in combobox...for using slots pls help me ...any one..i saw slots using code , i can try but its not working...
    void QIP::setInterface(int index)
    {
    qDebug()<<"Setting up new interface...\n";
    networkInterface = networkList.at(index);
    qDebug()<<"Network name: "<<networkInterface.name();
    qDebug()<<"\t[+] Done...\n";
    }
    give error in 'at' is not declared in the scope...whats problem...pls help me..any one....

  10. #10
    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 To Get System Network Interface List in using qt4.5

    1. Use the *'*"?$§%-CODE tags
    2. Reconsider your way of asking. Sorry but I don't understand your problem.


    What's 'networkList'? What error do you get? Out of range? Have you included all necessary header...

  11. #11
    Join Date
    Mar 2009
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How To Get System Network Interface List in using qt4.5

    i am retrive network interfaces in combobox,but i am select etho network in combobox...it gives systems crash error..i am using qt4.5 version...what is problem...help me any one

  12. #12
    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 To Get System Network Interface List in using qt4.5

    Ok, you do something like that:
    Qt Code:
    1. QComboBox *box = new QComboBox(this);
    2. QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();
    3. foreach (QNetworkInterface iface, list)
    4. {
    5. box->addItem(iface.name());
    6. }
    To copy to clipboard, switch view to plain text mode 
    And then? Which slot is connected to currentIndexChanged()? It's highly probably that the error occurs there. But without any code it's hardly to say. Maybee you could also send the debugger output...

  13. #13
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How To Get System Network Interface List in using qt4.5

    I think that your problem is simple: lack of basic knowlage of C++ and Qt GUI programming. My guess is that networkList was declared in other method so it no longer exist. What's more:
    1. Use CODE tags :]
    2. Try to change your way of posting and asking - typing many "please help me" instead of just one sentance describing errors you get is a bad idea - it's hard to help if we don't know much about your problem.
    3. I think that you are not reading our answer carefully or you dont understand it (because of english) or something like that, because we ask you something and your reply isn't connected with what we say. (like CODE tags :P)
    4. Summary: Read some C++ tutorial carefully and get trough some Qt Examples and demos for begginers.

    Good luck!
    Last edited by faldzip; 22nd March 2009 at 11:23. Reason: typo...
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

Similar Threads

  1. Replies: 0
    Last Post: 18th March 2009, 06:33
  2. system network adapter list
    By rakesh in forum Qt Programming
    Replies: 0
    Last Post: 17th March 2009, 13:02
  3. network adapter list display
    By rakesh in forum Qt Tools
    Replies: 0
    Last Post: 17th March 2009, 12:16

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.