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:
parent
d93a4158e0
commit
de7d2eb2ad
@ -45,13 +45,18 @@
|
||||
|
||||
//! [0]
|
||||
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]
|
||||
hostLabel = new QLabel(tr("&Server name:"));
|
||||
portLabel = new QLabel(tr("S&erver port:"));
|
||||
|
||||
hostCombo = new QComboBox;
|
||||
hostCombo->setEditable(true);
|
||||
// find out name of this machine
|
||||
QString name = QHostInfo::localHostName();
|
||||
@ -61,7 +66,7 @@ Client::Client(QWidget *parent)
|
||||
if (!domain.isEmpty())
|
||||
hostCombo->addItem(name + QChar('.') + domain);
|
||||
}
|
||||
if (name != QString("localhost"))
|
||||
if (name != QLatin1String("localhost"))
|
||||
hostCombo->addItem(QString("localhost"));
|
||||
// find out IP addresses of this machine
|
||||
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
|
||||
@ -76,54 +81,64 @@ Client::Client(QWidget *parent)
|
||||
hostCombo->addItem(ipAddressesList.at(i).toString());
|
||||
}
|
||||
|
||||
portLineEdit = new QLineEdit;
|
||||
portLineEdit->setValidator(new QIntValidator(1, 65535, this));
|
||||
|
||||
QLabel *hostLabel = new QLabel(tr("&Server name:"));
|
||||
hostLabel->setBuddy(hostCombo);
|
||||
QLabel *portLabel = new QLabel(tr("S&erver port:"));
|
||||
portLabel->setBuddy(portLineEdit);
|
||||
|
||||
statusLabel = new QLabel(tr("This examples requires that you run the "
|
||||
"Fortune Server example as well."));
|
||||
|
||||
getFortuneButton = new QPushButton(tr("Get Fortune"));
|
||||
getFortuneButton->setDefault(true);
|
||||
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(quitButton, QDialogButtonBox::RejectRole);
|
||||
|
||||
//! [1]
|
||||
tcpSocket = new QTcpSocket(this);
|
||||
//! [1]
|
||||
|
||||
connect(hostCombo, SIGNAL(editTextChanged(QString)),
|
||||
this, SLOT(enableGetFortuneButton()));
|
||||
connect(portLineEdit, SIGNAL(textChanged(QString)),
|
||||
this, SLOT(enableGetFortuneButton()));
|
||||
connect(getFortuneButton, SIGNAL(clicked()),
|
||||
this, SLOT(requestNewFortune()));
|
||||
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
|
||||
connect(hostCombo, &QComboBox::editTextChanged,
|
||||
this, &Client::enableGetFortuneButton);
|
||||
connect(portLineEdit, &QLineEdit::textChanged,
|
||||
this, &Client::enableGetFortuneButton);
|
||||
connect(getFortuneButton, &QAbstractButton::clicked,
|
||||
this, &Client::requestNewFortune);
|
||||
connect(quitButton, &QAbstractButton::clicked, this, &QWidget::close);
|
||||
//! [2] //! [3]
|
||||
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readFortune()));
|
||||
connect(tcpSocket, &QIODevice::readyRead, this, &Client::readFortune);
|
||||
//! [2] //! [4]
|
||||
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
|
||||
typedef void (QAbstractSocket::*QAbstractSocketErrorSignal)(QAbstractSocket::SocketError);
|
||||
connect(tcpSocket, static_cast<QAbstractSocketErrorSignal>(&QAbstractSocket::error),
|
||||
//! [3]
|
||||
this, SLOT(displayError(QAbstractSocket::SocketError)));
|
||||
this, &Client::displayError);
|
||||
//! [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(hostCombo, 0, 1);
|
||||
mainLayout->addWidget(portLabel, 1, 0);
|
||||
mainLayout->addWidget(portLineEdit, 1, 1);
|
||||
mainLayout->addWidget(statusLabel, 2, 0, 1, 2);
|
||||
mainLayout->addWidget(buttonBox, 3, 0, 1, 2);
|
||||
setLayout(mainLayout);
|
||||
|
||||
setWindowTitle(tr("Fortune Client"));
|
||||
setWindowTitle(QGuiApplication::applicationDisplayName());
|
||||
portLineEdit->setFocus();
|
||||
|
||||
QNetworkConfigurationManager manager;
|
||||
@ -142,7 +157,7 @@ Client::Client(QWidget *parent)
|
||||
}
|
||||
|
||||
networkSession = new QNetworkSession(config, this);
|
||||
connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened()));
|
||||
connect(networkSession, &QNetworkSession::opened, this, &Client::sessionOpened);
|
||||
|
||||
getFortuneButton->setEnabled(false);
|
||||
statusLabel->setText(tr("Opening network session."));
|
||||
@ -189,7 +204,7 @@ void Client::readFortune()
|
||||
in >> nextFortune;
|
||||
|
||||
if (nextFortune == currentFortune) {
|
||||
QTimer::singleShot(0, this, SLOT(requestNewFortune()));
|
||||
QTimer::singleShot(0, this, &Client::requestNewFortune);
|
||||
return;
|
||||
}
|
||||
//! [11]
|
||||
|
@ -46,7 +46,6 @@
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QComboBox;
|
||||
class QDialogButtonBox;
|
||||
class QLabel;
|
||||
class QLineEdit;
|
||||
class QPushButton;
|
||||
@ -60,7 +59,7 @@ class Client : public QDialog
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Client(QWidget *parent = 0);
|
||||
explicit Client(QWidget *parent = Q_NULLPTR);
|
||||
|
||||
private slots:
|
||||
void requestNewFortune();
|
||||
@ -70,14 +69,10 @@ private slots:
|
||||
void sessionOpened();
|
||||
|
||||
private:
|
||||
QLabel *hostLabel;
|
||||
QLabel *portLabel;
|
||||
QComboBox *hostCombo;
|
||||
QLineEdit *portLineEdit;
|
||||
QLabel *statusLabel;
|
||||
QPushButton *getFortuneButton;
|
||||
QPushButton *quitButton;
|
||||
QDialogButtonBox *buttonBox;
|
||||
|
||||
QTcpSocket *tcpSocket;
|
||||
QString currentFortune;
|
||||
|
@ -44,6 +44,7 @@
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
QGuiApplication::setApplicationDisplayName(Client::tr("Fortune Client"));
|
||||
Client client;
|
||||
client.show();
|
||||
return app.exec();
|
||||
|
@ -48,6 +48,7 @@
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
QGuiApplication::setApplicationDisplayName(Server::tr("Fortune Server"));
|
||||
Server server;
|
||||
server.show();
|
||||
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
|
||||
|
@ -46,11 +46,13 @@
|
||||
#include "server.h"
|
||||
|
||||
Server::Server(QWidget *parent)
|
||||
: QDialog(parent), tcpServer(0), networkSession(0)
|
||||
: QDialog(parent)
|
||||
, statusLabel(new QLabel)
|
||||
, tcpServer(Q_NULLPTR)
|
||||
, networkSession(0)
|
||||
{
|
||||
statusLabel = new QLabel;
|
||||
quitButton = new QPushButton(tr("Quit"));
|
||||
quitButton->setAutoDefault(false);
|
||||
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
||||
statusLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
|
||||
|
||||
QNetworkConfigurationManager manager;
|
||||
if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) {
|
||||
@ -68,7 +70,7 @@ Server::Server(QWidget *parent)
|
||||
}
|
||||
|
||||
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."));
|
||||
networkSession->open();
|
||||
@ -85,10 +87,11 @@ Server::Server(QWidget *parent)
|
||||
<< tr("You cannot kill time without injuring eternity.")
|
||||
<< tr("Computers are not intelligent. They only think they are.");
|
||||
//! [2]
|
||||
|
||||
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
|
||||
QPushButton *quitButton = new QPushButton(tr("Quit"));
|
||||
quitButton->setAutoDefault(false);
|
||||
connect(quitButton, &QAbstractButton::clicked, this, &QWidget::close);
|
||||
//! [3]
|
||||
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune()));
|
||||
connect(tcpServer, &QTcpServer::newConnection, this, &Server::sendFortune);
|
||||
//! [3]
|
||||
|
||||
QHBoxLayout *buttonLayout = new QHBoxLayout;
|
||||
@ -96,12 +99,26 @@ Server::Server(QWidget *parent)
|
||||
buttonLayout->addWidget(quitButton);
|
||||
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->addLayout(buttonLayout);
|
||||
setLayout(mainLayout);
|
||||
|
||||
setWindowTitle(tr("Fortune Server"));
|
||||
setWindowTitle(QGuiApplication::applicationDisplayName());
|
||||
}
|
||||
|
||||
void Server::sessionOpened()
|
||||
@ -165,8 +182,8 @@ void Server::sendFortune()
|
||||
//! [6] //! [7]
|
||||
|
||||
QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
|
||||
connect(clientConnection, SIGNAL(disconnected()),
|
||||
clientConnection, SLOT(deleteLater()));
|
||||
connect(clientConnection, &QAbstractSocket::disconnected,
|
||||
clientConnection, &QObject::deleteLater);
|
||||
//! [7] //! [8]
|
||||
|
||||
clientConnection->write(block);
|
||||
|
@ -56,7 +56,7 @@ class Server : public QDialog
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Server(QWidget *parent = 0);
|
||||
explicit Server(QWidget *parent = Q_NULLPTR);
|
||||
|
||||
private slots:
|
||||
void sessionOpened();
|
||||
@ -64,7 +64,6 @@ private slots:
|
||||
|
||||
private:
|
||||
QLabel *statusLabel;
|
||||
QPushButton *quitButton;
|
||||
QTcpServer *tcpServer;
|
||||
QStringList fortunes;
|
||||
QNetworkSession *networkSession;
|
||||
|
Loading…
Reference in New Issue
Block a user