diff options
author | Mårten Nordheim <marten.nordheim@qt.io> | 2017-09-04 10:33:26 +0200 |
---|---|---|
committer | Mårten Nordheim <marten.nordheim@qt.io> | 2017-09-22 14:51:48 +0000 |
commit | 7ef515b5c0b212d865168b64519902b35c0215fd (patch) | |
tree | 8384aa7c729ab73f89435502fd260f8b87c584f8 /examples/corelib | |
parent | ce019efb5cfcc0bce516a761be4295c568994a31 (diff) |
Modernize the Local Fortune Client and Server examples
Task-number: QTBUG-60625
Change-Id: I8ae77b782b580baf84cd5f353f8d584f674fb014
Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@qt.io>
Diffstat (limited to 'examples/corelib')
-rw-r--r-- | examples/corelib/ipc/localfortuneclient/client.cpp | 67 | ||||
-rw-r--r-- | examples/corelib/ipc/localfortuneclient/client.h | 13 | ||||
-rw-r--r-- | examples/corelib/ipc/localfortuneclient/main.cpp | 1 | ||||
-rw-r--r-- | examples/corelib/ipc/localfortuneserver/main.cpp | 2 | ||||
-rw-r--r-- | examples/corelib/ipc/localfortuneserver/server.cpp | 36 | ||||
-rw-r--r-- | examples/corelib/ipc/localfortuneserver/server.h | 4 |
6 files changed, 60 insertions, 63 deletions
diff --git a/examples/corelib/ipc/localfortuneclient/client.cpp b/examples/corelib/ipc/localfortuneclient/client.cpp index d5a1525769..8d415f73aa 100644 --- a/examples/corelib/ipc/localfortuneclient/client.cpp +++ b/examples/corelib/ipc/localfortuneclient/client.cpp @@ -54,45 +54,45 @@ #include "client.h" Client::Client(QWidget *parent) - : QDialog(parent) + : QDialog(parent), + hostLineEdit(new QLineEdit("fortune")), + getFortuneButton(new QPushButton(tr("Get Fortune"))), + statusLabel(new QLabel(tr("This examples requires that you run the " + "Local Fortune Server example as well."))), + socket(new QLocalSocket(this)) { - hostLabel = new QLabel(tr("&Server name:")); - hostLineEdit = new QLineEdit("fortune"); - + setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); + QLabel *hostLabel = new QLabel(tr("&Server name:")); hostLabel->setBuddy(hostLineEdit); - statusLabel = new QLabel(tr("This examples requires that you run the " - "Fortune Server example as well.")); statusLabel->setWordWrap(true); - getFortuneButton = new QPushButton(tr("Get Fortune")); getFortuneButton->setDefault(true); + QPushButton *quitButton = new QPushButton(tr("Quit")); - quitButton = new QPushButton(tr("Quit")); - - buttonBox = new QDialogButtonBox; + QDialogButtonBox *buttonBox = new QDialogButtonBox; buttonBox->addButton(getFortuneButton, QDialogButtonBox::ActionRole); buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); - socket = new QLocalSocket(this); + in.setDevice(socket); + in.setVersion(QDataStream::Qt_5_10); - connect(hostLineEdit, SIGNAL(textChanged(QString)), - this, SLOT(enableGetFortuneButton())); - connect(getFortuneButton, SIGNAL(clicked()), - this, SLOT(requestNewFortune())); - connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); - connect(socket, SIGNAL(readyRead()), this, SLOT(readFortune())); - connect(socket, SIGNAL(error(QLocalSocket::LocalSocketError)), - this, SLOT(displayError(QLocalSocket::LocalSocketError))); + connect(hostLineEdit, &QLineEdit::textChanged, + this, &Client::enableGetFortuneButton); + connect(getFortuneButton, &QPushButton::clicked, + this, &Client::requestNewFortune); + connect(quitButton, &QPushButton::clicked, this, &Client::close); + connect(socket, &QLocalSocket::readyRead, this, &Client::readFortune); + connect(socket, QOverload<QLocalSocket::LocalSocketError>::of(&QLocalSocket::error), + this, &Client::displayError); - QGridLayout *mainLayout = new QGridLayout; + QGridLayout *mainLayout = new QGridLayout(this); mainLayout->addWidget(hostLabel, 0, 0); mainLayout->addWidget(hostLineEdit, 0, 1); mainLayout->addWidget(statusLabel, 2, 0, 1, 2); mainLayout->addWidget(buttonBox, 3, 0, 1, 2); - setLayout(mainLayout); - setWindowTitle(tr("Fortune Client")); + setWindowTitle(QGuiApplication::applicationDisplayName()); hostLineEdit->setFocus(); } @@ -106,11 +106,9 @@ void Client::requestNewFortune() void Client::readFortune() { - QDataStream in(socket); - in.setVersion(QDataStream::Qt_4_0); - if (blockSize == 0) { - // Relies on the fact that QDataStream format streams a quint32 into sizeof(quint32) bytes + // Relies on the fact that QDataStream serializes a quint32 into + // sizeof(quint32) bytes if (socket->bytesAvailable() < (int)sizeof(quint32)) return; in >> blockSize; @@ -123,7 +121,7 @@ void Client::readFortune() in >> nextFortune; if (nextFortune == currentFortune) { - QTimer::singleShot(0, this, SLOT(requestNewFortune())); + QTimer::singleShot(0, this, &Client::requestNewFortune); return; } @@ -136,21 +134,22 @@ void Client::displayError(QLocalSocket::LocalSocketError socketError) { switch (socketError) { case QLocalSocket::ServerNotFoundError: - QMessageBox::information(this, tr("Fortune Client"), - tr("The host was not found. Please check the " - "host name and port settings.")); + QMessageBox::information(this, tr("Local Fortune Client"), + tr("The host was not found. Please make sure " + "that the server is running and that the " + "server name is correct.")); break; case QLocalSocket::ConnectionRefusedError: - QMessageBox::information(this, tr("Fortune Client"), + QMessageBox::information(this, tr("Local Fortune Client"), tr("The connection was refused by the peer. " "Make sure the fortune server is running, " - "and check that the host name and port " - "settings are correct.")); + "and check that the server name " + "is correct.")); break; case QLocalSocket::PeerClosedError: break; default: - QMessageBox::information(this, tr("Fortune Client"), + QMessageBox::information(this, tr("Local Fortune Client"), tr("The following error occurred: %1.") .arg(socket->errorString())); } diff --git a/examples/corelib/ipc/localfortuneclient/client.h b/examples/corelib/ipc/localfortuneclient/client.h index 8e628efcee..0c1ede94c9 100644 --- a/examples/corelib/ipc/localfortuneclient/client.h +++ b/examples/corelib/ipc/localfortuneclient/client.h @@ -52,11 +52,11 @@ #define CLIENT_H #include <QDialog> +#include <QDataStream> #include <qlocalsocket.h> QT_BEGIN_NAMESPACE -class QDialogButtonBox; class QLabel; class QLineEdit; class QPushButton; @@ -68,7 +68,7 @@ class Client : public QDialog Q_OBJECT public: - Client(QWidget *parent = 0); + explicit Client(QWidget *parent = nullptr); private slots: void requestNewFortune(); @@ -77,16 +77,15 @@ private slots: void enableGetFortuneButton(); private: - QLabel *hostLabel; QLineEdit *hostLineEdit; - QLabel *statusLabel; QPushButton *getFortuneButton; - QPushButton *quitButton; - QDialogButtonBox *buttonBox; + QLabel *statusLabel; QLocalSocket *socket; - QString currentFortune; + QDataStream in; quint32 blockSize; + + QString currentFortune; }; #endif diff --git a/examples/corelib/ipc/localfortuneclient/main.cpp b/examples/corelib/ipc/localfortuneclient/main.cpp index dd13a05be9..ed5cf4c569 100644 --- a/examples/corelib/ipc/localfortuneclient/main.cpp +++ b/examples/corelib/ipc/localfortuneclient/main.cpp @@ -55,6 +55,7 @@ int main(int argc, char *argv[]) { QApplication app(argc, argv); + QGuiApplication::setApplicationDisplayName(Client::tr("Local Fortune Client")); Client client; client.show(); return app.exec(); diff --git a/examples/corelib/ipc/localfortuneserver/main.cpp b/examples/corelib/ipc/localfortuneserver/main.cpp index cd066a0acd..6f8ec539fe 100644 --- a/examples/corelib/ipc/localfortuneserver/main.cpp +++ b/examples/corelib/ipc/localfortuneserver/main.cpp @@ -58,8 +58,8 @@ int main(int argc, char *argv[]) { QApplication app(argc, argv); + QGuiApplication::setApplicationDisplayName(Server::tr("Local Fortune Server")); Server server; server.show(); - qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); return app.exec(); } diff --git a/examples/corelib/ipc/localfortuneserver/server.cpp b/examples/corelib/ipc/localfortuneserver/server.cpp index 2eee4760be..9b8b08bd17 100644 --- a/examples/corelib/ipc/localfortuneserver/server.cpp +++ b/examples/corelib/ipc/localfortuneserver/server.cpp @@ -60,22 +60,21 @@ Server::Server(QWidget *parent) : QDialog(parent) { - statusLabel = new QLabel; - statusLabel->setWordWrap(true); - quitButton = new QPushButton(tr("Quit")); - quitButton->setAutoDefault(false); + setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); server = new QLocalServer(this); if (!server->listen("fortune")) { - QMessageBox::critical(this, tr("Fortune Server"), + QMessageBox::critical(this, tr("Local Fortune Server"), tr("Unable to start the server: %1.") .arg(server->errorString())); close(); return; } + QLabel *statusLabel = new QLabel; + statusLabel->setWordWrap(true); statusLabel->setText(tr("The server is running.\n" - "Run the Fortune Client example now.")); + "Run the Local Fortune Client example now.")); fortunes << tr("You've been leading a dog's life. Stay off the furniture.") << tr("You've got to think about tomorrow.") @@ -85,35 +84,36 @@ Server::Server(QWidget *parent) << tr("You cannot kill time without injuring eternity.") << tr("Computers are not intelligent. They only think they are."); - connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); - connect(server, SIGNAL(newConnection()), this, SLOT(sendFortune())); + QPushButton *quitButton = new QPushButton(tr("Quit")); + quitButton->setAutoDefault(false); + connect(quitButton, &QPushButton::clicked, this, &Server::close); + connect(server, &QLocalServer::newConnection, this, &Server::sendFortune); QHBoxLayout *buttonLayout = new QHBoxLayout; buttonLayout->addStretch(1); buttonLayout->addWidget(quitButton); buttonLayout->addStretch(1); - QVBoxLayout *mainLayout = new QVBoxLayout; + QVBoxLayout *mainLayout = new QVBoxLayout(this); mainLayout->addWidget(statusLabel); mainLayout->addLayout(buttonLayout); - setLayout(mainLayout); - setWindowTitle(tr("Fortune Server")); + setWindowTitle(QGuiApplication::applicationDisplayName()); } void Server::sendFortune() { QByteArray block; QDataStream out(&block, QIODevice::WriteOnly); - out.setVersion(QDataStream::Qt_4_0); - out << (quint32)0; - out << fortunes.at(qrand() % fortunes.size()); - out.device()->seek(0); - out << (quint32)(block.size() - sizeof(quint32)); + out.setVersion(QDataStream::Qt_5_10); + const int fortuneIndex = QRandomGenerator::bounded(0, fortunes.size()); + const QString &message = fortunes.at(fortuneIndex); + out << quint32(message.size()); + out << message; QLocalSocket *clientConnection = server->nextPendingConnection(); - connect(clientConnection, SIGNAL(disconnected()), - clientConnection, SLOT(deleteLater())); + connect(clientConnection, &QLocalSocket::disconnected, + clientConnection, &QLocalSocket::deleteLater); clientConnection->write(block); clientConnection->flush(); diff --git a/examples/corelib/ipc/localfortuneserver/server.h b/examples/corelib/ipc/localfortuneserver/server.h index c77b4e8127..6b90ba5932 100644 --- a/examples/corelib/ipc/localfortuneserver/server.h +++ b/examples/corelib/ipc/localfortuneserver/server.h @@ -64,14 +64,12 @@ class Server : public QDialog Q_OBJECT public: - Server(QWidget *parent = 0); + explicit Server(QWidget *parent = nullptr); private slots: void sendFortune(); private: - QLabel *statusLabel; - QPushButton *quitButton; QLocalServer *server; QStringList fortunes; }; |