PDA

View Full Version : Explaination of code required



Arpitgarg
8th March 2011, 23:57
Hey ,I am a student learning QT using C/C++.I started doing network examples few days back and wasn't able to understand the following snippet from an example.
Pls help me understand it.




QNetworkConfigurationManager manager;
if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequir ed) {
// Get saved network configuration
QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
settings.beginGroup(QLatin1String("QtNetwork"));
const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString();
settings.endGroup();

// If the saved network configuration is not currently discovered use the system default
QNetworkConfiguration config = manager.configurationFromIdentifier(id);
if ((config.state() & QNetworkConfiguration::Discovered) !=
QNetworkConfiguration::Discovered) {
config = manager.defaultConfiguration();
}



thanks
Arpit

Lykurg
9th March 2011, 05:46
What exactly do you don't understand?

Arpitgarg
9th March 2011, 06:25
Settings settings(QSettings::UserScope, QLatin1String("Trolltech"));
settings.beginGroup(QLatin1String("QtNetwork"));
const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString();

I wasn't able to understand how is this part working that is how it is getting the configrations, and especially Qlatin1string(" ") . Well this snippet is from Qt example Fortune server/client. Can i run this prg on diffrent machines i.e one with server another with client.

Lykurg
9th March 2011, 06:52
Ok, see Client::sessionOpened(). There the connection is stored, when it was successful. So in your code QSettings is only used to verify if there was a already working session and if so, this should be used. Therefore the normal QSettings syntax is used. There is a good example in the detailed description of QSettings on how to store and get settings back. Also on where to store these settings (the parameters of the c-tor).
As for the QLatin1String. It is just a small wrapper to avoid using the heavy QString. The purpose is to keep the compiler silent if you use QT_NO_CAST_FROM_ASCII. It may be important if you use translations within your application.

Arpitgarg
9th March 2011, 08:45
Thanks , pls correct me if I am wrong

So, QLatin1String(" Trolltech ") means it is using Trolltech object to access trolltech server setting & store it.

If yes then how do i access a computer on network of which only IP address is known to me.

Lykurg
9th March 2011, 10:17
No, QSettings only uses a local file to store a once made connection (The one after you have pressed the get fortune button). And that has nothing to do with Trolltech. It is just a name to identify the settings object (file). See QCoreApplication::setOrganizationName().


how do i access a computer on network of which only IP address is known to me.
Fill in the informations at server name and server port.