Polish the fortune server/client examples.

- Remove Qt::WindowContextHelpButtonHint.
- Make the server label interactive (enable copy).
- Introduce new connection syntax.
- Remove unneeded member variables.
- Use constructor initialization where appropriate.
- Adapt the layout to fullscreen platforms by wrapping it
  into a QGroupBox.

Change-Id: I6e397ad082f22ba1e99fc5a17440b2be1f9584f6
Reviewed-by: Topi Reiniö <topi.reinio@theqtcompany.com>
This commit is contained in:
Friedemann Kleint 2015-11-26 15:04:33 +01:00
parent d93a4158e0
commit de7d2eb2ad
6 changed files with 78 additions and 50 deletions

View File

@ -45,13 +45,18 @@
//! [0] //! [0]
Client::Client(QWidget *parent) Client::Client(QWidget *parent)
: QDialog(parent), networkSession(0) : QDialog(parent)
, hostCombo(new QComboBox)
, portLineEdit(new QLineEdit)
, getFortuneButton(new QPushButton(tr("Get Fortune")))
//! [1]
, tcpSocket(new QTcpSocket(this))
//! [1]
, blockSize(0)
, networkSession(Q_NULLPTR)
{ {
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
//! [0] //! [0]
hostLabel = new QLabel(tr("&Server name:"));
portLabel = new QLabel(tr("S&erver port:"));
hostCombo = new QComboBox;
hostCombo->setEditable(true); hostCombo->setEditable(true);
// find out name of this machine // find out name of this machine
QString name = QHostInfo::localHostName(); QString name = QHostInfo::localHostName();
@ -61,7 +66,7 @@ Client::Client(QWidget *parent)
if (!domain.isEmpty()) if (!domain.isEmpty())
hostCombo->addItem(name + QChar('.') + domain); hostCombo->addItem(name + QChar('.') + domain);
} }
if (name != QString("localhost")) if (name != QLatin1String("localhost"))
hostCombo->addItem(QString("localhost")); hostCombo->addItem(QString("localhost"));
// find out IP addresses of this machine // find out IP addresses of this machine
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses(); QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
@ -76,54 +81,64 @@ Client::Client(QWidget *parent)
hostCombo->addItem(ipAddressesList.at(i).toString()); hostCombo->addItem(ipAddressesList.at(i).toString());
} }
portLineEdit = new QLineEdit;
portLineEdit->setValidator(new QIntValidator(1, 65535, this)); portLineEdit->setValidator(new QIntValidator(1, 65535, this));
QLabel *hostLabel = new QLabel(tr("&Server name:"));
hostLabel->setBuddy(hostCombo); hostLabel->setBuddy(hostCombo);
QLabel *portLabel = new QLabel(tr("S&erver port:"));
portLabel->setBuddy(portLineEdit); portLabel->setBuddy(portLineEdit);
statusLabel = new QLabel(tr("This examples requires that you run the " statusLabel = new QLabel(tr("This examples requires that you run the "
"Fortune Server example as well.")); "Fortune Server example as well."));
getFortuneButton = new QPushButton(tr("Get Fortune"));
getFortuneButton->setDefault(true); getFortuneButton->setDefault(true);
getFortuneButton->setEnabled(false); getFortuneButton->setEnabled(false);
quitButton = new QPushButton(tr("Quit")); QPushButton *quitButton = new QPushButton(tr("Quit"));
buttonBox = new QDialogButtonBox; QDialogButtonBox *buttonBox = new QDialogButtonBox;
buttonBox->addButton(getFortuneButton, QDialogButtonBox::ActionRole); buttonBox->addButton(getFortuneButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
//! [1] connect(hostCombo, &QComboBox::editTextChanged,
tcpSocket = new QTcpSocket(this); this, &Client::enableGetFortuneButton);
//! [1] connect(portLineEdit, &QLineEdit::textChanged,
this, &Client::enableGetFortuneButton);
connect(hostCombo, SIGNAL(editTextChanged(QString)), connect(getFortuneButton, &QAbstractButton::clicked,
this, SLOT(enableGetFortuneButton())); this, &Client::requestNewFortune);
connect(portLineEdit, SIGNAL(textChanged(QString)), connect(quitButton, &QAbstractButton::clicked, this, &QWidget::close);
this, SLOT(enableGetFortuneButton()));
connect(getFortuneButton, SIGNAL(clicked()),
this, SLOT(requestNewFortune()));
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
//! [2] //! [3] //! [2] //! [3]
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readFortune())); connect(tcpSocket, &QIODevice::readyRead, this, &Client::readFortune);
//! [2] //! [4] //! [2] //! [4]
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), typedef void (QAbstractSocket::*QAbstractSocketErrorSignal)(QAbstractSocket::SocketError);
connect(tcpSocket, static_cast<QAbstractSocketErrorSignal>(&QAbstractSocket::error),
//! [3] //! [3]
this, SLOT(displayError(QAbstractSocket::SocketError))); this, &Client::displayError);
//! [4] //! [4]
QGridLayout *mainLayout = new QGridLayout; QGridLayout *mainLayout = Q_NULLPTR;
if (QGuiApplication::styleHints()->showIsFullScreen()) {
QVBoxLayout *outerVerticalLayout = new QVBoxLayout(this);
outerVerticalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding));
QHBoxLayout *outerHorizontalLayout = new QHBoxLayout;
outerHorizontalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::Ignored));
QGroupBox *groupBox = new QGroupBox(QGuiApplication::applicationDisplayName());
mainLayout = new QGridLayout(groupBox);
outerHorizontalLayout->addWidget(groupBox);
outerHorizontalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::Ignored));
outerVerticalLayout->addLayout(outerHorizontalLayout);
outerVerticalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding));
} else {
mainLayout = new QGridLayout(this);
}
mainLayout->addWidget(hostLabel, 0, 0); mainLayout->addWidget(hostLabel, 0, 0);
mainLayout->addWidget(hostCombo, 0, 1); mainLayout->addWidget(hostCombo, 0, 1);
mainLayout->addWidget(portLabel, 1, 0); mainLayout->addWidget(portLabel, 1, 0);
mainLayout->addWidget(portLineEdit, 1, 1); mainLayout->addWidget(portLineEdit, 1, 1);
mainLayout->addWidget(statusLabel, 2, 0, 1, 2); mainLayout->addWidget(statusLabel, 2, 0, 1, 2);
mainLayout->addWidget(buttonBox, 3, 0, 1, 2); mainLayout->addWidget(buttonBox, 3, 0, 1, 2);
setLayout(mainLayout);
setWindowTitle(tr("Fortune Client")); setWindowTitle(QGuiApplication::applicationDisplayName());
portLineEdit->setFocus(); portLineEdit->setFocus();
QNetworkConfigurationManager manager; QNetworkConfigurationManager manager;
@ -142,7 +157,7 @@ Client::Client(QWidget *parent)
} }
networkSession = new QNetworkSession(config, this); networkSession = new QNetworkSession(config, this);
connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened())); connect(networkSession, &QNetworkSession::opened, this, &Client::sessionOpened);
getFortuneButton->setEnabled(false); getFortuneButton->setEnabled(false);
statusLabel->setText(tr("Opening network session.")); statusLabel->setText(tr("Opening network session."));
@ -189,7 +204,7 @@ void Client::readFortune()
in >> nextFortune; in >> nextFortune;
if (nextFortune == currentFortune) { if (nextFortune == currentFortune) {
QTimer::singleShot(0, this, SLOT(requestNewFortune())); QTimer::singleShot(0, this, &Client::requestNewFortune);
return; return;
} }
//! [11] //! [11]

