examples/network/blockingfortuneclient/blockingclient.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
    48     : QDialog(parent)
    48     : QDialog(parent)
    49 {
    49 {
    50     hostLabel = new QLabel(tr("&Server name:"));
    50     hostLabel = new QLabel(tr("&Server name:"));
    51     portLabel = new QLabel(tr("S&erver port:"));
    51     portLabel = new QLabel(tr("S&erver port:"));
    52 
    52 
    53     hostLineEdit = new QLineEdit("Localhost");
    53     // find out which IP to connect to
       
    54     QString ipAddress;
       
    55     QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
       
    56     // use the first non-localhost IPv4 address
       
    57     for (int i = 0; i < ipAddressesList.size(); ++i) {
       
    58         if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
       
    59             ipAddressesList.at(i).toIPv4Address()) {
       
    60             ipAddress = ipAddressesList.at(i).toString();
       
    61             break;
       
    62         }
       
    63     }
       
    64     // if we did not find one, use IPv4 localhost
       
    65     if (ipAddress.isEmpty())
       
    66         ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
       
    67 
       
    68     hostLineEdit = new QLineEdit(ipAddress);
    54     portLineEdit = new QLineEdit;
    69     portLineEdit = new QLineEdit;
    55     portLineEdit->setValidator(new QIntValidator(1, 65535, this));
    70     portLineEdit->setValidator(new QIntValidator(1, 65535, this));
    56 
    71 
    57     hostLabel->setBuddy(hostLineEdit);
    72     hostLabel->setBuddy(hostLineEdit);
    58     portLabel->setBuddy(portLineEdit);
    73     portLabel->setBuddy(portLineEdit);
    68 
    83 
    69     buttonBox = new QDialogButtonBox;
    84     buttonBox = new QDialogButtonBox;
    70     buttonBox->addButton(getFortuneButton, QDialogButtonBox::ActionRole);
    85     buttonBox->addButton(getFortuneButton, QDialogButtonBox::ActionRole);
    71     buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
    86     buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
    72 
    87 
    73     connect(hostLineEdit, SIGNAL(textChanged(const QString &)),
    88     connect(hostLineEdit, SIGNAL(textChanged(QString)),
    74             this, SLOT(enableGetFortuneButton()));
    89             this, SLOT(enableGetFortuneButton()));
    75     connect(portLineEdit, SIGNAL(textChanged(const QString &)),
    90     connect(portLineEdit, SIGNAL(textChanged(QString)),
    76             this, SLOT(enableGetFortuneButton()));
    91             this, SLOT(enableGetFortuneButton()));
    77     connect(getFortuneButton, SIGNAL(clicked()),
    92     connect(getFortuneButton, SIGNAL(clicked()),
    78             this, SLOT(requestNewFortune()));
    93             this, SLOT(requestNewFortune()));
    79     connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
    94     connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
    80 //! [0]
    95 //! [0]
    81     connect(&thread, SIGNAL(newFortune(const QString &)),
    96     connect(&thread, SIGNAL(newFortune(QString)),
    82             this, SLOT(showFortune(const QString &)));
    97             this, SLOT(showFortune(QString)));
    83 //! [0] //! [1]
    98 //! [0] //! [1]
    84     connect(&thread, SIGNAL(error(int, const QString &)),
    99     connect(&thread, SIGNAL(error(int,QString)),
    85             this, SLOT(displayError(int, const QString &)));
   100             this, SLOT(displayError(int,QString)));
    86 //! [1]
   101 //! [1]
    87 
   102 
    88     QGridLayout *mainLayout = new QGridLayout;
   103     QGridLayout *mainLayout = new QGridLayout;
    89     mainLayout->addWidget(hostLabel, 0, 0);
   104     mainLayout->addWidget(hostLabel, 0, 0);
    90     mainLayout->addWidget(hostLineEdit, 0, 1);
   105     mainLayout->addWidget(hostLineEdit, 0, 1);