本文整理汇总了C++中QNetworkInterface类的典型用法代码示例。如果您正苦于以下问题:C++ QNetworkInterface类的具体用法?C++ QNetworkInterface怎么用?C++ QNetworkInterface使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了QNetworkInterface类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: QNetworkInterface
QString DeviceManager::getDeviceIP()
{
QNetworkInterface *qni;
qni = new QNetworkInterface();
*qni = qni->interfaceFromName(QString("%1").arg("wlan0"));
return qni->addressEntries().at(0).ip().toString();
}
示例2: settings
bool
Utilities::isOnline(QNetworkAccessManager *manager)
{
bool ret = false;
QSettings settings("Entomologist");
if (settings.value("work-offline", false).toBool())
return(false);
#if QT_VERSION >= 0x040700
if (manager->networkAccessible() != QNetworkAccessManager::NotAccessible)
ret = true;
else
ret = false;
#else
QNetworkInterface iface;
QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
for (int i = 0; i < ifaces.count(); ++i)
{
iface = ifaces.at(i);
if (iface.flags().testFlag(QNetworkInterface::IsUp)
&& !iface.flags().testFlag(QNetworkInterface::IsLoopBack) )
{
if (iface.addressEntries().count() >= 1)
{
ret = true;
break;
}
}
}
#endif
return(ret);
}
示例3: nativeSetMulticastInterface
bool QNativeSocketEnginePrivate::nativeSetMulticastInterface(const QNetworkInterface &iface)
{
#ifndef QT_NO_IPV6
if (socketProtocol == QAbstractSocket::IPv6Protocol) {
uint v = iface.isValid() ? iface.index() : 0;
return (::setsockopt(socketDescriptor, IPPROTO_IPV6, IPV6_MULTICAST_IF, (char *) &v, sizeof(v)) != -1);
}
#endif
struct in_addr v;
if (iface.isValid()) {
QList<QNetworkAddressEntry> entries = iface.addressEntries();
for (int i = 0; i < entries.count(); ++i) {
const QNetworkAddressEntry &entry = entries.at(i);
const QHostAddress &ip = entry.ip();
if (ip.protocol() == QAbstractSocket::IPv4Protocol) {
v.s_addr = htonl(ip.toIPv4Address());
int r = ::setsockopt(socketDescriptor, IPPROTO_IP, IP_MULTICAST_IF, (char *) &v, sizeof(v));
if (r != -1)
return true;
}
}
return false;
}
v.s_addr = INADDR_ANY;
return (::setsockopt(socketDescriptor, IPPROTO_IP, IP_MULTICAST_IF, (char *) &v, sizeof(v)) != -1);
}
示例4: QDialog
/* public ***********************************************/
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui_(new Ui::Dialog), Server_(0), Client_(0) {
LOG << "Dialog init.";
ui_->setupUi(this);
QNetworkInterface *inter = new QNetworkInterface();
QList<QHostAddress> list;
list = inter->allAddresses();
QString hostname = "";
for (int i = 0; i < list.size(); ++i) {
QHostAddress tmp(list.at(i).toIPv4Address());
if (tmp.toString().compare("127.0.0.1") && tmp.toString().compare("0.0.0.0"))
hostname = tmp.toString();
}
// ui_->serverIPEdit->setText(hostname);
ui_->serverIPEdit->setText("127.0.0.1");
ui_->serverPortEdit->setText("11111");
ui_->serverMsgEdit->setText("Hello");
// ui_->serverFileEdit->setText("/home/dn/yabss.crt");
ui_->clientIPEdit->setText("127.0.0.1");
ui_->clientPortEdit->setText("11111");
ui_->clientFileWidget->hide();
// ui_->recieveFileEdit->setText("/tmp/");
}
示例5: qWarning
void ImageProcessing::checkNetwork()
{
bool running = false;
QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
qWarning() << __FUNCTION__ << ": Running the network check";
if (pNextImage->isActive()) {
qWarning() << __FUNCTION__ << ": Turning off image display timer";
pNextImage->stop();
}
for (int i = 0; i < interfaces.size(); i++) {
QNetworkInterface ni = interfaces[i];
if (ni.name() != "wlan0")
continue;
qWarning() << __FUNCTION__ << ": Checking interface" << ni.name() << " with flags" << ni.flags();
if (ni.flags().testFlag(QNetworkInterface::IsUp) && ni.flags().testFlag(QNetworkInterface::IsRunning)) {
running = true;
getContentList();
}
}
if (!running) {
qWarning() << __FUNCTION__ << ": Network check didn't find an available interface";
showLocalImage();
}
}
示例6: foreach
// Go through all interfaces and add all IPs for interfaces that can
// broadcast and are not local.
foreach (QNetworkInterface networkInterface,
QNetworkInterface::allInterfaces())
{
if (!networkInterface.addressEntries().isEmpty()
&& networkInterface.isValid())
{
QNetworkInterface::InterfaceFlags interfaceFlags =
networkInterface.flags();
if (interfaceFlags & QNetworkInterface::IsUp
&& interfaceFlags & QNetworkInterface::IsRunning
&& interfaceFlags & QNetworkInterface::CanBroadcast
&& !(interfaceFlags & QNetworkInterface::IsLoopBack))
{
qDebug() << "ConnectionManager::handleNetworkSessionOpened():"
<< "The connection is valid!";
if (mNetworkSession) {
mAccessPoint = mNetworkSession->configuration().name();
emit accessPointChanged(mAccessPoint);
}
mMyIp.setAddress(networkInterface.addressEntries().at(0).ip().toString());
emit myIpChanged(mMyIp.toString());
setState(Connected);
return;
}
}
}
示例7: QNetworkSession
bool Network::openConnection(QString &netInterface)
{
// Internet Access Point
QNetworkConfigurationManager manager;
const bool canStartIAP = (manager.capabilities()
& QNetworkConfigurationManager::CanStartAndStopInterfaces);
// If there is a default access point, use it
QNetworkConfiguration cfg = manager.defaultConfiguration();
if (!cfg.isValid() || !canStartIAP) {
return false;
}
// Open session
m_session = new QNetworkSession(cfg);
m_session->open();
// Waits for session to be open and continues after that
m_session->waitForOpened();
// Show interface name to the user
QNetworkInterface iff = m_session->interface();
netInterface = iff.humanReadableName();
return true;
}
示例8: hasInternetConnection
/*
* Checks if we have internet access!
*/
bool UnixCommand::hasInternetConnection()
{
QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
bool result = false;
for (int i = 0; i < ifaces.count(); i++){
QNetworkInterface iface = ifaces.at(i);
if ( iface.flags().testFlag(QNetworkInterface::IsUp)
&& !iface.flags().testFlag(QNetworkInterface::IsLoopBack) ){
for (int j=0; j<iface.addressEntries().count(); j++){
/*
We have an interface that is up, and has an ip address
therefore the link is present.
We will only enable this check on first positive,
all later results are incorrect
*/
if (result == false)
result = true;
}
}
}
//It seems to be alright, but let's make a ping to see the result
/*if (result == true)
{
result = UnixCommand::doInternetPingTest();
}*/
return result;
}
示例9: interfaceBoxIndexChanged
//someone selected another interface
void ApplicationWindow::interfaceBoxIndexChanged(int boxIndex)
{
//get the right index data
int interfaceIndex=interfaceBox->itemData(boxIndex).toInt();
QNetworkInterface tmpInterface = QNetworkInterface::interfaceFromIndex (interfaceIndex);
macLine->setText(tmpInterface.hardwareAddress());
// indexLine->setText(QString("%1").arg(interfaceIndex));
QList<QNetworkAddressEntry> interfaceAdresses;
interfaceAdresses=tmpInterface.addressEntries();
if (interfaceAdresses.size()>0)
{
ipLine->setText(interfaceAdresses[0].ip().toString());
maskLine->setText(interfaceAdresses[0].netmask().toString());
}
else
{
ipLine->setText("");
maskLine->setText("");
}
writeLog(createHeaderText());
}
示例10: isConnectedToNetwork2
bool isConnectedToNetwork2()
{
QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
bool result = false;
for (int i = 0; i < ifaces.count(); i++)
{
QNetworkInterface iface = ifaces.at(i);
if ( iface.flags().testFlag(QNetworkInterface::IsUp)
&& !iface.flags().testFlag(QNetworkInterface::IsLoopBack) )
{
// this loop is important
for (int j=0; j<iface.addressEntries().count(); j++)
{
// we have an interface that is up, and has an ip address
// therefore the link is present
// we will only enable this check on first positive,
// all later results are incorrect
if (result == false)
result = true;
}
}
}
return result;
}
示例11: getNetworkAddressEntry
/**
* @brief wavrNetwork::getNetworkAddressEntry
* @param pAddressEntry Gets the address entries from network interface and fills in this variable.
* @return Returns true opon success.
* Gets the address entries from network interface whether IPV4 or IPV6 and fills the data in pAddressEntry.
*/
bool wavrNetwork::getNetworkAddressEntry(QNetworkAddressEntry* pAddressEntry) {
// get the first active network interface
QNetworkInterface networkInterface;
if(getNetworkInterface(&networkInterface)) {
wavrTrace::write("Querying IP address from network interface...");
// get a list of all associated ip addresses of the interface
QList<QNetworkAddressEntry> addressEntries = networkInterface.addressEntries();
// return the first address which is an ipv4 address
for(int index = 0; index < addressEntries.count(); index++) {
if(addressEntries[index].ip().protocol() == QAbstractSocket::IPv4Protocol) {
*pAddressEntry = addressEntries[index];
this->networkInterface = networkInterface;
this->interfaceName = networkInterface.name();
wavrTrace::write("IPv4 address found for network interface.");
return true;
}
}
// if ipv4 address is not present, check for ipv6 address
for(int index = 0; index < addressEntries.count(); index++) {
if(addressEntries[index].ip().protocol() == QAbstractSocket::IPv6Protocol) {
*pAddressEntry = addressEntries[index];
this->networkInterface = networkInterface;
this->interfaceName = networkInterface.name();
wavrTrace::write("IPv6 address found for network interface.");
return true;
}
}
wavrTrace::write("Warning: No IP address found for network interface.");
}
return false;
}
示例12: foreach
//Source : http://stackoverflow.com/a/7616111
foreach(QNetworkInterface netInterface, QNetworkInterface::allInterfaces())
{
// Return only the first non-loopback MAC Address
if (!(netInterface.flags() & QNetworkInterface::IsLoopBack)){
macAddress = netInterface.hardwareAddress();
break;
}
}
示例13: gconf_value_changed
void SpeedWrapper::gconf_value_changed()
{
bool ok;
//check netspeed enabled
if(!m_gconf_enabled->value().isValid())
m_gconf_enabled->set(true);
//check netspeed interface
if(!m_gconf_speed_interface->value().isValid())
m_gconf_speed_interface->set("wlan0");
//check netspeed update time
if(!m_gconf_update_time->value().isValid())
m_gconf_update_time->set(1000);
//check when online
if(!m_gconf_when_online->value().isValid())
m_gconf_when_online->set(false);
m_is_enabled = m_gconf_enabled->value().toBool();
m_speed_interface = m_gconf_speed_interface->value().toString();
m_when_online = m_gconf_when_online->value().toBool();
if(m_speed_interface.isEmpty() || m_speed_interface.isNull())
m_speed_interface = "wlan0";
m_update_time = m_gconf_update_time->value().toInt(&ok);
if(!ok)
m_update_time = 1000;
QNetworkInterface interface = QNetworkInterface::interfaceFromName(m_speed_interface);
m_network_interface_valid = interface.isValid();
bool online = (interface.flags().testFlag(QNetworkInterface::IsUp) &&
interface.flags().testFlag(QNetworkInterface::IsRunning));
if(online != m_isOnline)
{
emit onlineChanged(online);
updateData();
}
m_isOnline = online;
if(m_is_enabled)
{
if(m_when_online)
{
if(m_isOnline)
{
m_timer->stop();
m_timer->start(m_update_time);
}else{
m_timer->stop();
}
}else{
m_timer->stop();
m_timer->start(m_update_time);
}
}else{
m_timer->stop();
}
}
示例14: getHostAddress
// ----------------------------------------------------------------------------
// getHostAddress (static)
// ----------------------------------------------------------------------------
QHostAddress NetworkTools::getHostAddress(const QNetworkInterface &in_interface)
{
if (in_interface.isValid() == false) return QHostAddress();
QList<QNetworkAddressEntry> addr_entry_list = in_interface.addressEntries();
if (addr_entry_list.isEmpty()) return QHostAddress();
return addr_entry_list.first().ip();
}
示例15: QObject
OrderSave::OrderSave(QObject *parent) :
QObject(parent)
{
QNetworkInterface *qni;
qni = new QNetworkInterface();
*qni = qni->interfaceFromName(QString("%1").arg("eth0"));
mac = qni->hardwareAddress();
//»ñÈ¡MACµØÖ·
}