View File

@ -46,7 +46,6 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QComboBox; class QComboBox;
class QDialogButtonBox;
class QLabel; class QLabel;
class QLineEdit; class QLineEdit;
class QPushButton; class QPushButton;
@ -60,7 +59,7 @@ class Client : public QDialog
Q_OBJECT Q_OBJECT
public: public:
Client(QWidget *parent = 0); explicit Client(QWidget *parent = Q_NULLPTR);
private slots: private slots:
void requestNewFortune(); void requestNewFortune();
@ -70,14 +69,10 @@ private slots:
void sessionOpened(); void sessionOpened();
private: private:
QLabel *hostLabel;
QLabel *portLabel;
QComboBox *hostCombo; QComboBox *hostCombo;
QLineEdit *portLineEdit; QLineEdit *portLineEdit;
QLabel *statusLabel; QLabel *statusLabel;
QPushButton *getFortuneButton; QPushButton *getFortuneButton;
QPushButton *quitButton;
QDialogButtonBox *buttonBox;
QTcpSocket *tcpSocket; QTcpSocket *tcpSocket;
QString currentFortune; QString currentFortune;

View File

@ -44,6 +44,7 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication app(argc, argv); QApplication app(argc, argv);
QGuiApplication::setApplicationDisplayName(Client::tr("Fortune Client"));
Client client; Client client;
client.show(); client.show();
return app.exec(); return app.exec();

View File

