Modernize the Local Fortune Client and Server examples
Task-number: QTBUG-60625 Change-Id: I8ae77b782b580baf84cd5f353f8d584f674fb014 Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@qt.io>
This commit is contained in:
parent
ce019efb5c
commit
7ef515b5c0
@ -54,45 +54,45 @@
|
|||||||
#include "client.h"
|
#include "client.h"
|
||||||
|
|
||||||
Client::Client(QWidget *parent)
|
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:"));
|
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
||||||
hostLineEdit = new QLineEdit("fortune");
|
QLabel *hostLabel = new QLabel(tr("&Server name:"));
|
||||||
|
|
||||||
hostLabel->setBuddy(hostLineEdit);
|
hostLabel->setBuddy(hostLineEdit);
|
||||||
|
|
||||||
statusLabel = new QLabel(tr("This examples requires that you run the "
|
|
||||||
"Fortune Server example as well."));
|
|
||||||
statusLabel->setWordWrap(true);
|
statusLabel->setWordWrap(true);
|
||||||
|
|
||||||
getFortuneButton = new QPushButton(tr("Get Fortune"));
|
|
||||||
getFortuneButton->setDefault(true);
|
getFortuneButton->setDefault(true);
|
||||||
|
QPushButton *quitButton = new QPushButton(tr("Quit"));
|
||||||
|
|
||||||
quitButton = new QPushButton(tr("Quit"));
|
QDialogButtonBox *buttonBox = new QDialogButtonBox;
|
||||||
|
|
||||||
buttonBox = new QDialogButtonBox;
|
|
||||||
buttonBox->addButton(getFortuneButton, QDialogButtonBox::ActionRole);
|
buttonBox->addButton(getFortuneButton, QDialogButtonBox::ActionRole);
|
||||||
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
|
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
|
||||||
|
|
||||||
socket = new QLocalSocket(this);
|
in.setDevice(socket);
|
||||||
|
in.setVersion(QDataStream::Qt_5_10);
|
||||||
|
|
||||||
connect(hostLineEdit, SIGNAL(textChanged(QString)),
|
connect(hostLineEdit, &QLineEdit::textChanged,
|
||||||
this, SLOT(enableGetFortuneButton()));
|
this, &Client::enableGetFortuneButton);
|
||||||
connect(getFortuneButton, SIGNAL(clicked()),
|
connect(getFortuneButton, &QPushButton::clicked,
|
||||||
this, SLOT(requestNewFortune()));
|
this, &Client::requestNewFortune);
|
||||||
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
|
connect(quitButton, &QPushButton::clicked, this, &Client::close);
|
||||||
connect(socket, SIGNAL(readyRead()), this, SLOT(readFortune()));
|
connect(socket, &QLocalSocket::readyRead, this, &Client::readFortune);
|
||||||
connect(socket, SIGNAL(error(QLocalSocket::LocalSocketError)),
|
connect(socket, QOverload<QLocalSocket::LocalSocketError>::of(&QLocalSocket::error),
|
||||||
this, SLOT(displayError(QLocalSocket::LocalSocketError)));
|
this, &Client::displayError);
|
||||||
|
|
||||||
QGridLayout *mainLayout = new QGridLayout;
|
QGridLayout *mainLayout = new QGridLayout(this);
|
||||||
mainLayout->addWidget(hostLabel, 0, 0);
|
mainLayout->addWidget(hostLabel, 0, 0);
|
||||||
mainLayout->addWidget(hostLineEdit, 0, 1);
|
mainLayout->addWidget(hostLineEdit, 0, 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());
|
||||||
hostLineEdit->setFocus();
|
hostLineEdit->setFocus();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,11 +106,9 @@ void Client::requestNewFortune()
|
|||||||
|
|
||||||
void Client::readFortune()
|
void Client::readFortune()
|
||||||
{
|
{
|
||||||
QDataStream in(socket);
|
|
||||||
in.setVersion(QDataStream::Qt_4_0);
|
|
||||||
|
|
||||||
if (blockSize == 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))
|
if (socket->bytesAvailable() < (int)sizeof(quint32))
|
||||||
return;
|
return;
|
||||||
in >> blockSize;
|
in >> blockSize;
|
||||||
@ -123,7 +121,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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,21 +134,22 @@ void Client::displayError(QLocalSocket::LocalSocketError socketError)
|
|||||||
{
|
{
|
||||||
switch (socketError) {
|
switch (socketError) {
|
||||||
case QLocalSocket::ServerNotFoundError:
|
case QLocalSocket::ServerNotFoundError:
|
||||||
QMessageBox::information(this, tr("Fortune Client"),
|
QMessageBox::information(this, tr("Local Fortune Client"),
|
||||||
tr("The host was not found. Please check the "
|
tr("The host was not found. Please make sure "
|
||||||
"host name and port settings."));
|
"that the server is running and that the "
|
||||||
|
"server name is correct."));
|
||||||
break;
|
break;
|
||||||
case QLocalSocket::ConnectionRefusedError:
|
case QLocalSocket::ConnectionRefusedError:
|
||||||
QMessageBox::information(this, tr("Fortune Client"),
|
QMessageBox::information(this, tr("Local Fortune Client"),
|
||||||
tr("The connection was refused by the peer. "
|
tr("The connection was refused by the peer. "
|
||||||
"Make sure the fortune server is running, "
|
"Make sure the fortune server is running, "
|
||||||
"and check that the host name and port "
|
"and check that the server name "
|
||||||
"settings are correct."));
|
"is correct."));
|
||||||
break;
|
break;
|
||||||
case QLocalSocket::PeerClosedError:
|
case QLocalSocket::PeerClosedError:
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
QMessageBox::information(this, tr("Fortune Client"),
|
QMessageBox::information(this, tr("Local Fortune Client"),
|
||||||
tr("The following error occurred: %1.")
|
tr("The following error occurred: %1.")
|
||||||
.arg(socket->errorString()));
|
.arg(socket->errorString()));
|
||||||
}
|
}
|
||||||
|
@ -52,11 +52,11 @@
|
|||||||
#define CLIENT_H
|
#define CLIENT_H
|
||||||
|
|
||||||
#include <QDialog>
|
#include <QDialog>
|
||||||
|
#include <QDataStream>
|
||||||
|
|
||||||
#include <qlocalsocket.h>
|
#include <qlocalsocket.h>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
class QDialogButtonBox;
|
|
||||||
class QLabel;
|
class QLabel;
|
||||||
class QLineEdit;
|
class QLineEdit;
|
||||||
class QPushButton;
|
class QPushButton;
|
||||||
@ -68,7 +68,7 @@ class Client : public QDialog
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Client(QWidget *parent = 0);
|
explicit Client(QWidget *parent = nullptr);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void requestNewFortune();
|
void requestNewFortune();
|
||||||
@ -77,16 +77,15 @@ private slots:
|
|||||||
void enableGetFortuneButton();
|
void enableGetFortuneButton();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QLabel *hostLabel;
|
|
||||||
QLineEdit *hostLineEdit;
|
QLineEdit *hostLineEdit;
|
||||||
QLabel *statusLabel;
|
|
||||||
QPushButton *getFortuneButton;
|
QPushButton *getFortuneButton;
|
||||||
QPushButton *quitButton;
|
QLabel *statusLabel;
|
||||||
QDialogButtonBox *buttonBox;
|
|
||||||
|
|
||||||
QLocalSocket *socket;
|
QLocalSocket *socket;
|
||||||
QString currentFortune;
|
QDataStream in;
|
||||||
quint32 blockSize;
|
quint32 blockSize;
|
||||||
|
|
||||||
|
QString currentFortune;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -55,6 +55,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("Local Fortune Client"));
|
||||||
Client client;
|
Client client;
|
||||||
client.show();
|
client.show();
|
||||||
return app.exec();
|
return app.exec();
|
||||||
|
@ -58,8 +58,8 @@
|
|||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
QApplication app(argc, argv);
|
QApplication app(argc, argv);
|
||||||
|
QGuiApplication::setApplicationDisplayName(Server::tr("Local Fortune Server"));
|
||||||
Server server;
|
Server server;
|
||||||
server.show();
|
server.show();
|
||||||
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
|
|
||||||
return app.exec();
|
return app.exec();
|
||||||
}
|
}
|
||||||
|
@ -60,22 +60,21 @@
|
|||||||
Server::Server(QWidget *parent)
|
Server::Server(QWidget *parent)
|
||||||
: QDialog(parent)
|
: QDialog(parent)
|
||||||
{
|
{
|
||||||
statusLabel = new QLabel;
|
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
||||||
statusLabel->setWordWrap(true);
|
|
||||||
quitButton = new QPushButton(tr("Quit"));
|
|
||||||
quitButton->setAutoDefault(false);
|
|
||||||
|
|
||||||
server = new QLocalServer(this);
|
server = new QLocalServer(this);
|
||||||
if (!server->listen("fortune")) {
|
if (!server->listen("fortune")) {
|
||||||
QMessageBox::critical(this, tr("Fortune Server"),
|
QMessageBox::critical(this, tr("Local Fortune Server"),
|
||||||
tr("Unable to start the server: %1.")
|
tr("Unable to start the server: %1.")
|
||||||
.arg(server->errorString()));
|
.arg(server->errorString()));
|
||||||
close();
|
close();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QLabel *statusLabel = new QLabel;
|
||||||
|
statusLabel->setWordWrap(true);
|
||||||
statusLabel->setText(tr("The server is running.\n"
|
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.")
|
fortunes << tr("You've been leading a dog's life. Stay off the furniture.")
|
||||||
<< tr("You've got to think about tomorrow.")
|
<< 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("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.");
|
||||||
|
|
||||||
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
|
QPushButton *quitButton = new QPushButton(tr("Quit"));
|
||||||
connect(server, SIGNAL(newConnection()), this, SLOT(sendFortune()));
|
quitButton->setAutoDefault(false);
|
||||||
|
connect(quitButton, &QPushButton::clicked, this, &Server::close);
|
||||||
|
connect(server, &QLocalServer::newConnection, this, &Server::sendFortune);
|
||||||
|
|
||||||
QHBoxLayout *buttonLayout = new QHBoxLayout;
|
QHBoxLayout *buttonLayout = new QHBoxLayout;
|
||||||
buttonLayout->addStretch(1);
|
buttonLayout->addStretch(1);
|
||||||
buttonLayout->addWidget(quitButton);
|
buttonLayout->addWidget(quitButton);
|
||||||
buttonLayout->addStretch(1);
|
buttonLayout->addStretch(1);
|
||||||
|
|
||||||
QVBoxLayout *mainLayout = new QVBoxLayout;
|
QVBoxLayout *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::sendFortune()
|
void Server::sendFortune()
|
||||||
{
|
{
|
||||||
QByteArray block;
|
QByteArray block;
|
||||||
QDataStream out(&block, QIODevice::WriteOnly);
|
QDataStream out(&block, QIODevice::WriteOnly);
|
||||||
out.setVersion(QDataStream::Qt_4_0);
|
out.setVersion(QDataStream::Qt_5_10);
|
||||||
out << (quint32)0;
|
const int fortuneIndex = QRandomGenerator::bounded(0, fortunes.size());
|
||||||
out << fortunes.at(qrand() % fortunes.size());
|
const QString &message = fortunes.at(fortuneIndex);
|
||||||
out.device()->seek(0);
|
out << quint32(message.size());
|
||||||
out << (quint32)(block.size() - sizeof(quint32));
|
out << message;
|
||||||
|
|
||||||
QLocalSocket *clientConnection = server->nextPendingConnection();
|
QLocalSocket *clientConnection = server->nextPendingConnection();
|
||||||
connect(clientConnection, SIGNAL(disconnected()),
|
connect(clientConnection, &QLocalSocket::disconnected,
|
||||||
clientConnection, SLOT(deleteLater()));
|
clientConnection, &QLocalSocket::deleteLater);
|
||||||
|
|
||||||
clientConnection->write(block);
|
clientConnection->write(block);
|
||||||
clientConnection->flush();
|
clientConnection->flush();
|
||||||
|
@ -64,14 +64,12 @@ class Server : public QDialog
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Server(QWidget *parent = 0);
|
explicit Server(QWidget *parent = nullptr);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void sendFortune();
|
void sendFortune();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QLabel *statusLabel;
|
|
||||||
QPushButton *quitButton;
|
|
||||||
QLocalServer *server;
|
QLocalServer *server;
|
||||||
QStringList fortunes;
|
QStringList fortunes;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user