@ -48,6 +48,7 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication app(argc, argv); QApplication app(argc, argv);
QGuiApplication::setApplicationDisplayName(Server::tr("Fortune Server"));
Server server; Server server;
server.show(); server.show();
qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));

View File

@ -46,11 +46,13 @@
#include "server.h" #include "server.h"
Server::Server(QWidget *parent) Server::Server(QWidget *parent)
: QDialog(parent), tcpServer(0), networkSession(0) : QDialog(parent)
, statusLabel(new QLabel)
, tcpServer(Q_NULLPTR)
, networkSession(0)
{ {
statusLabel = new QLabel; setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
quitButton = new QPushButton(tr("Quit")); statusLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
quitButton->setAutoDefault(false);
QNetworkConfigurationManager manager; QNetworkConfigurationManager manager;
if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) {
@ -68,7 +70,7 @@ Server::Server(QWidget *parent)
} }
networkSession = new QNetworkSession(config, this); networkSession = new QNetworkSession(config, this);
connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened())); connect(networkSession, &QNetworkSession::opened, this, &Server::sessionOpened);
statusLabel->setText(tr("Opening network session.")); statusLabel->setText(tr("Opening network session."));
networkSession->open(); networkSession->open();
@ -85,10 +87,11 @@ Server::Server(QWidget *parent)
<< tr("You cannot kill time without injuring eternity.") << tr("You cannot kill time without injuring eternity.")
<< tr("Computers are not intelligent. They only think they are."); << tr("Computers are not intelligent. They only think they are.");
//! [2] //! [2]
QPushButton *quitButton = new QPushButton(tr("Quit"));
connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); quitButton->setAutoDefault(false);
connect(quitButton, &QAbstractButton::clicked, this, &QWidget::close);
//! [3] //! [3]
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune())); connect(tcpServer, &QTcpServer::newConnection, this, &Server::sendFortune);
//! [3] //! [3]
QHBoxLayout *buttonLayout = new QHBoxLayout; QHBoxLayout *buttonLayout = new QHBoxLayout;
@ -96,12 +99,26 @@ Server::Server(QWidget *parent)
buttonLayout->addWidget(quitButton); buttonLayout->addWidget(quitButton);
buttonLayout->addStretch(1); buttonLayout->addStretch(1);
QVBoxLayout *mainLayout = new QVBoxLayout; QVBoxLayout *mainLayout = Q_NULLPTR;
if (QGuiApplication::styleHints()->showIsFullScreen()) {
QVBoxLayout *outerVerticalLayout = new QVBoxLayout(this);
outerVerticalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding));
QHBoxLayout *outerHorizontalLayout = new QHBoxLayout;
outerHorizontalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::Ignored));
QGroupBox *groupBox = new QGroupBox(QGuiApplication::applicationDisplayName());
mainLayout = new QVBoxLayout(groupBox);
outerHorizontalLayout->addWidget(groupBox);
outerHorizontalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::Ignored));
outerVerticalLayout->addLayout(outerHorizontalLayout);
outerVerticalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding));
} else {
mainLayout = new QVBoxLayout(this);
}
mainLayout->addWidget(statusLabel); mainLayout->addWidget(statusLabel);
mainLayout->addLayout(buttonLayout); mainLayout->addLayout(buttonLayout);
setLayout(mainLayout);
setWindowTitle(tr("Fortune Server")); setWindowTitle(QGuiApplication::applicationDisplayName());
} }
void Server::sessionOpened() void Server::sessionOpened()
@ -165,8 +182,8 @@ void Server::sendFortune()
//! [6] //! [7] //! [6] //! [7]
QTcpSocket *clientConnection = tcpServer->nextPendingConnection(); QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
connect(clientConnection, SIGNAL(disconnected()), connect(clientConnection, &QAbstractSocket::disconnected,
clientConnection, SLOT(deleteLater())); clientConnection, &QObject::deleteLater);
//! [7] //! [8] //! [7] //! [8]
clientConnection->write(block); clientConnection->write(block);

View File

@ -56,7 +56,7 @@ class Server : public QDialog
Q_OBJECT Q_OBJECT
public: public:
Server(QWidget *parent = 0); explicit Server(QWidget *parent = Q_NULLPTR);
private slots: private slots:
void sessionOpened(); void sessionOpened();
@ -64,7 +64,6 @@ private slots:
private: private:
QLabel *statusLabel; QLabel *statusLabel;
QPushButton *quitButton;
QTcpServer *tcpServer; QTcpServer *tcpServer;
QStringList fortunes; QStringList fortunes;
QNetworkSession *networkSession; QNetworkSession *networkSession;